) {0 U/ F* b3 h4. 数组形状与类型变化# O7 O! e9 e4 l& R a+ E, }! N
# |& {7 n* F2 P3 ~! C0 j& A5 K
1. ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape. 示例代码如下: . f" J. A1 q. W6 {' ]( ^' D: M! s+ c, d9 Z+ `2 t
2. ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place. 示例代码如下: 5 j0 p) e( ]' h, h/ u, ] 1 I# g, g, d: |' R' N+ m Y# J( o2 J3. 修改类型 ndarray.astype(type) 示例代码如下:% q, C: o8 D) I2 E l" N! F
}0 R3 x& p; U A; C6 w3 |) V4. 修改小数位数 ndarray.round(arr, out) Return a with each element rounded to the given number of decimals. 示例代码如下: 4 {6 }0 p( c7 b8 E0 ^5 E; l, e " R( _6 t- K* m: q8 ~: o5. ndarray.flatten([order]) Return a copy of the array collapsed into one dimension. 示例代码如下:7 R# E! k& P! ~5 E9 }
5 B$ o! z. R& @1 ~; O" y# _6. ndarray.T 数组的转置 将数组的行、列进行互换 示例代码如下: ! r) ~# A/ ]5 l. n7 }4 {2 F' R% Q# b
7. ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array. 转换成bytes , @9 @; d# c# K4 b. E0 G4 o) Q 2 M3 v+ ~/ s2 Y9 N9 h8. ndarray.copy([order]) Return a copy of the array. 当我们不想修改某个数据的时候,就可以去进行拷贝操作。在拷贝的数据上进行操作,示例代码如下:! x+ J( v+ y" G% w8 [* g" L
: `& i3 k2 S: I& W, L6 }( c # j$ k$ {: b1 Y. K9 r4. 数组运算 C' N9 _6 j: y' C* I4 j
8 q/ }, T7 C# u' X, {: ^4.1 逻辑运算5 z1 f, I4 N! O7 Y: ~. q0 ^6 `
/ y8 U8 a3 b" C8 w9 C
5 T& @* g& B; ?5 F5 i v
通用判断函数,np.all(),示例代码如下: ' R' {; [% [: J$ V* ?. S( [+ }/ r6 H4 n. K
np.unique():返回新的数组的数值,不存在重复的值,示例代码如下:! j+ F5 |, G- F* x @0 {
2 E- E# t+ R' o" J U
np.where (三元运算符):通过使用np.where能够进行更加复杂的运算,示例代码如下: 7 k3 ~) J7 Z4 S . V' y9 G- k( ?8 D t# s- |% }4 [ + s; }; H' I' Z7 Y4 g( I4.2 统计运算 % j0 s2 z2 M/ c' Q3 B1 W8 g: s/ D* y2 Z5 Z8 A1 R( c, _" h( m
在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。注意:进行统计的时候,axis轴 的取值并不一定, NumPy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行 去进行统计。常用的指标如下: / {3 [7 Z6 u5 g& B3 R 8 g5 w: z+ S7 V; V0 y+ m" H" Y vmin(a[, axis, out, keepdims]) Return the minimum of an array or minimum along an axis. 示例代码如下:+ Z7 n4 V: K+ k+ W2 L* F, d6 z' e
8 u2 @) ?' l# d4 Mmax(a[, axis, out, keepdims]) Return the maximum of an array or maximum along an axis. 示例代码如下: 3 Q* g: ~# b. B, R9 \2 G7 H( j J8 W0 Z% {- P; T
median(a[, axis, out, overwrite_input, keepdims]) Compute the median along the specified axis. 示例代码如下:% b# G4 x9 H( @" G3 o" a( q
4 X0 k) g, d# Omean(a[, axis, dtype, out, keepdims]) Compute the arithmetic mean along the specified axis. 示例代码如下: 1 F( {2 ]6 ?# W 5 [- u ^! ^% o9 s6 nstd(a[, axis, dtype, out, ddof, keepdims]) Compute the standard deviation along the specified axis. 示例代码如下:1 ^5 j) a" O9 S9 V5 C( L* o
/ j% P9 `" x& Z0 ]6 ^var(a[, axis, dtype, out, ddof, keepdims]) Compute the variance along the specified axis. 示例代码如下:8 ^& Y, Q0 c9 O7 T) N
v7 H4 ]7 K1 i
np.argmax(temp, axis=) 示例代码如下: ) |. X% _ c. w# r' T + c5 R+ S# I, S! f# knp.argmin(temp, axis=) 示例代码如下:2 e8 P7 t: N* C
4 i! Y+ A9 I/ _+ [4 V- v4.3 数组间运算 & n7 U. h0 T" Z( d6 j2 s+ [/ B# {$ ~" ~) K
数组与数的运算,示例代码如下: * \0 @3 K5 @ X8 z1 d+ V4 M9 g# P$ ]" g9 J6 g2 T
矩阵运算,什么是矩阵?矩阵,英文matrix,和array的区别矩阵必须是2维的,但是array可以是多维的。示例代码如下:% c* N6 t% r1 G
& g. H% h1 f# c9 p, E; m
' a& F. E' t2 v$ M4.4 合并分割# |) L9 `: e: Q0 a) l. m. ]
9 K2 P* U: h1 s7 [8 [" q
numpy.concatenate((a1, a2, …), axis=0) 示例代码如下:! F4 U$ K' H3 G+ n7 |
# L* r u/ b. m8 s8 r! qnumpy.hstack(tup) Stack arrays in sequence horizontally (column wise). 示例代码如下: 5 l! u1 N0 [5 p& t 8 j5 U7 \5 A# C' K+ g' K8 O$ F4 s, Znumpy.vstack(tup) Stack arrays in sequence vertically (row wise). 示例代码如下: 3 l3 n2 M6 t; L3 F6 `/ ^( W1 ~. H7 F ( v6 y1 b' r' J" a9 `$ J7 Nnumpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays. 示例代码如下: 8 ~ s1 C5 T5 I ( F' i; M! s9 I———————————————— $ k6 B5 R2 Y( a* E版权声明:本文为CSDN博主「Amo Xiang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 / Z. L6 L( h& `4 J! y" ~7 W R原文链接:https://blog.csdn.net/xw1680/article/details/105931313 2 ` H# t4 M: g6 d6 F- Q* T2 T8 T! ?6 ?. n$ I/ p- |7 F