* C' w2 W! g4 w- S. t0 z2 i三维数组示例代码如下:0 g8 I: C+ |" j% p, A8 |
* Z; a$ q6 q2 ]0 s+ F5 U3 ]) F' ^' e9 f& Q3 |3 M
4. 数组形状与类型变化 9 m( Q" N8 {8 n/ U0 s % H3 l/ H- ]0 E6 }0 X+ x2 }% J$ ~8 s' @1. ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape. 示例代码如下:6 k, a) s d* t" G- { L
6 \+ C u+ S5 u" }, E$ `2. ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place. 示例代码如下:; D/ W. M; M O5 r
5 m% ?# F3 N+ D/ F9 s; J4. 修改小数位数 ndarray.round(arr, out) Return a with each element rounded to the given number of decimals. 示例代码如下:$ L" p( ^. h% {- C4 T4 u
) B0 B4 {; Y/ p5. ndarray.flatten([order]) Return a copy of the array collapsed into one dimension. 示例代码如下:) h+ M V9 d8 O8 Y
' H7 ]- I; R, B" A9 c8 e
6. ndarray.T 数组的转置 将数组的行、列进行互换 示例代码如下: ' Q. q, U& [- O+ W! M+ j& a7 W% |8 B1 j: [
7. ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array. 转换成bytes : m4 S) j4 \. Q 6 d8 a: D( C* R$ |; j, u5 `; H$ f8. ndarray.copy([order]) Return a copy of the array. 当我们不想修改某个数据的时候,就可以去进行拷贝操作。在拷贝的数据上进行操作,示例代码如下:9 r$ a, p4 o" i8 W1 p6 i: F
" C) i3 k" y3 K8 e" y w
a* \0 M' Z: b1 ^
4. 数组运算! E. O6 l2 x2 |1 I7 b
6 J: e$ a) H+ f) ^$ i; Q4.1 逻辑运算$ R$ m+ {7 e$ w
) y+ g" S `- b7 X# v2 l7 z# Q3 J6 m
通用判断函数,np.all(),示例代码如下: % w" r* Z3 D# e5 a! x9 Z |# e/ d5 d$ M) Z: Y2 `) ~
np.unique():返回新的数组的数值,不存在重复的值,示例代码如下: 3 h$ q) v. a8 w' E$ `0 n% O 6 B6 e$ X2 K' g% }np.where (三元运算符):通过使用np.where能够进行更加复杂的运算,示例代码如下:- P" G7 n9 D% |4 U; [% J
7 o* ^# ~, m( ^& ?& b' D) _' [: Y5 r0 q
4.2 统计运算! J5 V8 a* U* `+ { d h
' j( `" H" O, r
在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。注意:进行统计的时候,axis轴 的取值并不一定, NumPy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行 去进行统计。常用的指标如下: ( }4 Q) g& [$ E. L) e% e' g: F+ ]- Q! c+ S* b+ a) Y/ s8 N4 _/ w
min(a[, axis, out, keepdims]) Return the minimum of an array or minimum along an axis. 示例代码如下: 9 f5 E# v- e- Q* K1 ?4 [ U8 S! X$ u3 _! a+ g
max(a[, axis, out, keepdims]) Return the maximum of an array or maximum along an axis. 示例代码如下: 9 r( j- K: F; C $ a2 \- a4 f& m5 \9 O/ [8 qmedian(a[, axis, out, overwrite_input, keepdims]) Compute the median along the specified axis. 示例代码如下: 0 n O8 C5 C6 T2 } + g3 y; J& s* |# i8 s+ N/ B9 _, omean(a[, axis, dtype, out, keepdims]) Compute the arithmetic mean along the specified axis. 示例代码如下: 5 ~3 [' c( S* ~) G# w: \/ c8 T5 N/ Q/ f" D! ~6 G
std(a[, axis, dtype, out, ddof, keepdims]) Compute the standard deviation along the specified axis. 示例代码如下:* Q, P. w, g) o
" G6 n6 l4 W- X2 Y: s+ @& o5 l! j4 d
var(a[, axis, dtype, out, ddof, keepdims]) Compute the variance along the specified axis. 示例代码如下: : F e; J- e; s/ o5 O $ x: Q' r$ K5 m( j ^9 mnp.argmax(temp, axis=) 示例代码如下: : J# j Z; c! W, F C3 z* h3 q) v. i" X d! c- [
np.argmin(temp, axis=) 示例代码如下: k3 Y% r& q( u3 q+ P6 A/ X/ R
* `- t' I- i4 f5 z. L( C
4.3 数组间运算( ^- c% G* L& p" A+ J3 y: e
2 c6 o% W& ?7 a
数组与数的运算,示例代码如下:: m+ e! A( Q7 S9 w' T h