51. 创建一个 5x5 的二维数组,其中边界值为1,其余值为0
1 '''考察numpy中二维数组的切片和利用np.ones()函数生成数组'''
2 importnumpy as np3 Z = np.ones((5,5))4 Z[1:4,1:4] = 0#Z[1:-1,1:-1] =0也可以
5 Z
array([[1., 1., 1., 1., 1.],
[1., 0., 0., 0., 1.],
[1., 0., 0., 0., 1.],
[1., 0., 0., 0., 1.],
[1., 1., 1., 1., 1.]])
52. 使用数字 0 将一个全为 1 的 5x5 二维数组包围
1 '''考察np.pad()填充阵列'''
2 importnumpy as np3 Z = np.ones((5,5))4 Z = np.pad(Z,pad_width=1,mode='constant',constant_values=0)5 Z
array([[0., 0., 0., 0., 0., 0., 0.],
[0.,1., 1., 1., 1., 1., 0.],
[0.,1., 1., 1., 1., 1., 0.],
[0.,1., 1., 1., 1., 1., 0.],
[0.,1., 1., 1., 1., 1., 0.],
[0.,1., 1., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 0., 0.]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.pad.html?highlight=pad#numpy.pad
53. 创建一个 5x5 的二维数组,并设置值 1, 2, 3, 4 落在其对角线下方
1 '''考察np.diag()函数,构造对角线矩阵2 np.diag(v,k)'''
3 importnumpy as np4 Z = np.diag(1+np.arange(4),k=-1)#1+np.arange(4)生成矩阵[1,2,3,4]一维数组
5 Z
array([[0, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[0,2, 0, 0, 0],
[0, 0,3, 0, 0],
[0, 0, 0,4, 0]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.diag.html?highlight=diag#numpy.diag
54. 创建一个 10x10 的二维数组,并使得 1 和 0 沿对角线间隔放置
1 '''考察切片'''
2 importnumpy as np3 Z = np.zeros((10,10),dtype=int)4 Z[1::2,::2] = 1#先处理行,从第1行开始,跳过第0行
5 Z[::2,1::2] = 1#再处理列,从第0列开始
6 Z
array([[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0,1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0,1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0,1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0,1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]])
55. 创建一个 0-10 的一维数组,并将 (1, 9] 之间的数全部反转成负数
1 '''数组的索引及反转负数的写法'''
2 importnumpy as np3 Z = np.arange(11)4 Z[(1 < Z) & (Z <= 9)] *= -1
5 Z
array([ 0, 1, -2, -3, -4, -5, -6, -7, -8, -9, 10])
56. 找出两个一维数组中相同的元素
'''考察np.intersect1d()找到两个数组的交集'''impot numpy as np
Z1= np.random.randint(0,10,10)
Z2= np.random.randint(0,10,10)print("Z1:", Z1)print("Z2:", Z2)
np.intersect1d(Z1,Z2)
Z1: [5 2 7 6 7 8 1 5 2 4]
Z2: [4 9 4 2 2 9 2 5 1 4]
array([1, 2, 4, 5])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.intersect1d.html?highlight=intersect1d#numpy.intersect1d
57. 使用 NumPy 打印昨天、今天、明天的日期
1 '''Datetimes and Timedeltas'''
2 importnumpy as np3 yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')4 today = np.datetime64('today', 'D')5 tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')6 print("yesterday:", yesterday)7 print("today:", today)8 print("tomorrow:", tomorrow)
1 '''Datetimes and Timedeltas'''
2 importnumpy as np3 yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')4 today = np.datetime64('today', 'D')5 tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')6 print("yesterday:", yesterday)7 print("today:", today)8 print("tomorrow:", tomorrow)
1 '''Datetimes and Timedeltas'''
2 importnumpy as np3 yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')4 today = np.datetime64('today', 'D')5 tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')6 print("yesterday:", yesterday)7 print("today:", today)8 print("tomorrow:", tomorrow)
1 '''Datetimes and Timedeltas'''
2 importnumpy as np3 yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')4 today = np.datetime64('today', 'D')5 tomorrow = np.datetime64('today', 'D') + np.timedelta64(1, 'D')6 print("yesterday:", yesterday)7 print("today:", today)8 print("tomorrow:", tomorrow)
yesterday: 2019-10-10today:2019-10-11tomorrow:2019-10-12
具体用法:https://numpy.org/devdocs/reference/arrays.datetime.html?highlight=timedelta64
58. 使用五种不同的方法去提取一个随机数组的整数部分
1 '''提取数组整数部分的方法'''
2 importnumpy as np3 Z = np.random.uniform(0,10,10)4 print("原始值:", Z)5
6 print ("方法 1:", Z - Z%1)7 print ("方法 2:", np.floor(Z))8 #https://numpy.org/devdocs/reference/generated/numpy.floor.html?highlight=floor#numpy.floor
9 print ("方法 3:", np.ceil(Z)-1)#-1是因为这种方法往前取整
10 #https://numpy.org/devdocs/reference/generated/numpy.ceil.html?highlight=ceil#numpy.ceil
11 print ("方法 4:", Z.astype(int))12 print ("方法 5:", np.trunc(Z))13 #https://numpy.org/devdocs/reference/generated/numpy.trunc.html#numpy.trunc
59. 创建一个 5x5 的矩阵,其中每行的数值范围从 1 到 5
1 importnumpy as np2 Z = np.zeros((5,5))3 Z += np.arange(1,6)
60. 创建一个长度为 5 的等间隔一维数组,其值域范围从 0 到 1,但是不包括 0 和 1
1 '''np.linspace返回指定间隔的等间隔数字'''
2 importnumpy as np3 Z = np.linspace(0,1,6,endpoint=False)[1:]#0<=N<=1,num=6,endpoint=False:不#包含最后一个值,所以num=5;[1:]切片,数组不包含0
4 Z
具体用法:https://numpy.org/devdocs/reference/generated/numpy.linspace.html?highlight=linspace#numpy.linspace
61. 创建一个长度为10的随机一维数组,并将其按升序排序
1 '''sort()函数'''
2 importnumpy as np3 Z = np.random.random(10)4 Z.sort()5 Z
array([0.02100538, 0.02491363, 0.06035407, 0.33422597, 0.47044631,0.57816531, 0.74059169, 0.74421693, 0.85133402, 0.90469746])
62. 创建一个 3x3 的二维数组,并将列按升序排序
1 '''sort(axis=1)从1轴看去,升序排列'''
2 importnumpy as np3 Z = np.array([[7,4,3],[3,1,2],[4,2,6]])4 print("原始数组: \n", Z)5
6 Z.sort(axis=1)7 Z
原始数组:
[[7 4 3]
[3 1 2]
[4 2 6]]
array([[3, 4, 7],
[1, 2, 3],
[2, 4, 6]])
63. 创建一个长度为 5 的一维数组,并将其中最大值替换成 0
1 '''argmax()选取轴向方向最大值的索引'''
2 importnumpy as np3 Z = np.random.random(5)4 print("原数组:",Z)5 Z[Z.argmax()] = 0#Z.argmax()=3,也可以写成Z[np.argmax(Z)]=0
6 Z
原数组: [0.9726376 0.7217488 0.95154265 0.13562066 0.4923662]
array([0. ,0.7217488 , 0.95154265, 0.13562066, 0.4923662 ])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.argmax.html?highlight=argmax#numpy.argmax
64. 打印每个 NumPy 标量类型的最小值和最大值
1 importnumpy as np2 for dtype in[np.int8, np.int32, np.int64]:3 print("The minimum value of {}:".format(dtype), np.iinfo(dtype).min)4 print("The maximum value of {}:".format(dtype),np.iinfo(dtype).max)5 for dtype in[np.float32, np.float64]:6 print("The minimum value of {}:".format(dtype),np.finfo(dtype).min)7 print("The maximum value of {}:".format(dtype),np.finfo(dtype).max)
The minimum value of : -128The maximum value of: 127The minimum value of: -2147483648The maximum value of: 2147483647The minimum value of: -9223372036854775808The maximum value of: 9223372036854775807The minimum value of: -3.4028235e+38The maximum value of: 3.4028235e+38The minimum value of: -1.7976931348623157e+308The maximum value of: 1.7976931348623157e+308
65. 将 float32 转换为整型
1 '''np.astype()转换类型'''
2 importnumpy as np3 Z = np.arange(10, dtype=np.float32)4 print(Z)5
6 Z = Z.astype(np.int32, copy=False)#copy=False,返回输入数组
7 Z
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
array([0,1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)
具体用法:https://numpy.org/devdocs/reference/generated/numpy.ndarray.astype.html?highlight=astype#numpy.ndarray.astype
66. 将随机二维数组按照第 3 列从上到下进行升序排列
1 '''argsort()返回对数组排序的索引'''
2 importnumpy as np3 Z = np.random.randint(0,10,(5,5))4 print("排序前:\n",Z)5
6 Z[Z[:,2].argsort()#Z[np.argsort(Z[:,2])]
排序前:
[[3 4 7 8 5]
[7 4 7 3 6]
[8 6 7 7 4]
[4 8 6 8 5]
[6 6 8 3 2]]
array([[4, 8, 6, 8, 5],
[3, 4, 7, 8, 5],
[7, 4, 7, 3, 6],
[8, 6, 7, 7, 4],
[6, 6, 8, 3, 2]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.argsort.html?highlight=argsort#numpy.argsort
67. 从随机一维数组中找出距离给定数值(0.5)最近的数(存疑)
1 Z = np.random.uniform(0,1,20)2 print("随机数组: \n", Z)3 z = 0.5
4 m = Z.flat[np.abs(Z -z).argmin()]5
6 m
随机数组:
[0.93777567 0.51025044 0.59376374 0.22486936 0.93775471 0.8520337
0.98103144 0.85204206 0.22725586 0.09068851 0.4203348 0.40437622
0.4751998 0.1052024 0.30252427 0.77373344 0.22698046 0.80395887
0.33396734 0.6803853]0.5102504389227239
具体用法:https://numpy.org/devdocs/reference/generated/numpy.ndarray.flat.html?highlight=flat#numpy.ndarray.flat
https://numpy.org/devdocs/reference/generated/numpy.argmin.html?highlight=argmin#numpy.argmin
68. 将二维数组的前两行进行顺序交换
1 A = np.arange(25).reshape(5,5)2 print(A)3 A[[0,1]] = A[[1,0]]#第0,1行互换,注意是方括号,(0,1)的话就是元素的替换
4 print(A)
[[ 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]]
[[5 6 7 8 9]
[ 01 2 3 4]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
69. 找出随机一维数组中出现频率最高的值
'''bincount()计算非负整数数组中每个值的次数'''Z= np.random.randint(0,10,50)print("随机一维数组:", Z)
np.bincount(Z).argmax()
随机一维数组: [2 7 1 5 4 2 5 1 3 1 3 2 5 1 7 1 4 3 2 2 3 1 1 3 4 2 5 0 3 8 8 8 8 1 0 2 105 7 0 7 5 5 3 3 9 4 5 5]1
具体用法:https://numpy.org/devdocs/reference/generated/numpy.bincount.html?highlight=bincount#numpy.bincount
70. 找出给定一维数组中非 0 元素的位置索引
1 '''nonzero()'''
2 Z = np.nonzero([1,0,2,0,1,0,4,0])3 Z
(array([0, 2, 4, 6]),)
具体用法:https://numpy.org/devdocs/reference/generated/numpy.ndarray.nonzero.html?highlight=nonzero#numpy.ndarray.nonzero
71. 对于给定的 5x5 二维数组,在其内部随机放置 p 个值为 1 的数
1 '''np.put()用给定值替换数组的指定元素'''
2 '''np.random.choice()从给定一维数组生成随机样本'''
3 importnumpy as np4 p = 3
5 Z = np.zeros((5,5))6 np.put(Z, np.random.choice(range(5*5), p, replace=False),1)7 #np.random.choice(range(5*5),p,replace=False)作用是在一维数组[0,1,2,...,23,24]中#随机生成p(3)个随机样本组成的一维数组
8 #np.put(Z,[3,5,7],1)表示随机用3个1替代Z矩阵中的三个元素
9 Z
array([[0., 0., 0., 0., 0.],
[0., 0., 0.,1., 0.],
[0., 0., 0., 0.,1.],
[0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.put.html#numpy.put
https://numpy.org/devdocs/reference/random/generated/numpy.random.Generator.choice.html? highlight=numpy%20random%20choice#numpy.random.Generator.choice
72. 对于随机的 3x3 二维数组,减去数组每一行的平均值
1 '''mean()求取平均值(按行axis=1)'''
2 importnumpy as np3 X = np.random.rand(3, 3)4 print(X)5 Y = X - X.mean(axis=1, keepdims=True)6 Y
[[0.84664365 0.34947984 0.33087819]
[0.58354834 0.24696935 0.60656972]
[0.75552364 0.97000908 0.47839667]]
array([[0.33764309, -0.15952072, -0.17812237],
[0.1045192 , -0.23205979, 0.12754059],
[0.02088051, 0.23536595, -0.25624646]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.mean.html?highlight=mean#numpy.mean
73. 获得二维数组点积结果的对角线数组(存疑)
1 ”’对角线矩阵的求法'''
2 import numpy as np3 A = np.random.uniform(0,1,(3,3))4 B = np.random.uniform(0,1,(3,3))5 print(np.dot(A, B))6 # 较慢的方法7 np.diag(np.dot(A, B))8 #https://numpy.org/devdocs/reference/generated/numpy.diag.html?#highlight=diag#numpy.diag
[[0.61078817 0.42378285 0.80538938]
[0.24534479 0.51986857 0.73129344]
[0.45208917 0.77847453 0.89000805]]
array([0.61078817, 0.51986857, 0.89000805])
#较快的方法
np.sum(A * B.T, axis=1)
1 #更快的方法
2 np.einsum("ij, ji->i", A, B)
具体用法:https://numpy.org/devdocs/reference/generated/numpy.einsum.html?highlight=einsum#numpy.einsum
74. 找到随机一维数组中前 p 个最大值
1 '''argsort()返回对数组进行排序的索引'''
2 importnumpy as np3
4 Z = np.random.randint(1,100,100)5 print(Z)6
7 p = 5
8
9 Z[np.argsort(Z)[-p:]]#np.argsort(Z)[-5:]对Z一维数组升序排列以后最后五个数
[73 27 56 25 95 89 29 17 29 80 6 24 63 54 93 65 56 63 85 85 62 58 50 24
53 14 85 82 57 63 78 19 31 85 15 97 8 79 36 21 12 55 52 33 62 53 38 59
61 71 65 25 71 30 51 91 56 31 77 47 25 91 71 13 86 19 66 18 12 72 58 17
86 22 4 32 83 46 38 11 83 45 30 9 46 86 84 48 47 20 26 27 10 98 59 2
81 68 22 74]
array([91, 93, 95, 97, 98])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.argsort.html?highlight=argsort#numpy.argsort
75. 计算随机一维数组中每个元素的 4 次方数值
1 '''np.power()幂函数'''
2 importnumpy as np3 x = np.random.randint(2,5,5)4 print(x)5
6 np.power(x,4)
[3 3 4 3 4]
array([81, 81, 256, 81, 256])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.power.html?highlight=power#numpy.power
76. 对于二维随机数组中各元素,保留其 2 位小数
'''np.set_printoptions()确定浮点数,数组和其他NumPy对象的显示方式'''
importnumpy as np
Z= np.random.random((5,5))print(Z)
np.set_printoptions(precision=2)
Z
[[7.33e-01 4.29e-01 7.42e-01 5.49e-01 6.55e-01]
[7.28e-01 6.06e-01 4.49e-01 6.74e-01 5.44e-01]
[9.92e-01 6.40e-01 8.97e-01 1.39e-01 9.26e-01]
[1.84e-01 7.20e-01 6.65e-01 8.43e-01 6.27e-01]
[7.65e-01 3.06e-01 8.20e-01 3.71e-04 5.66e-01]]
array([[7.33e-01, 4.29e-01, 7.42e-01, 5.49e-01, 6.55e-01],
[7.28e-01, 6.06e-01, 4.49e-01, 6.74e-01, 5.44e-01],
[9.92e-01, 6.40e-01, 8.97e-01, 1.39e-01, 9.26e-01],
[1.84e-01, 7.20e-01, 6.65e-01, 8.43e-01, 6.27e-01],
[7.65e-01, 3.06e-01, 8.20e-01, 3.71e-04, 5.66e-01]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.set_printoptions.html?highlight=set_printoptions#numpy.set_printoptions
77. 使用科学记数法输出 NumPy 数组
Z = np.random.random([5,5])print(Z)
Z/1e3
[[0.14 0.28 0.87 0.81 0.88]
[0.88 0.31 0.03 0.71 0.18]
[0.9 0.4 0.66 0.98 0.96]
[0.99 0.24 0.23 0.33 0.11]
[0.11 0.57 0.36 0.36 0.45]]
array([[1.37e-04, 2.77e-04, 8.75e-04, 8.07e-04, 8.75e-04],
[8.77e-04, 3.12e-04, 3.20e-05, 7.07e-04, 1.83e-04],
[8.95e-04, 3.97e-04, 6.60e-04, 9.82e-04, 9.64e-04],
[9.92e-04, 2.38e-04, 2.34e-04, 3.28e-04, 1.05e-04],
[1.10e-04, 5.70e-04, 3.56e-04, 3.55e-04, 4.47e-04]])
78. 使用 NumPy 找出百分位数(25%,50%,75%)
1 '''np.percentile()计算沿指定轴的数据的第q个百分位数。返回数组元素的第q个百分点'''
2 importnumpy as np3 a = np.arange(15)4 print(a)5
6 np.percentile(a, q=[25, 50, 75])
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
array([3.5, 7. , 10.5])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.percentile.html?highlight=percentile#numpy.percentile
79. 找出数组中缺失值的总数及所在位置
1 '''isnan()找到缺失值的位置及总数'''
2 #生成含缺失值的 2 维数组
3 Z = np.random.rand(10,10)4 Z[np.random.randint(10, size=5), np.random.randint(10, size=5)] =np.nan5 #np.random.randint(10, size=5), np.random.randint(10, size=5)表示nan的位置
6 Z7 print("缺失值总数: \n", np.isnan(Z).sum())8 print("缺失值索引: \n", np.where(np.isnan(Z)))
array([[0.84, 0.52, 0.14, 0.95, 0.26, nan, 0.02, 0.69, 0.57, 0.09],
[0.84, 0.81, 0.27, 0.93, 0.09, 0.52, 0.61, 0.23, 0.66, 0.45],
[ nan, nan,0.13, 0.46, 0.33, 0.62, 0.25, 0.69, 0.07, 0.23],
[0.83, 0.96, 0.87, 0.8 , 0.71, 0.5 , 0.7 , 0.33, 0.52, 0.59],
[0.35, 0.78, 0.06, 0.26, 0.08, 0.51, nan, 0.28, 0.28, 0.26],
[0.4 , 0.75, 0.58, 0.48, 0.91, 0.02, 0.01, 0.15, 0.26, 0.7],
[0.73, 0.39, 0.58, 0.72, 0.07, 0.04, 0.93, 0.2 , 0.14, 0.17],
[0.66, 0.2 , 0.16, 0.59, 0.28, 0.17, 0.05, 0.57, 0.89, 0.56],
[0.64, 0.74, 0.86, 0.51, 0.77, 0.9 , 0.8 , 0.19, 0.42, 0.34],
[0.97, 0.77, 0.51, 0.78, 0.11, 0.27, 0.66, 0.67, nan, 0.7]])
缺失值总数:5缺失值索引:
(array([0,2, 2, 4, 9]), array([5, 0, 1, 6, 8]))
具体用法:https://numpy.org/devdocs/reference/generated/numpy.isnan.html?highlight=isnan#numpy.isnan
80. 从随机数组中删除包含缺失值的行(存疑)
1 #生成含缺失值的 2 维数组
2 Z = np.random.rand(10,10)3 Z[np.random.randint(10, size=5), np.random.randint(10, size=5)] =np.nan4 Z5 Z[np.sum(np.isnan(Z), axis=1) == 0]
array([[0.84, 0.81, 0.27, 0.93, 0.09, 0.52, 0.61, 0.23, 0.66, 0.45],
[0.83, 0.96, 0.87, 0.8 , 0.71, 0.5 , 0.7 , 0.33, 0.52, 0.59],
[0.4 , 0.75, 0.58, 0.48, 0.91, 0.02, 0.01, 0.15, 0.26, 0.7],
[0.73, 0.39, 0.58, 0.72, 0.07, 0.04, 0.93, 0.2 , 0.14, 0.17],
[0.66, 0.2 , 0.16, 0.59, 0.28, 0.17, 0.05, 0.57, 0.89, 0.56],
[0.64, 0.74, 0.86, 0.51, 0.77, 0.9 , 0.8 , 0.19, 0.42, 0.34]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.sum.html#numpy.sum
81. 统计随机数组中的各元素的数量
1 Z = np.random.randint(0,100,25).reshape(5,5)2 print(Z)3
4 np.unique(Z, return_counts=True) #返回值中,第 2 个数组对应第 1 个数组元素的数量
[[70 6 41 91 37]
[85 23 66 52 20]
[95 6 50 72 55]
[23 56 3 29 98]
[85 4 8 94 49]]
(array([3, 4, 6, 8, 20, 23, 29, 37, 41, 49, 50, 52, 55, 56, 66, 70, 72,85, 91, 94, 95, 98]),
array([1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1]))
具体用法:https://numpy.org/devdocs/reference/generated/numpy.unique.html?highlight=unique#numpy.unique
82. 将数组中各元素按指定分类转换为文本值
1 #指定类别如下
2 #1 → 汽车
3 #2 → 公交车
4 #3 → 火车
5
6
7 Z = np.random.randint(1,4,10)8 print(Z)9
10 label_map = {1: "汽车", 2: "公交车", 3: "火车"}11
12 [label_map[x] for x in Z]
[3 3 1 3 2 3 1 1 3 2]
['火车', '火车', '汽车', '火车', '公交车', '火车', '汽车', '汽车', '火车', '公交车']
83. 将多个 1 维数组拼合为单个 Ndarray
1 Z1 = np.arange(3)2 Z2 = np.arange(3,7)3 Z3 = np.arange(7,10)4
5 Z =np.array([Z1, Z2, Z3])6 print(Z)7
8 np.concatenate(Z)
[array([0, 1, 2]) array([3, 4, 5, 6]) array([7, 8, 9])]
array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.concatenate.html?highlight=concatenate#numpy.concatenate
84. 打印各元素在数组中升序排列的索引
1 a = np.random.randint(100, size=10)2 print('Array:', a)3
4 a.argsort()
Array: [70 29 17 84 11 76 26 9 92 3]
array([9, 7, 4, 2, 6, 1, 0, 5, 3, 8])
85. 得到二维随机数组各行的最大值
1 Z = np.random.randint(1,100, size=[5,5])2 print(Z)3
4 np.amax(Z, axis=1)
[[76 13 79 38 62]
[76 85 91 24 56]
[81 58 48 77 15]
[78 80 87 45 21]
[31 51 89 38 38]]
array([79, 91, 81, 87, 89])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.amax.html?highlight=amax#numpy.amax
86. 得到二维随机数组各行的最小值(区别上面的方法)
1 Z = np.random.randint(1,100, [5,5])2 print(Z)3 #np.amin(Z, axis=1)
4 np.apply_along_axis(np.min, arr=Z, axis=1)#区别于上一个方法
[[32 29 68 26 31]
[74 33 31 99 61]
[84 31 95 70 73]
[94 8 68 12 30]
[62 90 71 3 93]]
array([26, 31, 31, 8, 3])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.apply_along_axis.html?highlight=apply_along_axis#numpy.apply_along_axis
87. 计算两个数组之间的欧氏距离
1 a = np.array([1, 2])2 b = np.array([7, 8])3 print(a,b)4 #数学计算方法
5 print(np.sqrt(np.power((8-2), 2) + np.power((7-1), 2)))6
7 #NumPy 计算
8 np.linalg.norm(b-a)
[1 2] [7 8]8.48528137423857
8.48528137423857
具体用法:https://numpy.org/devdocs/reference/generated/numpy.linalg.norm.html?highlight=linalg%20norm#numpy.linalg.norm
88. 打印复数的实部和虚部
a = np.array([1 + 2j, 3 + 4j, 5 + 6j])print(a)print("实部:", a.real)print("虚部:", a.imag)
[1.+2.j 3.+4.j 5.+6.j]
实部: [1. 3. 5.]
虚部: [2. 4. 6.]
89. 求解给出矩阵的逆矩阵并验证
matrix = np.array([[1., 2.], [3., 4.]])
inverse_matrix=np.linalg.inv(matrix)#验证原矩阵和逆矩阵的点积是否为单位矩阵
assert np.allclose(np.dot(matrix, inverse_matrix), np.eye(2))
inverse_matrix
array([[-2. , 1. ],
[1.5, -0.5]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.linalg.inv.html?highlight=linalg%20inv#numpy.linalg.inv
90. 使用 Z-Score 标准化算法对数据进行标准化处理
Z-Score 标准化公式:
Z=(X−mean(X))/sd(X)
1 importnumpy as np2 #根据公式定义函数
3 def zscore(x, axis =None):4 xmean = x.mean(axis=axis, keepdims=True)5 xstd = np.std(x, axis=axis, keepdims=True)6 zscore = (x-xmean)/xstd7 returnzscore8
9 #生成随机数据
10 Z = np.random.randint(10, size=(5,5))11 print(Z)12
13 zscore(Z)
[[9 8 1 5 3]
[8 4 3 0 1]
[2 4 6 2 3]
[6 7 6 2 4]
[3 7 3 0 5]]
array([[1.97058711, 1.57006127, -1.23361957, 0.36848377, -0.4325679],
[1.57006127, -0.03204207, -0.4325679 , -1.63414541, -1.23361957],
[-0.83309374, -0.03204207, 0.7690096 , -0.83309374, -0.4325679],
[0.7690096 , 1.16953544, 0.7690096 , -0.83309374, -0.03204207],
[-0.4325679 , 1.16953544, -0.4325679 , -1.63414541, 0.36848377]])
91. 使用 Min-Max 标准化算法对数据进行标准化处理
Min-Max 标准化公式:
Y=(Z−min(Z))/(max(Z)−min(Z))
1 #根据公式定义函数
2 def min_max(x, axis=None):3 min = x.min(axis=axis, keepdims=True)4 max = x.max(axis=axis, keepdims=True)5 result = (x-min)/(max-min)6 returnresult7
8 #生成随机数据
9 Z = np.random.randint(10, size=(5,5))10 print(Z)11
12 min_max(Z)
[[4 2 8 4 7]
[3 2 7 3 4]
[4 3 5 3 4]
[9 4 1 7 9]
[3 7 6 3 8]]
array([[0.375, 0.125, 0.875, 0.375, 0.75],
[0.25 , 0.125, 0.75 , 0.25 , 0.375],
[0.375, 0.25 , 0.5 , 0.25 , 0.375],
[1. , 0.375, 0. , 0.75 , 1. ],
[0.25 , 0.75 , 0.625, 0.25 , 0.875]])
92. 使用 L2 范数对数据进行标准化处理(存疑)
L2 = √ X12 + X22 + X32 + ... + Xi2
1 '''np.linalg.norm()矩阵和向量范数'''
2 importnumpy as np3 #根据公式定义函数
4 def L2_normalize(v, axis=-1, order=2):5 L2 = np.linalg.norm(v, ord = order, axis=axis, keepdims=True)6 L2[L2==0] = 1
7 return v/L28
9 #生成随机数据
10 Z = np.random.randint(10, size=(5,5))11 print(Z)12
13 L2_normalize(Z)
1 # 根据公式定义函数2 def l2_normalize(v, axis=-1, order=2):3 l2 = np.linalg.norm(v, ord = order, axis=axis, keepdims=True)4 l2[l2==0] = 1
5 return v/l26
7 # 生成随机数据8 Z = np.random.randint(10, size=(5,5))9 print(Z)10
11 l2_normalize(Z)
[[7 5 7 4 1]
[3 0 5 7 1]
[8 9 3 4 4]
[9 7 9 2 3]
[0 9 1 4 6]]
array([[0.59160798, 0.42257713, 0.59160798, 0.3380617 , 0.08451543],
[0.32732684, 0. , 0.54554473, 0.76376262, 0.10910895],
[0.58658846, 0.65991202, 0.21997067, 0.29329423, 0.29329423],
[0.60133779, 0.46770717, 0.60133779, 0.13363062, 0.20044593],
[0. , 0.77748158, 0.08638684, 0.34554737, 0.51832106]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.linalg.norm.html?highlight=linalg%20norm#numpy.linalg.norm
93. 使用 NumPy 计算变量直接的相关性系数
1 '''np.corrcoef()Return Pearson product-moment correlation coefficients'''
2 importnumpy as np3
4 Z =np.array([5 [1, 2, 1, 9, 10, 3, 2, 6, 7], #特征 A
6 [2, 1, 8, 3, 7, 5, 10, 7, 2], #特征 B
7 [2, 1, 1, 8, 9, 4, 3, 5, 7]]) #特征 C
8
9 np.corrcoef(Z)
相关性系数取值从 [-1, 1] 变换,靠近 1 则代表正相关性较强,-1 则代表负相关性较强。结果如下所示,变量 A 与变量 A 直接的相关性系数为 1,因为是同一个变量。变量 A 与变量 C 之间的相关性系数为 0.97,说明相关性较强。
[A] [B] [C]
array([[1. , -0.06, 0.97] [A]
[-0.06, 1. , -0.01], [B]
[0.97, -0.01, 1. ]]) [C]
具体用法:https://numpy.org/devdocs/reference/generated/numpy.corrcoef.html?highlight=corrcoef#numpy.corrcoef
94. 使用 NumPy 计算矩阵的特征值和特征向量
1 '''np.linalg.eig()计算矩阵的特征值和特征向量'''
2 importnumpy as np3 M = np.array([[1,2,3], [4,5,6], [7,8,9]])#np.matrix()尽量少使用
4 w, v =np.linalg.eig(M)5 #w 对应特征值,v 对应特征向量
6 M, w, v
(array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]),
array([1.61168440e+01, -1.11684397e+00, -9.75918483e-16]),
array([[-0.23197069, -0.78583024, 0.40824829],
[-0.52532209, -0.08675134, -0.81649658],
[-0.8186735 , 0.61232756, 0.40824829]]))
具体用法:https://numpy.org/devdocs/reference/generated/numpy.matrix.html?highlight=matrix#numpy.matrix
https://numpy.org/devdocs/reference/generated/numpy.linalg.eig.html?highlight=linalg%20eig#numpy.linalg.eig
95. 使用 NumPy 计算 Ndarray 两相邻元素差值
1 '''np.diff()'''
2 importnumpy as np3
4 Z = np.random.randint(1,10,10)5 print(Z)6
7 #计算 Z 两相邻元素差值
8 print(np.diff(Z, n=1))9
10 #重复计算 2 次
11 print(np.diff(Z, n=2))12
13 #重复计算 3 次
14 print(np.diff(Z, n=3))
[ 8 3 2 2 6 4 6 6 5 1]
[-5 -1 0 4 -2 2 0 -1 -4]
[4 1 4 -6 4 -2 -1 -3]
[-3 3 -10 10 -6 1 -2]
具体用法:https://numpy.org/devdocs/reference/generated/numpy.diff.html?highlight=diff#numpy.diff
96. 使用 NumPy 将 Ndarray 相邻元素依次累加
1 '''连加'''
2 importnumpy as np3 Z = np.random.randint(1,10,10)4 print(Z)5
6 """
7 [第一个元素, 第一个元素 + 第二个元素, 第一个元素 + 第二个元素 + 第三个元素, ...]8 """
9 np.cumsum(Z)
[1 4 6 3 8 3 6 5 2 4]
array([1, 5, 11, 14, 22, 25, 31, 36, 38, 42])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.cumsum.html?highlight=cumsum#numpy.cumsum
97. 使用 NumPy 按列连接两个数组
1 '''np.c_按列连接两个数组'''
2 importnumpy as np3 M1 = np.array([[1, 2, 3],[7,8,9]])4 M2 = np.array([[4, 5, 6],[10,11,12]])5 print(M1)6 print(M2)7 np.c_[M1, M2]
[[1 2 3]
[7 8 9]]
[[4 5 6]
[10 11 12]]
array([[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.c_.html?highlight=c_#numpy.c_
98. 使用 NumPy 按行连接两个数组
1 '''np.r_[]按行连接数组'''
2 importnumpy as np3 M1 = np.array([[1, 2, 3],[7,8,9]])4 M2 = np.array([[4, 5, 6],[10,11,12]])5 print(M1)6 print(M2)7 np.r_[M1, M2]
[[1 2 3]
[7 8 9]]
[[4 5 6]
[10 11 12]]
array([[1, 2, 3],
[7, 8, 9],
[4, 5, 6],
[10, 11, 12]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.r_.html?highlight=r_#numpy.r_
99. 使用 NumPy 打印九九乘法表
1 '''np.fromfunction()通过在每个坐标上执行一个函数来构造一个数组'''
2 importnumpy as np3 np.fromfunction(lambda i, j: (i + 1) * (j + 1), (9, 9))4 #i表示行,j表示列;(i+1)*(j+1)是构造的函数;
5 #(9,9)是二维数组,9行9列
array([[ 1., 2., 3., 4., 5., 6., 7., 8., 9.],
[2., 4., 6., 8., 10., 12., 14., 16., 18.],
[3., 6., 9., 12., 15., 18., 21., 24., 27.],
[4., 8., 12., 16., 20., 24., 28., 32., 36.],
[5., 10., 15., 20., 25., 30., 35., 40., 45.],
[6., 12., 18., 24., 30., 36., 42., 48., 54.],
[7., 14., 21., 28., 35., 42., 49., 56., 63.],
[8., 16., 24., 32., 40., 48., 56., 64., 72.],
[9., 18., 27., 36., 45., 54., 63., 72., 81.]])
具体用法:https://numpy.org/devdocs/reference/generated/numpy.fromfunction.html?highlight=fromfunction#numpy.fromfunction
100. 使用 NumPy 将实验楼 LOGO 转换为 Ndarray 数组