import numpy as np arr=np.array([1,2,3,4,5]) arr array([1, 2, 3, 4, 5]) arr=np.
array(['a','b',3,4,5]) arr array(['a', 'b', '3', '4', '5'], dtype='<U1') list_1=
[i for i in range(100)] np.array(list_1) array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
<>第一部分

<>1数组内部函数创建
#1.全1数组 arr_1=np.ones(5) arr_1 array([1., 1., 1., 1., 1.]) #2.全0数组 arr_0=np.
zeros(5) arr_0 array([0., 0., 0., 0., 0.]) #3.全为fill_value s的数组 arr_full=np.full
(shape=[2,3],fill_value=123) arr_full array([[123, 123, 123], [123, 123, 123]])
#4.等查数列(步长跳跃) arr_arange=np.arange(start=0,stop=10,step=3) arr_arange array([0,
3, 6, 9]) #5.等分位函数arr_linspace=np.linspace(start=0,stop=9,num=11)arr_linspace
array([0. , 0.9, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9. ]) #6.随机整数
arr_random_int=np.random.randint(0,100,size=10)arr_random_int array([35, 83,
71, 28, 30, 15, 15, 56, 13, 56])
#7.正态分布数arr_random_θ=np.random.randn(20)arr_random_θ array([-0.26125824,
-1.10471905, 0.47196747, -0.45830774, -0.77667846, -0.19228595, -0.88741877,
0.78935227, 0.38761409, 0.11356884, -0.79957499, -0.42100158, 1.31760935,
0.83588336, 0.70025361, 0.96548828, -0.05750231, 1.00665188, -0.58256791,
-1.06221666]) #8.随机数float
01范围arr_random_float=np.random.random([2,3])arr_random_float array([[0.4602639
, 0.723614 , 0.75192543], [0.1464745 , 0.96532921, 0.86107633]]) #9.随机数float
任意范围 arr_random_float_2=np.random.uniform(5,10,5) arr_random_float_2
array([6.41475055, 7.90246005, 6.81719872, 9.80708543, 7.85627567])
<>2.查看操作
#1.2.1 轴数,维度 arr=np.random.randint(0,100,size=(4,2)) arr.ndim 2 #1.2.2
形状尺寸arr=np.random.randint(0,100,size=(4,2,5))arr.shape (4, 2, 5) #1.2.3数组元素个数
sizearr=np.random.randint(0,100,size=(3,4,5))arr.size 60
#1.2.4数据类型arr=np.random.randint(0,100,size=(3,4,5),dtype='int64')arr.dtype
dtype('int64') #1.2.5数组中每个元素大小 arr=np.random.randint(0,100,size=(3,4,5),dtype=
'int32') arr.itemsize 4
<>3.文件io操作
#1.3.1保存数组 x=np.random.randn(5) y=np.arange(0,10,1) z=np.linspace(0,100,80) np.
save("x_arr",x)#默认 .npy np.savez("all_arr",xarr=x,yarr=y,zarr=z) #1.3.2读取保存数组 np
.load("x_arr.npy") array([-2.48913545, 0.00585772, 2.21861779, 0.48855107,
-0.90451461]) np.load("all_arr.npz")['yarr'] array([0, 1, 2, 3, 4, 5, 6, 7, 8,
9]) #1.3.3读取保存数组arr =np.random.randint(0,10,size=(3,4)) np.savetxt("arr.csv",arr
,delimiter=",")np.loadtxt("arr.csv",delimiter=",",dtype=np.int32) array([[5, 3,
0, 2], [2, 6, 0, 5], [4, 6, 7, 5]])
<>第二部分 数据类型

<>ndarray的数据类型:

* int: int8、uint8、int16、int32、int64
* float: float16、float32、float64
* str # 2.1创建指定数据类型np.array(np.arange(1,10,2),dtype='int8') array([1, 3, 5,
7, 9], dtype=int8) # 2.2转换时指定数据类型arr
=list(range(20))np.asarray(arr,dtype='float32') array([ 0., 1., 2., 3., 4., 5.,
6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.],
dtype=float32) # 2.3转换时指定数据类型 arr=np.random.randint(0,10,5,dtype='int32') arr.
dtype dtype('int32') arr=arr.astype('float64') arr.dtype dtype('float64')
<>第三部分 数组运算

<>3.1加减乘除幂运算
arr1=np.array(list(range(1,5))) arr2=np.array(list(range(2,6))) arr1-arr2
array([-1, -1, -1, -1]) arr1+arr2 array([3, 5, 7, 9]) arr2/arr1 array([2. , 1.5
, 1.33333333, 1.25 ]) arr2*arr1 array([ 2, 6, 12, 20]) arr2**arr1 array([ 2, 9,
64, 625], dtype=int32)
<>3.2逻辑运算 > 、<、=、>=、<=
arr1<5 array([ True, True, True, True]) arr1>5 array([False, False, False,
False]) arr1==4 array([False, False, False, True]) arr1==arr2 array([False,
False, False, False])
<>3.3数组与标量计算
arr1=np.array([5,5,5,5]) arr2=np.array(list(range(5))) arr1+=5 arr1-=5 arr1*=5
# arr1/=5 没有除等
<>第四部分 复制和视图

<>4.1 拷贝复制 完全没有复制

<>在操作数组时,有时会将其数据复制到新数组中,有时不复制。

<>对于初学者来说,这通常会引起混乱。有以下三种情况
a=np.random.randint(0,100,[4,5])b=a a is b # 返回True a和b是两个不同名字对应同⼀个内存对象 True b[
0,0]=1024 #a与b位置相同,同时被修改print(a[0,0],b[0,0]) 1024 1024 display(a,b)
array([[1024, 31, 42, 19, 68], [ 69, 67, 22, 7, 93], [ 77, 62, 20, 15, 25], [
4, 46, 88, 67, 80]]) array([[1024, 31, 42, 19, 68], [ 69, 67, 22, 7, 93], [ 77,
62, 20, 15, 25], [ 4, 46, 88, 67, 80]])
<>4.2 查看或浅拷贝
a=np.random.randint(0,100,[4,5]) b=a.view() # 使⽤a中的数据创建⼀个新数组对象 a is b# 返回False
a和b是两个不同名字对应同⼀个内存对象 False b.base is a # 返回False a和b是两个不同名字对应同⼀个内存对象 True b.flags
.owndata # 返回False b中的数据不是其⾃⼰的 False a.flags.owndata # 返回True a中的数据是其⾃⼰的 True a[
0,0]=1024 b[0,0] 1024 display(a,b) array([[1024, 86, 36, 96, 66], [ 17, 68, 11,
88, 26], [ 89, 39, 14, 21, 90], [ 49, 82, 46, 98, 3]]) array([[1024, 86, 36,
96, 66], [ 17, 68, 11, 88, 26], [ 89, 39, 14, 21, 90], [ 49, 82, 46, 98, 3]])
<>4.3 深拷贝
a=np.random.randint(0,100,[4,5])b=a.copy() b is a False b.base is a False b.
flags.owndata True a.flags.owndata True b[0,0]=1024 a[0,0] 52 display(a,b)
array([[52, 60, 16, 93, 64], [67, 36, 75, 82, 42], [86, 58, 17, 54, 22], [72,
14, 85, 92, 17]]) array([[1024, 60, 16, 93, 64], [ 67, 36, 75, 82, 42], [ 86,
58, 17, 54, 22], [ 72, 14, 85, 92, 17]])
*
copy应该在不再需要原来的数组情况下,切⽚后调⽤。例如,假设a是⼀个巨⼤的中间结果,⽽最终结果b仅包含的⼀⼩部分a,则在b使⽤切⽚进⾏构造时应制作⼀个深拷⻉:
a=np.arange(1e8)b=a[::1000000]del ab.shape (100,)
<>第五部分 索引 切片 迭代

<>5.1基本索引与切片
## 5.1基本索引与切片arr=np.arange(10)arr array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr[5]
5 arr[5:8] array([5, 6, 7]) arr[5::2] array([5, 7, 9]) arr[::2] array([0, 2, 4,
6, 8]) arr[:7:2] array([0, 2, 4, 6]) arr[::-2] # 倒序取 array([9, 7, 5, 3, 1]) arr[
::-1] array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) arr[0:3]=999 arr array([999, 999,
999, 3, 4, 5, 6, 7, 8, 9]) temp=arr[5:8] temp[1]=1034 arr array([ 999, 999,
999, 3, 4, 5, 1034, 7, 8, 9])
<>5.2高维索引与切片

* 对于⼆维数组或者⾼维数组,我们可以按照之前的知识来索引,当然也可以传⼊⼀个以逗号隔开的索引
* 列表来选区单个或多个元素 arr2d=np.array(list(range(12)))arr2d.shape=(3,4)arr2d array([[
0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) arr2d[0,-1]# 第一位表示行,二位表示列 3 arr2d[
0,2] 2 arr2d[:2,-2:] array([[2, 3], [6, 7]]) arr2d[:2,:-2] array([[0, 1], [4,
5]]) arr2d[:2,2:] array([[2, 3], [6, 7]]) arr2d[:2,:2] array([[0, 1], [4, 5]])
<>5.3 花式索引和索引技巧

* 整数数组进⾏索引即花式索引,其和切⽚不⼀样,它总是将数据复制到新数组中 # 一维索引arr1=np.arange(1,11)arr1 array([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) arr2=arr1[[1,3,3,5,7,7,7]]arr2 array([2, 4, 4,
6, 8, 8, 8]) arr2[-1]=1024#不影响arr1arr2 array([ 2, 4, 4, 6, 8, 8, 1024]) arr1
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # 二维索引arr2d=np.arange(20)arr2d array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) arr2d.
shape=(4,5)arr2d array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13,
14], [15, 16, 17, 18, 19]]) arr2d[[1,3]]#索引编号第1行和第第3行 array([[ 5, 6, 7, 8, 9],
[15, 16, 17, 18, 19]]) arr2d[[0,2]]#索引编号第0行和第第2行 array([[ 0, 1, 2, 3, 4], [10,
11, 12, 13, 14]]) arr2d[([1,3],[2,4])]#索引编号第1行和第3行 与 第2列和第4列 的交叉点
元素,相当于坐标arr2d[3,4] 和arr2d[1,2] array([ 7, 19]) arr2d[1,2] 7 arr2d[3,4] 19 #
选择输出一个区域arr2d[np.ix_([1,3,3,3],[2,4,4])] array([[ 7, 9, 9], [17, 19, 19], [17,
19, 19], [17, 19, 19]]) #相当于arr2d[[1,3,3,3]][:,[2,4,4]] array([[ 7, 9, 9], [17,
19, 19], [17, 19, 19], [17, 19, 19]]) #也相当于arr2d_tmp=arr2d[[1,3,3,3]]#
arr2d_tmparr2d_tmp[:,[2,4,4]] array([[ 7, 9, 9], [17, 19, 19], [17, 19, 19],
[17, 19, 19]]) # ix_()函数可⽤于组合不同的向量 # 第⼀个列表存的是待提取元素的⾏标,第⼆个列表存的是待提取元素的列
<>5.4 boolean值索引(布尔索引)
# names = #
np.array(['softpo','Brandon','Will','Michael','Will','Ella','Daniel','softpo','
# Will','Brandon']) # cond1 = names == 'Will' # cond1 # # 输出array([False,
False, True, False, True, False, False, False, True, # False]) # names[cond1] #
array(['Will', 'Will', 'Will'], dtype='<U7') # arr =
np.random.randint(0,100,size = (10,8)) # 0~100随机数 # cond2 = arr > 90 # #
找到所有⼤于90的索引,返回boolean类型的数组 shape(10,8),⼤于返回True,否则False # arr[cond2] #
返回数据全部是⼤于90的 names =np.array(['softpo','Brandon','Will','Michael','Will','Ella',
'Daniel','softpo','Will','Brandon']) names array(['softpo', 'Brandon', 'Will',
'Michael', 'Will', 'Ella', 'Daniel', 'softpo', 'Will', 'Brandon'], dtype='<U7')
cond1= names == 'Will'cond1 array([False, False, True, False, True, False,
False, False, True, False]) names[cond1]#索引为True 的对应元素 array(['Will', 'Will',
'Will'], dtype='<U7') arr=np.random.randint(0,100,size=[10,8])arr array([[81,
59, 92, 17, 87, 49, 89, 45], [ 3, 1, 12, 17, 54, 68, 11, 11], [35, 86, 20, 44,
38, 80, 39, 52], [80, 5, 50, 16, 20, 78, 94, 91], [90, 68, 25, 35, 27, 67, 43,
44], [23, 54, 45, 75, 14, 10, 21, 72], [84, 26, 63, 50, 56, 3, 67, 90], [98,
15, 25, 96, 58, 44, 9, 19], [35, 75, 46, 79, 8, 94, 83, 88], [ 8, 52, 68, 69,
72, 71, 31, 88]]) arr_bool=arr>90arr_bool array([[False, False, True, False,
False, False, False, False], [False, False, False, False, False, False, False,
False], [False, False, False, False, False, False, False, False], [False,
False, False, False, False, False, True, True], [False, False, False, False,
False, False, False, False], [False, False, False, False, False, False, False,
False], [False, False, False, False, False, False, False, False], [ True,
False, False, True, False, False, False, False], [False, False, False, False,
False, True, False, False], [False, False, False, False, False, False, False,
False]]) arr[arr_bool] array([92, 94, 91, 98, 96, 94])
<>第六部分 形状操作

<>6.1 数组变形
arr1=np.random.randint(1,100,size=(3,4,5))arr1 array([[[68, 40, 48, 91, 49],
[96, 37, 30, 75, 78], [79, 74, 75, 26, 51], [15, 45, 20, 59, 54]], [[73, 5, 71,
88, 62], [16, 6, 36, 49, 89], [40, 7, 42, 34, 97], [62, 87, 82, 70, 96]], [[94,
85, 62, 11, 26], [ 2, 76, 43, 9, 40], [21, 23, 99, 36, 78], [69, 71, 30, 11,
98]]]) arr1.reshape(12,5) # 修改形状 array([[68, 40, 48, 91, 49], [96, 37, 30, 75,
78], [79, 74, 75, 26, 51], [15, 45, 20, 59, 54], [73, 5, 71, 88, 62], [16, 6,
36, 49, 89], [40, 7, 42, 34, 97], [62, 87, 82, 70, 96], [94, 85, 62, 11, 26], [
2, 76, 43, 9, 40], [21, 23, 99, 36, 78], [69, 71, 30, 11, 98]]) arr1.reshape(-1,
5)# 为负数的值会被自动赋值相应的行或列数 array([[68, 40, 48, 91, 49], [96, 37, 30, 75, 78], [79,
74, 75, 26, 51], [15, 45, 20, 59, 54], [73, 5, 71, 88, 62], [16, 6, 36, 49,
89], [40, 7, 42, 34, 97], [62, 87, 82, 70, 96], [94, 85, 62, 11, 26], [ 2, 76,
43, 9, 40], [21, 23, 99, 36, 78], [69, 71, 30, 11, 98]]) arr1.reshape(6,-1)#
为负数的值会被自动赋值相应的行或列数 array([[68, 40, 48, 91, 49, 96, 37, 30, 75, 78], [79, 74,
75, 26, 51, 15, 45, 20, 59, 54], [73, 5, 71, 88, 62, 16, 6, 36, 49, 89], [40,
7, 42, 34, 97, 62, 87, 82, 70, 96], [94, 85, 62, 11, 26, 2, 76, 43, 9, 40],
[21, 23, 99, 36, 78, 69, 71, 30, 11, 98]])
<>6.2 数组转换
arr1=np.random.randint(1,100,size=(3,5))arr1 array([[15, 31, 44, 13, 74], [54,
82, 18, 33, 56], [76, 84, 71, 23, 29]]) arr1.T array([[15, 54, 76], [31, 82,
84], [44, 18, 71], [13, 33, 23], [74, 56, 29]]) arr2=np.random.randint(1,100,
size=(3,4,5,6))arr2 array([[[[89, 5, 9, 23, 15, 45], [83, 72, 90, 98, 14, 54],
[41, 16, 31, 28, 25, 28], [ 6, 48, 8, 62, 69, 4], [36, 1, 63, 52, 40, 17]],
[[22, 19, 56, 69, 75, 30], [43, 7, 3, 76, 15, 79], [13, 45, 78, 19, 81, 77],
[60, 51, 81, 40, 48, 65], [99, 79, 74, 52, 4, 27]], [[73, 12, 54, 42, 67, 43],
[81, 78, 35, 71, 80, 66], [90, 43, 2, 24, 55, 3], [98, 56, 76, 15, 65, 5], [64,
85, 92, 22, 39, 79]], [[55, 24, 54, 6, 35, 42], [88, 84, 46, 15, 30, 83], [ 9,
62, 55, 81, 65, 75], [14, 99, 34, 75, 63, 53], [19, 39, 61, 88, 95, 6]]],

[[[57, 37, 14, 32, 79, 88], [ 1, 32, 80, 8, 52, 86], [42, 38, 11, 75, 56,
22], [70, 43, 24, 14, 77, 26], [75, 68, 1, 94, 98, 88]], [[99, 73, 68, 14, 48,
5], [29, 52, 72, 99, 10, 16], [21, 86, 85, 15, 29, 43], [87, 52, 83, 6, 59,
83], [51, 61, 67, 7, 14, 50]], [[ 4, 77, 14, 45, 13, 9], [58, 6, 24, 59, 88,
92], [11, 72, 35, 60, 94, 37], [59, 94, 67, 92, 75, 84], [77, 23, 39, 77, 67,
14]], [[63, 8, 16, 69, 16, 74], [34, 61, 64, 72, 15, 15], [33, 62, 49, 37, 64,
84], [94, 22, 9, 4, 56, 48], [53, 16, 14, 61, 56, 63]]],

[[[28, 90, 96, 64, 61, 15], [41, 99, 61, 83, 37, 14], [97, 92, 99, 16, 72,
46], [42, 30, 83, 83, 15, 31], [52, 98, 16, 98, 90, 31]], [[37, 35, 37, 66, 97,
6], [18, 29, 14, 26, 96, 11], [78, 40, 75, 41, 17, 15], [77, 82, 29, 87, 65,
30], [56, 69, 40, 90, 67, 2]], [[14, 43, 51, 60, 52, 91], [66, 45, 11, 74, 11,
2], [25, 44, 72, 6, 13, 22], [63, 24, 90, 86, 43, 3], [78, 29, 36, 18, 94,
10]], [[56, 64, 86, 32, 76, 90], [61, 42, 25, 98, 3, 32], [ 6, 51, 25, 60, 36,
68], [19, 24, 51, 20, 35, 27], [22, 21, 85, 17, 82, 30]]]]) arr3=arr2.T arr3.
shape (6, 5, 4, 3) arr2.shape (3, 4, 5, 6) # transpose改变数组维度
对原数组形状进行排序,然后利用索引调换顺序arr4=np.transpose(arr2,axes=(3,0,2,1)) arr4.shape (6, 3,
5, 4)
<>6.3 数组堆叠
arr1=np.array([range(1,4)])arr2=np.array([range(4,7)]) arr1 array([[1, 2, 3]])
np.concatenate([arr1,arr2],axis=0) # 记忆 0代表在下方合并相当于union,1代表在右侧合并相当于right join
array([[1, 2, 3], [4, 5, 6]]) np.concatenate([arr1,arr2],axis=1) # 记忆
0代表在下方合并相当于union,1代表在右侧合并相当于right join array([[1, 2, 3, 4, 5, 6]]) np.hstack((
arr1,arr2)) #水平方向叠加 array([[1, 2, 3, 4, 5, 6]]) np.vstack((arr1,arr2)) # 竖直方向叠加
array([[1, 2, 3], [4, 5, 6]])
<>6.4 数组拆分split
import numpy as pd arr = np.random.randint(0,10,size=(6,5))np.split(arr,
indices_or_sections=2,axis=0) [array([[6, 7, 3, 3, 2], [1, 5, 8, 5, 4], [6, 0,
7, 6, 9]]), array([[2, 1, 2, 3, 3], [0, 4, 5, 4, 7], [9, 9, 2, 1, 4]])] np.split
(arr,indices_or_sections=2,axis=0)# 竖向分割 确保竖向(行数)为偶数 [array([[6, 7, 3, 3, 2],
[1, 5, 8, 5, 4], [6, 0, 7, 6, 9]]), array([[2, 1, 2, 3, 3], [0, 4, 5, 4, 7],
[9, 9, 2, 1, 4]])] np.split(arr,indices_or_sections=[2,],axis=1)# 横向切割向分割
确保横向(列数)为偶数,否则传入 indices_or_sections=[2,],2为第二列,留空时分成两份 [array([[6, 7], [1, 5],
[6, 0], [2, 1], [0, 4], [9, 9]]), array([[3, 3, 2], [8, 5, 4], [7, 6, 9], [2,
3, 3], [5, 4, 7], [2, 1, 4]])] np.split(arr,indices_or_sections=[2,3],axis=1)#
横向切割向分割 确保横向(列数)为偶数,否则可传入 indices_or_sections=[2,3],2为第二列,3为第三列,#
于是数组被分为分成1,2列,3列,至最后一列 [array([[6, 7], [1, 5], [6, 0], [2, 1], [0, 4], [9,
9]]), array([[3], [8], [7], [2], [5], [2]]), array([[3, 2], [5, 4], [6, 9], [3,
3], [4, 7], [1, 4]])] arr array([[6, 7, 3, 3, 2], [1, 5, 8, 5, 4], [6, 0, 7, 6,
9], [2, 1, 2, 3, 3], [0, 4, 5, 4, 7], [9, 9, 2, 1, 4]]) np.vsplit(arr,
indices_or_sections=3)# 在竖直方向被平均分成三份 [array([[6, 7, 3, 3, 2], [1, 5, 8, 5,
4]]), array([[6, 0, 7, 6, 9], [2, 1, 2, 3, 3]]), array([[0, 4, 5, 4, 7], [9, 9,
2, 1, 4]])] np.hsplit(arr,indices_or_sections=[1,4])#
在水平方向以索引1,4为断点分割成三份(此处索引标注从1开始,不是从0开始) [array([[6], [1], [6], [2], [0], [9]]),
array([[7, 3, 3], [5, 8, 5], [0, 7, 6], [1, 2, 3], [4, 5, 4], [9, 2, 1]]),
array([[2], [4], [9], [3], [7], [4]])]
<>第七部分 广播机制

* 当两个数组的形状并不相同的时候,我们可以通过扩展数组的方法来实现加减乘(除)等操作,这种机制叫广播机制
<>7.1排序规则np.sort(array,[axis=1,0])
import numpy as np arr1=np.sort(np.array([1,2,3,4]*3)).reshape(4,3) arr2=np.
sort(np.array([1,2,3,4]*3)).reshape(4,3) arr2 array([[1, 1, 1], [2, 2, 2], [3,
3, 3], [4, 4, 4]]) #2 np.sort(arr1,axis=0) array([[1, 1, 1], [2, 2, 2], [3, 3,
3], [4, 4, 4]]) arr1 array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) arr3=np
.sort(arr1,axis=1) arr3 array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) np.
sort(arr3,axis=0) array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) arr1
array([[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]]) ## 默认axis = 1 按行排序
水平方向从小到大排列, arr2=np.sort(np.random.randint(1,10,(4,3))) arr2 ##竖直方向从小到大排列, np.
sort(arr2,axis=0) ##水平方向从小到大排列, np.sort(arr2,axis=1) array([[1, 5, 8], [3, 8,
9], [6, 8, 9], [1, 1, 2]])
<>7.2一维数组广播
arr1=np.sort(np.array([1,2,3,4]*3)).reshape(4,3) arr2=np.array([1,2,3]) arr3=
arr1+arr3 arr3 array([[2, 2, 2], [4, 4, 4], [6, 6, 6], [8, 8, 8]])

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A96rZFV5-1638201973086)(attachment:image.png)]

<>7.3二维数组广播
arr1=np.sort(np.array([0,1,2,3,]*3)).reshape(4,3) arr2=np.array([[1],[2],[3],[4
]]) arr3=arr1+arr2 arr3 array([[1, 1, 1], [3, 3, 3], [5, 5, 5], [7, 7, 7]])

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OVQABGWd-1638201973089)(attachment:image.png)]

<>7.4三维数组⼴播
import numpy as np arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2)
#shape(3,4,2) arr2 = np.array([0,1,2,3,4,5,6,7]).reshape(4,2) #shape(4,2) arr3 =
arr1+ arr2 # arr2数组在0维上复制3份 shape(3,4,2) arr3 array([[[ 0, 2], [ 4, 6], [ 8,
10], [12, 14]], [[ 0, 2], [ 4, 6], [ 8, 10], [12, 14]], [[ 0, 2], [ 4, 6], [ 8,
10], [12, 14]]]) arr1 array([[[0, 1], [2, 3], [4, 5], [6, 7]], [[0, 1], [2, 3],
[4, 5], [6, 7]], [[0, 1], [2, 3], [4, 5], [6, 7]]]) arr2 array([[0, 1], [2, 3],
[4, 5], [6, 7]])

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Gt2kj621-1638201973092)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EYQjkrZn-1638201973094)(attachment:image.png)]
# 维度只能从z轴方向运算广播 import numpy as np arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape
(3,4,2) #shape(3,4,2) arr2 = np.arange(0,12).reshape(3,4) #shape(,2) # arr3 =
arr1 + arr2 # arr2数组在0维上复制3份 shape(3,4,2) # arr3
<>第八部分 通用函数

<>8.1 通用函数:元素级数字函数

*
abs、sqrt、square、exp、log、sin、cos、tan,maxinmum、minimum、all、any、inner、clip、round、trace、ceil、floor
import numpy as np arr=np.arange(1,10) np.sqrt(arr)# 开平方 np.square(arr)#平方 np.
clip(arr,2,9)## 盖帽法 x=np.array([1,5,2,9,3,6,8]) y=np.array([2,4,3,7,1,9,0]) np.
maximum(x,y)# 返回两组数据的同位置最大值 arr2=np.arange(0,25).reshape(5,5) np.inner(arr2[0],
arr2)# 返回一维数组向量内积 print("数组最大值:%s"%(np.max(arr))) print("数组最小值:%s"%(np.min(arr))
) print("以e为基数求e的数组次方%s"%np.exp(arr)) print("以10为低求数组对数%s"%np.log(arr)) print(
"开方%s"%np.sqrt(arr)) print("平均值%s"%np.mean(arr)) print("标准差%s"%np.std(arr))
print("极小值%s"%np.argmin(arr)) print("极大值%s"%np.argmax(arr)) print("判断极限值:%s"%np.
isinf(arr)) 数组最大值:9 数组最小值:1 以e为基数求e的数组次方[2.71828183e+00 7.38905610e+00
2.00855369e+01 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03
2.98095799e+03 8.10308393e+03] 以10为低求数组对数[0. 0.69314718 1.09861229 1.38629436
1.60943791 1.79175947 1.94591015 2.07944154 2.19722458] 开方[1. 1.41421356
1.73205081 2. 2.23606798 2.44948974 2.64575131 2.82842712 3. ] 平均值5.0
标准差2.581988897471611 极小值0 极大值8 判断极限值:[False False False False False False False
False False]
<>8.2 where函数
import numpy as np arr1=np.array([1,2,5,7,9]) arr2 = np.array([2,4,6,8,10])
bool_v=np.array([True,False,True,True,True]) np.where(bool_v,arr1,arr2) np.where
(arr1<5,arr1,-16) array([ 1, 2, -16, -16, -16])
<>8.3排序方法

* arr.sort()、np.sort()、arr.argsort() import numpy as np arr =np.array([9,3,11,
6,17,5,4,15,1]) arr.sort()# 直接改变原数组 np.sort(arr)# 返回深拷贝结果 array([ 1, 3, 4, 5,
6, 9, 11, 15, 17]) arr =np.array([9,3,11,6,17,5,4,15,1]) arr.argsort()#
返回从小到大的排序索引 array([8, 1, 6, 5, 3, 0, 2, 7, 4], dtype=int64)
<>8.4集合运算函数
a=np.arange(2,10,2) b=np.arange(3,7) np.intersect1d(a,b)#交集 np.union1d(a,b)#并集
np.setdiff1d(a,b)# 差集 a有b无的元素 array([2, 8])
<>8.5集合运算函数
import numpy as np arr1=np.arange(0,50) arr1.min() arr1.max() np.argmax(arr1)
#返回最大值索引 np.argwhere(arr1>20)# 返回值符合条件(大于20)的索引 np.cumsum(arr1)# 累加计算 arr2=np.
arange(0,50).reshape(5,10) arr2.mean(axis=0)# 计算列的平均值 arr2.mean(axis=1)#计算行的平均值
np.cov(arr2,rowvar=True)# 协方差矩阵 np.corrcoef(arr2,rowvar=True)# 相关系数 array([[1.,
1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1.,
1.], [1., 1., 1., 1., 1.]]) arr2.mean(axis=0) array([20., 21., 22., 23., 24.,
25., 26., 27., 28., 29.]) np.cov(arr2,rowvar=True)# array([[9.16666667,
9.16666667, 9.16666667, 9.16666667, 9.16666667], [9.16666667, 9.16666667,
9.16666667, 9.16666667, 9.16666667], [9.16666667, 9.16666667, 9.16666667,
9.16666667, 9.16666667], [9.16666667, 9.16666667, 9.16666667, 9.16666667,
9.16666667], [9.16666667, 9.16666667, 9.16666667, 9.16666667, 9.16666667]])
<>9 线性代数
a=np.array([[4,2,3],[1,3,1]]) b=np.array([[2,7],[-5,-7],[9,3]]) a.dot(b)#矩阵运算
a的最后一维要求和b的第一维相等 np.dot(a,b)# a@b #缩写 array([[ 25, 23], [ -4, -11]]) #矩阵的其他运算
from numpy.linalg import inv,det,eig,qr,svd a=np.array([[1,2,3], [2,3,4], [4,5,8
] ]) b=inv(a) a.dot(b) array([[ 1.00000000e+00, 2.22044605e-16,
-5.55111512e-17], [ 4.44089210e-16, 1.00000000e+00, 2.22044605e-16], [
0.00000000e+00, -1.77635684e-15, 1.00000000e+00]])

技术
今日推荐
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信