numpy 百度百科传送门
1. 导入numpy并查看版本
import numpy as np
print ( np. __version__)
2. 创建numpy数组
2.1 np.array(list):创建一维数组(向量)
w = np. array( [ 1 , 2 , 3 ] )
2.2 np.array(list):创建多维数组(矩阵)
X = np. array(
[
[ 1 , 2 , 3 ] ,
[ 4 , 5 , 6 ]
]
)
2.3 np.zeros(shape, dtype):创建元素值全为0的numpy数组
zero = np. zeros( shape= ( 2 , 3 ) , dtype= int )
2.4 np.ones(shape, dtype):创建元素值全为1的numpy数组
one = np. ones( shape= ( 2 , 3 ) , dtype= float )
2.5 np.full(fill_value, shape, dtype):创建元素值全为指定元素的numpy数组
M = np. full( fill_value= 2021 , shape= ( 3 , 3 ) , dtype= int )
3. numpy区间与区间划分
3.1 np.arange(start, stop, step):区间
M = np. arange( start= 1 , stop= 10 , step= 2 )
3.2 np.linspace(start, stop, num):区间划分
M = np. linspace( start= 0 , stop= 10 , num= 5 )
4. numpy中的随机数
4.1 np.random.seed(seed):设置随机种子
np. random. seed( 2021 )
4.2 np.random.random(size):随机生成5个(0, 1)之间的浮点数
array_rand = np. random. random( size= 5 )
4.3 np.random.randint(low, high, size):随机生成5个[0, 10)之间的整数
array_rand = np. random. randint( low= 0 , high= 10 , size= 5 )
4.4 np.random.normal(loc, scale, size):随机生成5个满足正态分布的浮点数
array_rand = np. random. normal( loc= 0.0 , scale= 1.0 , size= 5 )
4.5 np.random.randn(shape):随机生成5个满足标准正态分布的浮点数
array_rand = np. random. randn( 2 , 3 )
4.6 np.random.shuffle(x):对数组或列表进行乱序处理 M = np. arange( start= 0 , stop= 5 , step= 1 )
np. random. seed( 2021 )
np. random. shuffle( M)
print ( M)
5. numpy数组的常用属性和常用方法
5.1 np.reshape(a, newshape):改变数组形状 M = np. arange( start= 1 , stop= 7 , step= 1 )
M_23 = np. reshape( a= M, newshape= ( 2 , 3 ) )
M_23 = np. reshape( a= M, newshape= ( 2 , - 1 ) )
M_23 = np. reshape( a= M, newshape= ( - 1 , 3 ) )
5.2 M.shape:查看数组形状 M = np. arange( start= 1 , stop= 7 , step= 1 ) . reshape( 2 , - 1 )
print ( M. shape)
5.3 M.size:查看数组元素总数 M = np. arange( start= 1 , stop= 7 , step= 1 ) . reshape( 2 , - 1 )
print ( M. size)
5.4 M.ndim:查看数组维度 M = np. arange( start= 1 , stop= 7 , step= 1 ) . reshape( 2 , - 1 )
print ( M. ndim)
6. numpy数组的访问与切片
6.1 数组访问 M = np. arange( start= 1 , stop= 21 , step= 1 ) . reshape( 4 , - 1 )
'''
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]]
'''
print ( M[ 2 ] [ 1 ] )
print ( M[ 1 ] )
print ( M[ : , 2 ] )
6.2 数组切片 M = np. arange( start= 1 , stop= 21 , step= 1 ) . reshape( 4 , - 1 )
'''
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]]
'''
print ( M[ : 2 , : ] )
'''
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
'''
print ( M[ 1 : 3 , 1 : 3 ] )
'''
[[ 7 8]
[12 13]]
'''
print ( M[ : , : : 2 ] )
'''
[[ 1 3 5]
[ 6 8 10]
[11 13 15]
[16 18 20]]
'''
col_index = [ 0 , 1 , 4 ]
print ( M[ : , col_index] )
'''
[[ 1 2 5]
[ 6 7 10]
[11 12 15]
[16 17 20]]
'''
7. numpy数组的连接与划分
7.1 np.concatenate(list, axis):数组的连接 A = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
B = np. array( [ [ 5 , 6 ] , [ 7 , 8 ] ] )
C = np. array( [ [ 9 , 10 ] , [ 11 , 12 ] ] )
X = np. concatenate( [ A, B, C] )
print ( X. shape)
Y = np. concatenate( [ A, B, C] , axis= 1 )
print ( Y. shape)
7.2 np.vstack(list) | np.hstack(list):数组在两个方向上的堆叠 A = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
B = np. array( [ [ 5 , 6 ] , [ 7 , 8 ] ] )
C = np. array( [ [ 9 , 10 ] , [ 11 , 12 ] ] )
X = np. vstack( [ A, B, C] )
print ( X. shape)
Y = np. hstack( [ A, B, C] )
print ( Y. shape)
7.3 np.split(arr, position_list, axis):数组的拆分 M = np. array( [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] )
X, Y, Z = np. split( M, [ 1 , 3 ] )
print ( X. shape, Y. shape, Z. shape)
A, B = np. split( M, 2 , axis= 1 )
print ( A. shape, B. shape)
7.4 np.vsplit(arr, list) | np.hsplit(arr, list):数组在两个方向上的拆分 M = np. array( [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] )
x1, x2, x3 = np. vsplit( M, [ 1 , 2 ] )
print ( x1. shape, x2. shape, x3. shape)
y1, y2 = np. hsplit( M, 2 )
print ( y1. shape, y2. shape)
8. numpy数组的运算
8.1 基本运算 M = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
print ( M + 1 )
print ( 0 - M)
print ( 1.5 * M)
print ( M / 2 )
print ( M // 3 )
print ( M % 3 )
print ( M ** 2 )
y = np. linspace( 0 , 3.1415926535 , num= 7 )
print ( np. sin( y) )
print ( np. cos( y) )
print ( np. exp( M) )
print ( np. power( M, 3 ) )
8.2 矩阵运算 A = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
B = np. array( [ [ 5 , 6 ] , [ 7 , 8 ] ] )
print ( A + B)
print ( A * B)
print ( A. dot( B) )
print ( np. dot( A, B) )
print ( A. T)
print ( np. transpose( A) )
print ( np. linalg. inv( A) )
print ( np. linalg. inv( A) . dot( A) )
C = np. zeros( shape= ( 2 , 3 ) )
print ( np. linalg. pinv( C) )
8.3 聚合操作 M = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
print ( np. sum ( M) )
print ( np. sum ( M, axis= 0 ) )
print ( np. sum ( M, axis= 1 ) )
print ( np. prod( M) )
print ( np. mean( M) )
print ( np. median( M) )
print ( np. percentile( a= M, q= 50 ) )
for percent in [ 0 , 25 , 50 , 75 , 100 ] :
print ( percent, np. percentile( M, percent) )
print ( np. var( M) )
print ( np. std( M) )
9. 其他部分
9.1 arg索引 M = np. array( [ 4 , 7 , 1 , 3 , 8 , 0 , 6 , 2 , 9 , 5 ] )
print ( np. argmin( M) )
print ( np. argmax( M) )
print ( np. argsort( M) )
9.2 Fancy Indexing X = np. arange( 16 )
index = [ 3 , 5 , 8 ]
print ( X[ index] )
Y = X. reshape( 4 , - 1 )
rows = [ 0 , 2 , 3 ]
cols = [ 1 , 1 , 3 ]
print ( Y[ rows, cols] )
9.3 数组的比较 X = np. arange( 16 ) . reshape( 4 , - 1 )
print ( np. sum ( X > 3 ) )
print ( np. count_nonzero( X) )
print ( np. any ( X == 0 ) )
print ( np. any ( X == 16 ) )
print ( np. any ( X < 0 ) )
print ( np. any ( X > 10 ) )
print ( np. all ( X > 0 ) )
print ( np. all ( X >= 0 ) )