# M2 Y e; B$ K1 B2 p* |; d* Z + w5 R, a3 F8 N4 s7 }* | W4. 数组形状与类型变化( n' @, R j$ K+ g' |1 t0 P
3 r* Z: M7 l. i e* q- Q1. ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape. 示例代码如下:, e. E& l, P- C) Q+ N* q% M
6 S9 \: I6 D. G4 k9 h7 r g3. 修改类型 ndarray.astype(type) 示例代码如下: " u W2 k0 c1 s- Z( j% H" g- b9 B! i% E9 q# g& E: Y# j
4. 修改小数位数 ndarray.round(arr, out) Return a with each element rounded to the given number of decimals. 示例代码如下:5 W/ B( s! ^; ?7 H, }8 f0 x
& |( u( T3 |4 ?: p
5. ndarray.flatten([order]) Return a copy of the array collapsed into one dimension. 示例代码如下:# _- E A9 O, S& b
6 V9 [& r: K' C; i0 b9 s6 M; ~
6. ndarray.T 数组的转置 将数组的行、列进行互换 示例代码如下: 0 g8 h4 l( B3 r3 k$ ]6 | ) S) C$ }+ c. L4 |. M7. ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array. 转换成bytes % j3 ], y/ f: L( s! J& E$ L3 Z* Z3 e& b
8. ndarray.copy([order]) Return a copy of the array. 当我们不想修改某个数据的时候,就可以去进行拷贝操作。在拷贝的数据上进行操作,示例代码如下: + _8 X+ v# k& U8 ]0 j0 A1 q, O# E* V7 V
9 U9 H* S) S" l! ~3 A/ {2 g4. 数组运算 0 [9 C# \9 y- @" x& t- p U2 K* Z8 O
4.1 逻辑运算 # E# p! I! G6 m+ l* w4 c* d1 ]5 Y0 I3 T8 u4 T) Y& p
( J8 q0 I$ L4 p& l; H5 O
通用判断函数,np.all(),示例代码如下: % F& b/ w& w! F2 Q/ P! I" A* \, {5 C ( P' P1 L/ ^: u5 v8 Znp.unique():返回新的数组的数值,不存在重复的值,示例代码如下: ' b1 z# Z) T# n. s" s9 e# h8 r" U
np.where (三元运算符):通过使用np.where能够进行更加复杂的运算,示例代码如下: ' I7 M# [" Z+ t! U" }3 Q# [ $ d- h$ s" _' |; M. J- N2 ^6 i Q9 ?. c, O4 {/ `
4.2 统计运算 6 U& m; q2 n* p# Q' t , T8 `5 k' S& T在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。注意:进行统计的时候,axis轴 的取值并不一定, NumPy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行 去进行统计。常用的指标如下: % c. v0 ]1 y# `% K) h * ]9 P' o! }+ X. t1 U5 w5 Q6 xmin(a[, axis, out, keepdims]) Return the minimum of an array or minimum along an axis. 示例代码如下: O# g) H# z g' S. B: `7 ]9 c
+ n# \+ S7 {$ l9 g8 n$ ^, Gmax(a[, axis, out, keepdims]) Return the maximum of an array or maximum along an axis. 示例代码如下:6 ?9 N; \: J! J7 O% j
! ]' h' ?% T5 @4 \" X& ^
median(a[, axis, out, overwrite_input, keepdims]) Compute the median along the specified axis. 示例代码如下:4 E( D2 V# ?, G/ Y, r o
% g9 d* F( a7 i9 V) ]$ k8 c/ Zmean(a[, axis, dtype, out, keepdims]) Compute the arithmetic mean along the specified axis. 示例代码如下:0 I+ d7 u$ b# t- Y% d' F8 q. i
5 g) w! K: c: H& B9 ?# x( m3 Gstd(a[, axis, dtype, out, ddof, keepdims]) Compute the standard deviation along the specified axis. 示例代码如下:8 R1 z9 ?( R: Y$ `) \7 p
. i0 T- G8 f' h- ]5 [8 F" C' Pvar(a[, axis, dtype, out, ddof, keepdims]) Compute the variance along the specified axis. 示例代码如下: # H( `! s c Z7 f, T- t3 ?. F1 _3 V* j% j7 r% w) G' a
np.argmax(temp, axis=) 示例代码如下:$ u1 x6 }8 H. e7 _0 s
" H. F! H) \- I9 X7 y: h
np.argmin(temp, axis=) 示例代码如下:+ N9 Y' m4 C' K$ h' \ E7 ^4 ]- h. h
" T2 b4 y' B' C& n( N+ c s8 `
4.3 数组间运算 $ R7 X# C6 X. }9 S. f8 b: ^: m, q7 `& q _" o: \2 t
数组与数的运算,示例代码如下:; d1 l; @0 H. D8 V7 ~! L; w9 `% g
2 P3 O. e0 ?9 R2 i3 _# S( T
矩阵运算,什么是矩阵?矩阵,英文matrix,和array的区别矩阵必须是2维的,但是array可以是多维的。示例代码如下: - M6 q& ]. }. i( Y2 }8 F1 E ) U+ K% R! D* Q L+ p% g3 {( L0 g; V: X) U5 U
4.4 合并分割 0 w/ ^4 f) C+ d1 b3 E) F" z) X' {" L# a; S1 d+ P0 A
numpy.concatenate((a1, a2, …), axis=0) 示例代码如下:% m4 E. r: Q" l, r1 n. X
A$ ]9 ?( y+ n" a' m- t! D
numpy.hstack(tup) Stack arrays in sequence horizontally (column wise). 示例代码如下: ; x5 K; e' b7 v" a( h+ u( X0 V9 f& W6 @( C, `8 b! q
numpy.vstack(tup) Stack arrays in sequence vertically (row wise). 示例代码如下: ( N) \9 C. K, M( f1 _3 B# n: x% { C0 h
numpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays. 示例代码如下:: P8 y, F* ^2 b8 O' C& Y# k7 v
3 V5 d! o0 k4 K1 j6 A" v g
———————————————— - q1 M, S2 n" K版权声明:本文为CSDN博主「Amo Xiang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。3 J* t% O, R: ~8 @4 H
原文链接:https://blog.csdn.net/xw1680/article/details/105931313 - K& } k5 N8 x" @1 {% B& `) b) s$ X7 ?, H
' w2 {5 ^% Z* d' L) l$ c3 ~ s