N' I. D) Y& v/ t9 J二维数组示例代码如下:/ L' ~& F+ |, z
3 R h* n5 \6 ~ k. J
三维数组示例代码如下:% k! D8 |! d4 t* p* G+ d2 ~1 w- y
" B2 [+ `% b( o8 x% B" _/ J/ o C0 a
4. 数组形状与类型变化 0 [8 N6 z1 G0 v" B% B/ f0 J y' U8 p1 V v, t- G" V9 K
1. ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape. 示例代码如下: . e$ i# R- x" o) `9 h - ]. r6 v; J* B7 U3 O2. ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place. 示例代码如下: " V" d% S& f9 v# o8 x) W/ v / }, A" F. W, d( s3. 修改类型 ndarray.astype(type) 示例代码如下:& @+ n+ L: \5 H Y$ P+ M. E* x
# _! {9 p1 `) z) x2 V4. 修改小数位数 ndarray.round(arr, out) Return a with each element rounded to the given number of decimals. 示例代码如下:; v7 Y1 ?) \2 }3 ^' |. m% I
1 V* {2 d$ T$ h9 A. U. q' J' i5. ndarray.flatten([order]) Return a copy of the array collapsed into one dimension. 示例代码如下: ( F+ ?6 O5 }" S+ `) L' I! a/ H6 R, |/ R
6. ndarray.T 数组的转置 将数组的行、列进行互换 示例代码如下: . }" f4 n3 n6 D8 m4 `) t( |1 H: e# H5 I0 l# ]0 P, i! h9 x; ?
7. ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array. 转换成bytes, ^+ K7 [9 ]' i+ O9 c) U; j& @& @# b( @
. H9 T3 ~9 e& u/ W# _5 `! z6 K8. ndarray.copy([order]) Return a copy of the array. 当我们不想修改某个数据的时候,就可以去进行拷贝操作。在拷贝的数据上进行操作,示例代码如下:8 w7 e8 h- K! n8 _- C8 G( P6 b
: ]/ l8 \. U ?# H1 h" S2 S2 V0 x+ m4 k
4. 数组运算) X# o3 j3 p* s. m- V
3 K% k$ d! _! K4 }) U- X6 w% K) b( ^
4.1 逻辑运算2 F$ C' i2 m! [. u, E0 y! o
: d. c* j" S6 z$ D8 u
, O/ }, [! _7 G/ l# }通用判断函数,np.all(),示例代码如下: ' H, s1 z' V5 y5 ~) r: h6 ` a9 _0 s% M" ^) @% i0 X
np.unique():返回新的数组的数值,不存在重复的值,示例代码如下:' R0 N# b- H* h; p! n/ p' u2 p
8 ~' |, r) U3 M* L; |* E4 U
np.where (三元运算符):通过使用np.where能够进行更加复杂的运算,示例代码如下: 6 R t1 r8 N" \* Q$ _0 @% ]' {: E( Y+ v8 `* L9 f+ u
1 L; d3 V/ |2 Z! t2 d- `1 _0 X
4.2 统计运算2 f9 \. h8 t1 K: `
2 t+ h5 W7 J ~+ S7 b% ~% S
在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。注意:进行统计的时候,axis轴 的取值并不一定, NumPy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行 去进行统计。常用的指标如下:# q0 |% O. X6 L, U
7 y% ?, N) v$ u( s2 s% {min(a[, axis, out, keepdims]) Return the minimum of an array or minimum along an axis. 示例代码如下:) _( p( I# |4 d' U- T- r7 |
2 k. S, ^& a+ w$ S
max(a[, axis, out, keepdims]) Return the maximum of an array or maximum along an axis. 示例代码如下:- }% r& [7 ~0 w% p0 N# i4 c" _+ s
; u4 `! ^% [+ H2 ?median(a[, axis, out, overwrite_input, keepdims]) Compute the median along the specified axis. 示例代码如下:# W# V1 E) l2 G) d3 y
2 ~3 f0 ~( M9 N6 e) A! Zmean(a[, axis, dtype, out, keepdims]) Compute the arithmetic mean along the specified axis. 示例代码如下: 9 d1 o! x( A2 e% Q, M: `, T, b ! T0 D* `1 [+ K# [2 z1 m# _; astd(a[, axis, dtype, out, ddof, keepdims]) Compute the standard deviation along the specified axis. 示例代码如下:: w- L, H7 S/ H7 d A1 h, [+ }
9 v4 F) l) i2 P8 X———————————————— * A. k. j9 P" u; ?; d, o版权声明:本文为CSDN博主「Amo Xiang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。! {' F6 {6 A h {- W
原文链接:https://blog.csdn.net/xw1680/article/details/1059313139 h. Q/ x# w' @- x0 n/ p
6 |* q" u. d: R& ~; S0 i