$ g* f6 T' d3 h& M( A2 Z4. 数组形状与类型变化 0 T3 q# i: O& W, v+ h* U* D, I; h5 [- g, D7 }, b; u
1. ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape. 示例代码如下:* x9 d# H% q$ r0 Z
$ y' L/ b& i( e, N: u- T8 m0 Q8 h2. ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place. 示例代码如下:' }' D; P( W3 e) W
2 p( U. L9 d: _- c- G" H! o& v
3. 修改类型 ndarray.astype(type) 示例代码如下: e: `0 P5 h9 t$ N1 |* R5 K + J5 I2 q8 X6 k5 d" m4. 修改小数位数 ndarray.round(arr, out) Return a with each element rounded to the given number of decimals. 示例代码如下:, Y* M9 d/ Q6 S
5 p% g2 W; {5 k7 \4 [; V* k! C
5. ndarray.flatten([order]) Return a copy of the array collapsed into one dimension. 示例代码如下:% n/ Z# H& V l: p3 T- O0 Y
l# G& q9 t5 O! R7 o z( s
6. ndarray.T 数组的转置 将数组的行、列进行互换 示例代码如下:" s6 O8 h* |1 Y
" \$ P. ?6 O- U0 T+ r3 D8 o- ?7. ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array. 转换成bytes' C' c, j; o0 m5 F0 _1 U( o
. C% l" e7 |4 S& p$ c j4 ^8. ndarray.copy([order]) Return a copy of the array. 当我们不想修改某个数据的时候,就可以去进行拷贝操作。在拷贝的数据上进行操作,示例代码如下:+ o( F. C% u1 c8 p% ]
, j6 h3 `/ c, v m
" |1 y, s. I$ \+ a+ Y4. 数组运算" S& i! A. i7 R3 W
1 n1 U$ }2 _' K4 @/ V3 X
4.1 逻辑运算 2 F- S/ h' D% x5 x# Q0 c ( D; U M5 ]9 C2 s( |- c. t6 t3 x. R
通用判断函数,np.all(),示例代码如下: % S' d6 B. w$ s6 g& Q9 j& L4 N' ? d
np.unique():返回新的数组的数值,不存在重复的值,示例代码如下: " W. C, b) b5 v) T2 M" m l" }) c$ ?( h, F: c1 O! v
np.where (三元运算符):通过使用np.where能够进行更加复杂的运算,示例代码如下: * U1 {/ Y3 h6 x4 { / \6 M/ d- e8 ? ' u! N2 O3 d; [. O3 p' i4.2 统计运算 |7 A& J5 K5 I4 p
L$ J1 n, N7 I8 T: \1 d+ _& h在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。注意:进行统计的时候,axis轴 的取值并不一定, NumPy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行 去进行统计。常用的指标如下: * {3 c l( B! |1 W0 P0 f3 K. s8 ~) L' V* C
min(a[, axis, out, keepdims]) Return the minimum of an array or minimum along an axis. 示例代码如下: % Q/ Q( d! L! G% W! } C & `3 k/ Z/ e4 ^! Z" ^+ F: Imax(a[, axis, out, keepdims]) Return the maximum of an array or maximum along an axis. 示例代码如下: - u* w. u/ Z3 P/ @( [# y! z" K: Z" _* Z& k2 v9 k' f
median(a[, axis, out, overwrite_input, keepdims]) Compute the median along the specified axis. 示例代码如下:- \2 }) `% n6 N, R8 C' H% T
7 G2 c" ]* Y& q( r0 Smean(a[, axis, dtype, out, keepdims]) Compute the arithmetic mean along the specified axis. 示例代码如下:& e7 I* q8 r3 s8 r
4 W+ a4 P" c9 k! z3 ?) t
std(a[, axis, dtype, out, ddof, keepdims]) Compute the standard deviation along the specified axis. 示例代码如下: I9 {- c+ U( W( x6 s" M! J) R) M s2 @
var(a[, axis, dtype, out, ddof, keepdims]) Compute the variance along the specified axis. 示例代码如下:% E F" d3 z" h! P
' B3 P) U. `3 ], L+ q3 k2 l
np.argmax(temp, axis=) 示例代码如下:/ r# m" i9 y6 o" y7 A- T" W; T% T
) N% X( _3 P$ a- qnp.argmin(temp, axis=) 示例代码如下: 0 f( X1 U/ r3 u) l6 B j + v' B9 l8 T* [! J0 l4.3 数组间运算 - \* A4 U* ~# Q" _( s. @" T; Z/ A0 T! U1 { j, S4 V+ Z
数组与数的运算,示例代码如下: / y: i: \; j7 Z% s% U+ B6 C/ u9 S& P H. _6 f0 w
矩阵运算,什么是矩阵?矩阵,英文matrix,和array的区别矩阵必须是2维的,但是array可以是多维的。示例代码如下: r: b }6 A6 q4 _: n1 K9 K% ^
, P2 g- T8 h7 {- L+ z0 P0 S
- J4 j! a" S+ Y: c0 i
4.4 合并分割 - C! T3 T6 }" w- v . b: k/ g0 s: e+ A: w* h# e2 ~numpy.concatenate((a1, a2, …), axis=0) 示例代码如下:4 u1 l9 R8 @( O
( Z' ]4 _) V, C) b
numpy.hstack(tup) Stack arrays in sequence horizontally (column wise). 示例代码如下:* M5 u" _* @ o) o6 q. @
, q) ^1 o' U8 j* `# lnumpy.vstack(tup) Stack arrays in sequence vertically (row wise). 示例代码如下: - J' p' w) }6 \5 S8 {: Q& {" c8 B3 C# T5 u; V3 {& `: G2 z
numpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays. 示例代码如下: 1 K& { a) f" t9 I5 P1 }3 G$ l) S U# |" b, Q5 K% b% T! O
———————————————— + x( O2 O9 {; c6 h- J7 B版权声明:本文为CSDN博主「Amo Xiang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。/ g% G0 w$ i- C- C0 a
原文链接:https://blog.csdn.net/xw1680/article/details/1059313135 s. Y3 A H5 X
! D' M8 W4 W% v, { y9 V