) U; A( i* M+ H; T+ S( q" B7 e二维数组示例代码如下:: {! h. c3 ^# C
) I9 L! x7 }8 k9 ~4 u, l$ q" d三维数组示例代码如下: 9 v( f! H$ s9 B8 ?9 l$ p 8 F+ ^# }# S" p- j# b" Z4 V/ |0 |( x
4. 数组形状与类型变化) [ `% t9 M, m( f/ u
( p, L6 @: r4 R! m: A T1 z
1. ndarray.reshape(shape[, order]) Returns an array containing the same data with a new shape. 示例代码如下: & p" {/ m' L6 }6 Z & v( F2 b3 {& V, ?9 Z2. ndarray.resize(new_shape[, refcheck]) Change shape and size of array in-place. 示例代码如下: / ^6 m; q$ h/ k8 b6 H* t& U/ d2 K2 P& y X4 f0 }$ N2 o
3. 修改类型 ndarray.astype(type) 示例代码如下:6 e" R( w& N( k0 \
* `) c) I: |7 [4 c- D
4. 修改小数位数 ndarray.round(arr, out) Return a with each element rounded to the given number of decimals. 示例代码如下: 5 A* I" M/ g- b- r/ S+ J8 m3 t; ~
5. ndarray.flatten([order]) Return a copy of the array collapsed into one dimension. 示例代码如下: c. M5 \3 P( a! ?8 t 3 [' o( q# {9 n$ \* |; a M/ ^6. ndarray.T 数组的转置 将数组的行、列进行互换 示例代码如下: ; e" ^# H; u, m" n/ @ S# w! v7 ~! ?0 G# ?$ P& K! K. z$ w
7. ndarray.tostring([order])或者ndarray.tobytes([order]) Construct Python bytes containing the raw data bytes in the array. 转换成bytes ) f9 V0 G2 F+ ^! X6 q* W z5 o3 S: i! h! z4 p
8. ndarray.copy([order]) Return a copy of the array. 当我们不想修改某个数据的时候,就可以去进行拷贝操作。在拷贝的数据上进行操作,示例代码如下:: A+ {: P+ {9 Y1 u; }1 _
9 \1 \) i+ i6 T) F% b. ~: c5 H! w
8 m; z9 Y3 O- u% Y! P- T4. 数组运算% L. k1 p8 U5 o" L; M6 O8 x
7 O8 j7 I4 ]% f' t" z1 T# |4.1 逻辑运算( a s( Q/ [' ?' ^
2 D$ S/ G# v1 S . |4 x5 |6 `& F: L# ^1 z通用判断函数,np.all(),示例代码如下:5 y2 ]; k0 {/ q" \
6 L) ]4 n# d9 |* u# n
np.unique():返回新的数组的数值,不存在重复的值,示例代码如下: ' p# B% L. {% C) [. { 1 B/ A- R% i+ \& xnp.where (三元运算符):通过使用np.where能够进行更加复杂的运算,示例代码如下:5 ?# I/ h/ X! f! n$ I. ~' z- c
" G$ Y' ]9 I# m# v% A' }3 e& @- X4 g3 n; G' |. U# s
4.2 统计运算 1 `* R+ }: u2 n& g7 X' Z6 p * L0 K, C/ j, w. z, _ Y9 S: X {在数据挖掘/机器学习领域,统计指标的值也是我们分析问题的一种方式。注意:进行统计的时候,axis轴 的取值并不一定, NumPy中不同的API轴的值都不一样,在这里,axis 0代表列, axis 1代表行 去进行统计。常用的指标如下:5 r! X1 y: y* @$ u& S6 t
+ E; f8 \& ]( K' Xmin(a[, axis, out, keepdims]) Return the minimum of an array or minimum along an axis. 示例代码如下:* [8 J* T/ ^, ]
S8 A3 D: P% }2 t/ }; d" ~
max(a[, axis, out, keepdims]) Return the maximum of an array or maximum along an axis. 示例代码如下: & |( T2 R0 [ P5 k/ n: q ) g! B+ V; y* i5 Z) Y0 v9 ]median(a[, axis, out, overwrite_input, keepdims]) Compute the median along the specified axis. 示例代码如下:' b! n* o9 e1 i& ]3 v5 c* [: C
/ k( ]' ]* ?( r0 i: f3 S* ?# h: A) `mean(a[, axis, dtype, out, keepdims]) Compute the arithmetic mean along the specified axis. 示例代码如下:! H4 Z( E7 t' E6 m6 M
+ Y1 u- r/ H0 {; `std(a[, axis, dtype, out, ddof, keepdims]) Compute the standard deviation along the specified axis. 示例代码如下:6 E1 P: x4 x$ w& s0 h
8 o0 {7 X0 p% W- ^; z
var(a[, axis, dtype, out, ddof, keepdims]) Compute the variance along the specified axis. 示例代码如下: ' t6 O6 `( r$ a2 o4 {! O% a+ i# V; N6 x2 z( l O8 i
np.argmax(temp, axis=) 示例代码如下:3 Z3 R2 n$ L) G
: `7 l- b- u% V& r& x5 j
np.argmin(temp, axis=) 示例代码如下:8 }2 o4 z& w, M% R- [% `; A3 B
/ H8 O' i9 _( I6 U
4.3 数组间运算+ k; q. h9 ?, L! H W$ ]
6 M) |) @5 Z: Q I5 o
数组与数的运算,示例代码如下: ( E. G7 P& ?! M - c6 r! ~3 e# A矩阵运算,什么是矩阵?矩阵,英文matrix,和array的区别矩阵必须是2维的,但是array可以是多维的。示例代码如下:) x5 o' v. M1 `6 A6 L
: e1 m: q- F2 I
! C7 L, |# J0 }. |( O
4.4 合并分割 , N& y) _. i" S" u1 K z; \ h; F$ j' C6 H4 a4 X
numpy.concatenate((a1, a2, …), axis=0) 示例代码如下: 1 P2 o! e# o5 ], S. w2 _% D7 o0 O9 B
numpy.hstack(tup) Stack arrays in sequence horizontally (column wise). 示例代码如下:4 I' { A/ a4 R' o s$ h6 p
6 i! G& d8 N- G9 ~2 h( Enumpy.vstack(tup) Stack arrays in sequence vertically (row wise). 示例代码如下:- _, {5 v2 a g
/ S' [$ G5 U1 R$ b' w3 C9 ~- vnumpy.split(ary, indices_or_sections, axis=0) Split an array into multiple sub-arrays. 示例代码如下:% `( t! _$ j- u6 N
1 s5 n' g* u$ u" B. n. ^$ Z- }
————————————————3 Z Y- C |! W% X
版权声明:本文为CSDN博主「Amo Xiang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 " p: m5 g |: d1 H原文链接:https://blog.csdn.net/xw1680/article/details/105931313 3 N+ q% U. W* L& x& @0 S: s9 P; `( B) w$ A