7 ?& k. B' T( @ z) U4. 数组形状与类型变化1 @+ d1 M9 c1 z. j K
7 P! Y& C0 z* h# P- `
1. ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape. 示例代码如下: % X# ~: g( |% d5 b6 \. ^1 \, D5 S/ c/ b7 Z) W
2. ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place. 示例代码如下: J# ^) j2 s) v; {9 w* R1 z: p5 |. j* [" v
3. 修改类型 ndarray.astype(type) 示例代码如下:, A5 P) @' L1 V: W
$ J. u3 Z# [3 Y" d1 \% o# a* }4. 修改小数位数 ndarray.round(arr, out) Return a with each element rounded to the given number of decimals. 示例代码如下:$ [. c9 z7 U8 z- q- A# o) j2 e: d$ T& U
4 m& Q8 e2 u N3 L5. ndarray.flatten([order]) Return a copy of the array collapsed into one dimension. 示例代码如下: " X! v( q1 {, W5 ^1 x 2 J" P( m9 h; S, c6. ndarray.T 数组的转置 将数组的行、列进行互换 示例代码如下: - L6 o4 b, x) W* w! L2 q: d' V( L. S
7. ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array. 转换成bytes5 Z: J" G# T# E3 t
+ P7 M: E# }6 F9 W
8. ndarray.copy([order]) Return a copy of the array. 当我们不想修改某个数据的时候,就可以去进行拷贝操作。在拷贝的数据上进行操作,示例代码如下:: ]& Z9 @; k( J- T f N7 B _
" y |) T( G6 w, H+ s' @" @& ~ # i n4 [3 L% _4 y4. 数组运算5 d% e6 B. ?7 d* u. ~& a1 {
+ J8 m$ }. x* L- j
4.1 逻辑运算% j! f7 c0 l. }9 z' |) M1 d
# u7 D' Q, Q* m. f) L, J/ L1 v
7 o5 e7 o5 c+ ^- V8 U8 f, I
通用判断函数,np.all(),示例代码如下: 4 j3 E' V1 ?. N) t' v3 g; V# l' B: T; Z
np.unique():返回新的数组的数值,不存在重复的值,示例代码如下: : K' e" |7 @ ?5 X K! w 8 N7 Y0 _2 w: H* F+ e/ ]np.where (三元运算符):通过使用np.where能够进行更加复杂的运算,示例代码如下: . J) I* O! }4 ?2 s/ K4 e; [; U$ a; K3 V4 O e2 x' R
+ p n# @! _( `. x
4.2 统计运算2 ^1 e* \ N& b. @/ a a ]
5 j% m1 l2 X( v7 C c+ C: u4 l在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。注意:进行统计的时候,axis轴 的取值并不一定, NumPy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行 去进行统计。常用的指标如下: 2 A4 h: D0 C' R& X # L$ G- W P4 k9 r' Wmin(a[, axis, out, keepdims]) Return the minimum of an array or minimum along an axis. 示例代码如下: ; s# X/ W d' h: k5 h* C$ F* P9 ] ( f3 d2 o% r6 r9 [2 y( Amax(a[, axis, out, keepdims]) Return the maximum of an array or maximum along an axis. 示例代码如下: 0 B) H# q4 }2 m! g+ [& E: F ; k3 O7 u8 F8 g) c/ ?" M3 F- a gmedian(a[, axis, out, overwrite_input, keepdims]) Compute the median along the specified axis. 示例代码如下:( Z. q2 t4 n9 k; u b9 `
, h$ N4 O, T! ~ U5 m( w
mean(a[, axis, dtype, out, keepdims]) Compute the arithmetic mean along the specified axis. 示例代码如下: 3 t3 R& s I- E' S5 f1 n) K9 O- ]3 `, ?& Y
std(a[, axis, dtype, out, ddof, keepdims]) Compute the standard deviation along the specified axis. 示例代码如下: 0 \4 {! P; k) y & c$ O# z' n. g" kvar(a[, axis, dtype, out, ddof, keepdims]) Compute the variance along the specified axis. 示例代码如下: 5 l4 Q! K6 _! T, N: R8 ^+ ]& [+ m# y( f% U6 t- s: v, H
np.argmax(temp, axis=) 示例代码如下: k2 }& N# P2 W- d
8 r/ m- X' t8 {3 T$ d3 T$ P
np.argmin(temp, axis=) 示例代码如下:9 k- E$ f$ X5 s8 v. }" ~) U
4 u) h8 l" B/ }- G i2 W4.3 数组间运算* ?. i; b& Q; q
) s+ Z! x1 u. t- S数组与数的运算,示例代码如下:- p2 H3 O) }( E' ?' P4 M
- K9 S) P0 i9 V. B* E
矩阵运算,什么是矩阵?矩阵,英文matrix,和array的区别矩阵必须是2维的,但是array可以是多维的。示例代码如下: . ?$ }' w* W0 A( M4 |! D+ L8 F6 N b- C- }: u- z$ g
# b4 c$ g8 U4 g! j0 {
4.4 合并分割) g' T/ x1 z/ m
+ s" p' g- a* g% n( I$ |& J2 anumpy.concatenate((a1, a2, …), axis=0) 示例代码如下:& k. m1 E1 C" _: @& E
8 ^1 K) j! W& w! h
numpy.hstack(tup) Stack arrays in sequence horizontally (column wise). 示例代码如下: 0 s9 j' W3 g$ Q8 `5 z- T) ~, N 6 X7 o7 f+ b. T, B4 h2 y k/ j0 Inumpy.vstack(tup) Stack arrays in sequence vertically (row wise). 示例代码如下: ( X' S, a ]; t% R; H ) d e! L5 N/ D9 L4 ynumpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays. 示例代码如下: ( M D( E: y4 p. n) W9 a2 i. w% \/ U3 K4 Z0 \- q; ?
————————————————0 \9 m1 v& b+ q P& N
版权声明:本文为CSDN博主「Amo Xiang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 4 D7 t7 y2 ^- |9 [( B原文链接:https://blog.csdn.net/xw1680/article/details/105931313' k. x0 L" k# M0 c4 h+ U
5 t, ^: O6 U5 H: q- ~0 t. O9 V2 ~. B% Y1 S! h1 e1 e