大家好,我又来给大家分享新知识了,最近几篇博客(文末有链接)都致力于将以前的学习资源整理成博客的形式分享给大家,真心干货满满,希望能给大家带来收获。
那么本篇博客将会给出大家平时使用NumPy的时候经常需要用到的功能代码,同时也会给出运行结果,以帮助大家更进一步的理解。
另外,我也以注释的形式更进一步的补充说明代码的功能及其作用,需要本篇博文中用到的文档文件以及代码的朋友,也可以三连支持一下,并评论留下你的邮箱,我会在看到后的第一时间发送给你。
当然啦,你也可以把本篇博文当作一本小小的NumPy书籍,当需要用到pandas哪些知识的时候,Ctrl+F就可以搜索到啦,现在不看的话就先收藏着。
一、基本操作
1.1 数组创建
import numpy as np
l = [1,2,3,4,5]
nd1 = np.array(l)
print(nd1)
display(nd1)
[1 2 3 4 5]
array([1, 2, 3, 4, 5])
nd2 = np.zeros(shape = (3,4),dtype = np.int16)
nd2
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int16)
nd3 = np.ones(shape = (3,5),dtype=np.float32)
nd3
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]], dtype=float32)
nd4 = np.full(shape = (3,4,5),fill_value=3.1415926)
nd4
array([[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],
[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],
[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],
[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]]])
nd5 = np.random.randint(0,100,size = 20)
nd5
array([26, 97, 32, 88, 28, 65, 77, 74, 68, 97, 32, 35, 46, 55, 33, 83, 63,
6, 77, 5])
nd6 = np.random.rand(3,5)
nd6
array([[0.64501117, 0.58522154, 0.90132264, 0.99409845, 0.63923959],
[0.2164321 , 0.53874694, 0.54988461, 0.82581533, 0.42652412],
[0.01025381, 0.49834132, 0.71353756, 0.44433708, 0.05175048]])
nd7 = np.random.randn(3,5)
display(nd7)
array([[-1.34974864, -0.35255807, -0.06337357, -0.39990286, 0.18276669],
[ 0.42686337, -0.29675634, -0.66351388, 0.15499455, 0.22191029],
[-2.24510816, -0.25372978, 0.61602861, -0.53877681, 1.8443575 ]])
nd8 = np.random.normal(loc = 175,scale = 10,size = (3,5))
print(nd8)
[[154.35099611 188.63428445 178.86129064 173.99374674 173.92688007]
[173.48768953 185.57252565 172.63843251 192.40089968 177.04776165]
[166.25486758 198.20977267 162.28102209 167.1159521 183.41324182]]
nd9 = np.arange(1,100,step = 10)
nd9
array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])
nd10 = np.linspace(1,100,num = 19)
nd10
array([ 1. , 6.5, 12. , 17.5, 23. , 28.5, 34. , 39.5, 45. ,
50.5, 56. , 61.5, 67. , 72.5, 78. , 83.5, 89. , 94.5,
100. ])
1.2 查看数组属性
import numpy as np
nd = np.random.randn(5,3)
nd
array([[ 0.72059556, -1.95187973, -0.56373137],
[-1.73917205, -1.16500837, 1.42147895],
[ 0.38178684, -1.44311435, -0.47301186],
[-1.02896549, 0.05223197, 0.05234227],
[ 0.21264316, -1.0635056 , -0.98622601]])
nd.shape
(5, 3)
nd.dtype
dtype('float64')
nd.size
15
nd.ndim
2
nd.itemsize
8
1.3 文件读写
nd1 = np.random.randint(0,100,size = (3,5))
nd2 = np.random.randn(3,5)
display(nd1,nd2)
array([[17, 9, 81, 22, 35],
[33, 3, 76, 56, 39],
[51, 7, 98, 68, 76]])
array([[-1.00157588, -1.42826357, -0.0595288 , 1.52754491, -0.36709515],
[-1.27106787, 0.10364645, 0.32066376, -1.66321598, -1.25959691],
[-1.71740637, -0.25518009, -0.81794158, 0.76914636, 1.14322894]])
np.save('./data',nd1)
np.load('./data.npy')
array([[17, 9, 81, 22, 35],
[33, 3, 76, 56, 39],
[51, 7, 98, 68, 76]])
np.savez('./data.npz',a = nd1,abc = nd2)
data = np.load('./data.npz')
data
<numpy.lib.npyio.NpzFile at 0x2352497f7c8>
data['a']
array([[17, 9, 81, 22, 35],
[33, 3, 76, 56, 39],
[51, 7, 98, 68, 76]])
data['abc']
array([[-1.00157588, -1.42826357, -0.0595288 , 1.52754491, -0.36709515],
[-1.27106787, 0.10364645, 0.32066376, -1.66321598, -1.25959691],
[-1.71740637, -0.25518009, -0.81794158, 0.76914636, 1.14322894]])
np.savez_compressed('./data2.npz',x = nd1,y = nd2)
np.load('./data2.npz')['x']
array([[0, 4, 9, 0, 8],
[6, 2, 5, 1, 1],
[7, 5, 2, 4, 3]])
np.savetxt(fname = './data.txt',
X = nd1,
fmt='%0.2f',
delimiter=',')
np.savetxt(fname = './data.cvs',
X = nd1,
fmt='%d',
delimiter=';')
np.loadtxt('./data.cvs',delimiter=';')
array([[0., 4., 9., 0., 8.],
[6., 2., 5., 1., 1.],
[7., 5., 2., 4., 3.]])
np.loadtxt('./data.txt',delimiter=',')
array([[0., 4., 9., 0., 8.],
[6., 2., 5., 1., 1.],
[7., 5., 2., 4., 3.]])
二、数据类型
np.array([2,4,7],dtype = np.int8)
array([2, 4, 7], dtype=int8)
np.array([-3,-7,255,108,0,256],dtype = np.uint8)
array([253, 249, 255, 108, 0, 0], dtype=uint8)
np.random.randint(0,100,size = 10,dtype = 'int64')
array([ 8, 36, 88, 80, 20, 52, 83, 42, 18, 1], dtype=int64)
nd = np.random.rand(10,2)
nd
array([[0.91420288, 0.83364378],
[0.88974876, 0.33297581],
[0.40118288, 0.07479842],
[0.16937616, 0.26712123],
[0.94525666, 0.34948455],
[0.96704048, 0.61851364],
[0.90099123, 0.3941368 ],
[0.58999302, 0.40133028],
[0.95706455, 0.53783821],
[0.19142784, 0.38579803]])
nd.dtype
dtype('float64')
np.asarray(nd,dtype = 'float16')
array([[0.914 , 0.8335 ],
[0.8896 , 0.333 ],
[0.4011 , 0.07477],
[0.1694 , 0.267 ],
[0.9453 , 0.3494 ],
[0.967 , 0.6187 ],
[0.901 , 0.394 ],
[0.59 , 0.4014 ],
[0.957 , 0.5376 ],
[0.1914 , 0.3857 ]], dtype=float16)
nd.astype(dtype = np.float16)
array([[0.914 , 0.8335 ],
[0.8896 , 0.333 ],
[0.4011 , 0.07477],
[0.1694 , 0.267 ],
[0.9453 , 0.3494 ],
[0.967 , 0.6187 ],
[0.901 , 0.394 ],
[0.59 , 0.4014 ],
[0.957 , 0.5376 ],
[0.1914 , 0.3857 ]], dtype=float16)
nd = np.random.randn(1000,3)
np.save('./data1',nd)
np.save('./data2',nd.astype('float16'))
nd2 = np.array(list('abcdefghi'))
nd2
array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], dtype='<U1')
nd2.dtype
dtype('<U1')
三、数组运算
3.1 基本运算
nd1 = np.random.randint(0,10,size = 5)
nd2 = np.random.randint(0,10,size = 5)
display(nd1,nd2)
array([7, 9, 4, 6, 7])
array([0, 4, 6, 6, 0])
nd3 = nd1 - nd2
nd3
array([ 7, 5, -2, 0, 7])
nd1 * nd2
array([ 0, 36, 24, 36, 0])
nd1 / nd2
d:\python\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in true_divide
"""Entry point for launching an IPython kernel.
array([ inf, 2.25 , 0.66666667, 1. , inf])
nd1**nd2
array([ 1, 6561, 4096, 46656, 1], dtype=int32)
2**3
8
np.power(2,3)
8
np.power(nd1,nd2)
array([ 1, 6561, 4096, 46656, 1], dtype=int32)
np.log(100)
4.605170185988092
np.log10(1000)
3.0
np.log2(1024)
10.0
3.2 逻辑运算
display(nd1,nd2)
array([7, 9, 4, 6, 7])
array([0, 4, 6, 6, 0])
nd1 > nd2
array([ True, True, False, False, True])
nd1 < nd2
array([False, False, True, False, False])
nd1 >= nd2
array([ True, True, False, True, True])
nd1 == nd2
array([False, False, False, True, False])
3.3 数组与标量计算
nd1
array([7, 9, 4, 6, 7])
nd1 + 10
array([17, 19, 14, 16, 17])
nd1 - 1024
array([-1017, -1015, -1020, -1018, -1017])
nd1 * 256
array([1792, 2304, 1024, 1536, 1792])
nd1 / 1024
array([0.00683594, 0.00878906, 0.00390625, 0.00585938, 0.00683594])
1/nd1
array([0.14285714, 0.11111111, 0.25 , 0.16666667, 0.14285714])
1/np.array([1,3,0,5])
d:\python\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in true_divide
"""Entry point for launching an IPython kernel.
array([1. , 0.33333333, inf, 0.2 ])
3.4 -= += *=直接改变原数组
display(nd1,nd2)
array([7, 9, 4, 6, 7])
array([0, 4, 6, 6, 0])
nd1 -= 100
nd1
array([-93, -91, -96, -94, -93])
nd2 +=100
nd2
array([100, 104, 106, 106, 100])
nd1 *= 3
nd1
array([-279, -273, -288, -282, -279])
四、复制和视图
4.1 完全没有复制
a = np.random.randint(0,10,size = 5)
b = a
display(a,b)
array([7, 9, 3, 2, 9])
array([7, 9, 3, 2, 9])
a is b
True
a[0] = 1024
display(a,b)
array([1024, 9, 3, 2, 9])
array([1024, 9, 3, 2, 9])
4.2 视图、查看或者浅拷贝
a = np.random.randint(0,100,size = 5)
b = a.view()
display(a,b)
array([69, 16, 91, 6, 96])
array([69, 16, 91, 6, 96])
a is b
False
a.flags.owndata
True
b.flags.owndata
False
a[0] = 1024
b[1] = 2048
display(a,b)
array([1024, 2048, 91, 6, 96])
array([1024, 2048, 91, 6, 96])
4.3 深拷贝
a = np.random.randint(-100,0,size = 10)
b = a.copy()
display(a,b)
array([-99, -5, -16, -5, -11, -72, -57, -69, -66, -96])
array([-99, -5, -16, -5, -11, -72, -57, -69, -66, -96])
display(a is b)
display(a.flags.owndata)
display(b.flags.owndata)
False
True
True
a[0] = 1024
b[2] = 2048
display(a,b)
array([1024, -5, -16, -5, -11, -72, -57, -69, -66, -96])
array([ -99, -5, 2048, -5, -11, -72, -57, -69, -66, -96])
a = np.arange(1e8)
a
array([0.0000000e+00, 1.0000000e+00, 2.0000000e+00, ..., 9.9999997e+07,
9.9999998e+07, 9.9999999e+07])
b = a[[1,3,5,7,9,99]].copy()
del a
b
array([ 1., 3., 5., 7., 9., 99.])
五、索引、切片和迭代
5.1 基本索引和切片
a = np.random.randint(0,30,size = 10)
a
array([28, 19, 9, 26, 28, 4, 14, 24, 8, 3])
a[3]
a[[1,3,5]]
array([19, 26, 4])
a[0:3]
array([28, 19, 9])
a[:3]
array([28, 19, 9])
a[5:9]
array([ 4, 14, 24, 8])
a[5:]
array([ 4, 14, 24, 8, 3])
a[::2]
array([28, 9, 28, 14, 8])
a[3::3]
array([26, 14, 3])
a[::-1]
array([ 3, 8, 24, 14, 4, 28, 26, 9, 19, 28])
a
array([28, 19, 9, 26, 28, 4, 14, 24, 8, 3])
a[::-2]
array([ 3, 24, 4, 26, 19])
a[5::-3]
array([4, 9])
a[1:7:2]
array([19, 26, 4])
b = np.random.randint(0,30,size = (10,10))
b
array([[25, 23, 13, 28, 17, 13, 8, 16, 25, 3],
[20, 7, 6, 26, 26, 13, 24, 0, 20, 18],
[ 1, 24, 10, 25, 24, 21, 16, 4, 26, 29],
[12, 0, 18, 20, 9, 16, 14, 19, 19, 20],
[28, 18, 29, 7, 7, 15, 5, 13, 13, 7],
[28, 28, 25, 13, 7, 8, 5, 16, 8, 2],
[27, 9, 2, 25, 14, 7, 26, 5, 14, 11],
[ 6, 14, 10, 20, 24, 28, 10, 0, 24, 12],
[ 8, 21, 22, 21, 24, 6, 25, 21, 12, 26],
[ 0, 19, 8, 5, 20, 1, 3, 3, 15, 27]])
b[1]
array([20, 7, 6, 26, 26, 13, 24, 0, 20, 18])
b[[0,3,5]]
array([[25, 23, 13, 28, 17, 13, 8, 16, 25, 3],
[12, 0, 18, 20, 9, 16, 14, 19, 19, 20],
[28, 28, 25, 13, 7, 8, 5, 16, 8, 2]])
b[1,6]
24
b[3,[2,5,6]]
array([18, 16, 14])
b[2:7,1::3]
array([[24, 24, 4],
[ 0, 9, 19],
[18, 7, 13],
[28, 7, 16],
[ 9, 14, 5]])
b[-1,-1]
27
b[-2,[-2,-3,-4]]
array([12, 21, 25])
5.2 花式索引和索引技巧
a = np.arange(20)
a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
b = a[3:7]
b
array([3, 4, 5, 6])
b[0] = 1024
display(a,b)
array([ 0, 1, 2, 1024, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19])
array([1024, 4, 5, 6])
a = np.arange(20)
b = a[[3,4,5,6]]
display(a,b)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
array([3, 4, 5, 6])
b[0] = 1024
display(a,b)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
array([1024, 4, 5, 6])
a = np.random.randint(0,151,size = (100,3))
a
array([[100, 128, 122],
[110, 1, 6],
[120, 100, 108],
[ 57, 112, 145],
[ 46, 121, 130],
[141, 20, 25],
[ 7, 98, 15],
[141, 145, 121],
[ 1, 29, 117],
[ 94, 62, 46],
[ 35, 135, 101],
[ 96, 38, 137],
[ 97, 114, 60],
[121, 113, 38],
[ 4, 85, 107],
[ 13, 79, 12],
[129, 92, 150],
[ 54, 38, 34],
[109, 55, 88],
[ 1, 52, 23],
[ 16, 80, 146],
[122, 72, 126],
[ 39, 76, 52],
[144, 68, 69],
[121, 141, 147],
[141, 71, 110],
[100, 40, 108],
[ 91, 121, 65],
[ 3, 44, 105],
[ 79, 61, 75],
[117, 146, 88],
[ 3, 2, 71],
[ 78, 24, 86],
[125, 62, 93],
[ 53, 88, 132],
[ 96, 75, 36],
[102, 119, 97],
[ 24, 48, 78],
[104, 21, 150],
[ 8, 34, 30],
[108, 56, 58],
[ 22, 144, 100],
[ 53, 115, 94],
[ 52, 104, 9],
[ 59, 2, 4],
[ 85, 48, 138],
[119, 21, 73],
[ 9, 31, 77],
[ 21, 3, 132],
[ 43, 113, 39],
[ 51, 5, 134],
[ 37, 97, 123],
[ 0, 92, 73],
[ 37, 86, 13],
[ 48, 78, 128],
[ 74, 56, 48],
[138, 105, 68],
[129, 129, 103],
[ 48, 42, 14],
[ 50, 102, 123],
[ 6, 97, 16],
[ 22, 88, 122],
[ 98, 23, 137],
[ 95, 74, 20],
[ 20, 111, 132],
[ 67, 9, 28],
[ 74, 1, 79],
[ 62, 8, 27],
[ 24, 12, 22],
[ 58, 111, 130],
[ 29, 23, 28],
[ 10, 30, 38],
[113, 62, 122],
[ 70, 141, 97],
[137, 106, 53],
[ 76, 91, 101],
[128, 22, 101],
[134, 75, 100],
[114, 92, 79],
[103, 20, 145],
[105, 26, 51],
[ 34, 51, 18],
[117, 115, 31],
[ 7, 119, 75],
[ 23, 74, 149],
[ 56, 15, 11],
[143, 73, 148],
[ 11, 22, 26],
[ 18, 113, 120],
[ 37, 150, 21],
[115, 89, 13],
[134, 108, 98],
[115, 58, 4],
[114, 58, 29],
[ 26, 135, 10],
[ 82, 147, 147],
[ 56, 13, 39],
[ 21, 125, 134],
[120, 71, 32],
[143, 46, 124]])
cond = a >= 120
a[cond]
array([128, 122, 120, 145, 121, 130, 141, 141, 145, 121, 135, 137, 121,
129, 150, 146, 122, 126, 144, 121, 141, 147, 141, 121, 146, 125,
132, 150, 144, 138, 132, 134, 123, 128, 138, 129, 129, 123, 122,
137, 132, 130, 122, 141, 137, 128, 134, 145, 149, 143, 148, 120,
150, 134, 135, 147, 147, 125, 134, 120, 143, 124])
cond2 = cond[:,0]*cond[:,1]*cond[:,2]
a[cond2]
array([[141, 145, 121],
[121, 141, 147]])
cond1 = a >=120
cond2 = a <= 30
a[cond2[:,0]*cond2[:,1]*cond2[:,2]]
array([[24, 12, 22],
[29, 23, 28],
[11, 22, 26]])
六、形状操作
6.1 数组变形
a = np.random.randint(0,10,size = (3,5))
a
array([[0, 2, 3, 7, 1],
[2, 1, 9, 3, 0],
[3, 3, 8, 7, 1]])
a.reshape(5,3)
array([[0, 2, 3],
[7, 1, 2],
[1, 9, 3],
[0, 3, 3],
[8, 7, 1]])
a.reshape(15,1,1)
array([[[0]],
[[2]],
[[3]],
[[7]],
[[1]],
[[2]],
[[1]],
[[9]],
[[3]],
[[0]],
[[3]],
[[3]],
[[8]],
[[7]],
[[1]]])
a.reshape(-1,3)
array([[0, 2, 3],
[7, 1, 2],
[1, 9, 3],
[0, 3, 3],
[8, 7, 1]])
6.2 数组转置
a.T
array([[0, 2, 3],
[2, 1, 3],
[3, 9, 8],
[7, 3, 7],
[1, 0, 1]])
np.transpose(a,(1,0))
array([[0, 2, 3],
[2, 1, 3],
[3, 9, 8],
[7, 3, 7],
[1, 0, 1]])
b = np.random.randint(0,10,size = (3,5,7))
b
array([[[3, 0, 4, 0, 4, 8, 0],
[6, 2, 2, 1, 6, 0, 4],
[9, 5, 2, 8, 8, 6, 2],
[0, 6, 2, 8, 0, 0, 5],
[4, 3, 2, 3, 4, 0, 0]],
[[6, 7, 8, 5, 5, 4, 9],
[3, 0, 5, 6, 8, 6, 4],
[9, 3, 5, 5, 6, 4, 6],
[3, 0, 5, 9, 2, 7, 4],
[5, 5, 4, 7, 8, 2, 9]],
[[6, 8, 6, 8, 3, 1, 7],
[7, 9, 1, 3, 9, 3, 2],
[1, 9, 7, 7, 4, 2, 8],
[9, 6, 7, 3, 6, 0, 9],
[3, 3, 6, 2, 5, 2, 5]]])
c = np.transpose(b,(2,1,0))
c.shape
(7, 5, 3)
6.3 数据堆叠合并
nd1 = np.random.randint(0,10,size = (3,5))
nd2 = np.random.randint(0,10,size = (3,5))
display(nd1,nd2)
array([[5, 1, 3, 3, 4],
[3, 0, 9, 3, 7],
[2, 9, 3, 8, 4]])
array([[1, 9, 9, 0, 7],
[7, 8, 8, 0, 8],
[6, 0, 9, 3, 3]])
np.concatenate([nd1,nd2])
array([[5, 1, 3, 3, 4],
[3, 0, 9, 3, 7],
[2, 9, 3, 8, 4],
[1, 9, 9, 0, 7],
[7, 8, 8, 0, 8],
[6, 0, 9, 3, 3]])
np.concatenate([nd1,nd2],axis = 1)
array([[5, 1, 3, 3, 4, 1, 9, 9, 0, 7],
[3, 0, 9, 3, 7, 7, 8, 8, 0, 8],
[2, 9, 3, 8, 4, 6, 0, 9, 3, 3]])
np.hstack((nd1,nd2))
array([[5, 1, 3, 3, 4, 1, 9, 9, 0, 7],
[3, 0, 9, 3, 7, 7, 8, 8, 0, 8],
[2, 9, 3, 8, 4, 6, 0, 9, 3, 3]])
np.vstack((nd1,nd2,nd2,nd2,nd1))
array([[5, 1, 3, 3, 4],
[3, 0, 9, 3, 7],
[2, 9, 3, 8, 4],
[1, 9, 9, 0, 7],
[7, 8, 8, 0, 8],
[6, 0, 9, 3, 3],
[1, 9, 9, 0, 7],
[7, 8, 8, 0, 8],
[6, 0, 9, 3, 3],
[1, 9, 9, 0, 7],
[7, 8, 8, 0, 8],
[6, 0, 9, 3, 3],
[5, 1, 3, 3, 4],
[3, 0, 9, 3, 7],
[2, 9, 3, 8, 4]])
a = np.random.randint(0,10,size = (3,5))
b = np.random.randint(0,10,size = (3,6))
display(a,b)
np.concatenate([a,b])
array([[2, 5, 3, 6, 8],
[0, 8, 1, 2, 3],
[6, 0, 7, 1, 2]])
array([[0, 9, 6, 2, 4, 8],
[3, 0, 0, 0, 8, 3],
[7, 3, 1, 3, 7, 0]])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-205-16b2fe89e6c7> in <module>
2 b = np.random.randint(0,10,size = (3,6))
3 display(a,b)
----> 4 np.concatenate([a,b]) # 报错
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 6
np.concatenate([a,b],axis =1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-242-9acf5d5d319b> in <module>
----> 1 np.concatenate([a,b],axis =1)
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
6.4 数组拆分
a = np.random.randint(0,100,size = (15,10))
a
array([[73, 62, 19, 9, 90, 98, 38, 91, 14, 72],
[64, 34, 80, 81, 77, 83, 69, 84, 5, 42],
[29, 79, 49, 87, 15, 9, 98, 68, 33, 72],
[ 0, 89, 36, 54, 11, 12, 27, 39, 14, 11],
[26, 84, 21, 94, 18, 32, 64, 31, 64, 39],
[45, 0, 61, 18, 16, 29, 77, 34, 18, 86],
[10, 63, 81, 38, 44, 47, 28, 96, 54, 53],
[67, 50, 24, 15, 42, 10, 18, 92, 41, 42],
[61, 0, 67, 41, 13, 87, 82, 57, 54, 50],
[32, 69, 72, 33, 56, 12, 12, 66, 55, 94],
[49, 45, 68, 15, 24, 51, 18, 18, 59, 47],
[77, 58, 93, 79, 89, 63, 41, 82, 8, 20],
[70, 84, 75, 67, 39, 53, 32, 31, 20, 37],
[20, 51, 54, 30, 5, 71, 59, 51, 96, 68],
[52, 31, 73, 56, 60, 3, 81, 2, 7, 69]])
np.split(a,indices_or_sections=5)
[array([[73, 62, 19, 9, 90, 98, 38, 91, 14, 72],
[64, 34, 80, 81, 77, 83, 69, 84, 5, 42],
[29, 79, 49, 87, 15, 9, 98, 68, 33, 72]]),
array([[ 0, 89, 36, 54, 11, 12, 27, 39, 14, 11],
[26, 84, 21, 94, 18, 32, 64, 31, 64, 39],
[45, 0, 61, 18, 16, 29, 77, 34, 18, 86]]),
array([[10, 63, 81, 38, 44, 47, 28, 96, 54, 53],
[67, 50, 24, 15, 42, 10, 18, 92, 41, 42],
[61, 0, 67, 41, 13, 87, 82, 57, 54, 50]]),
array([[32, 69, 72, 33, 56, 12, 12, 66, 55, 94],
[49, 45, 68, 15, 24, 51, 18, 18, 59, 47],
[77, 58, 93, 79, 89, 63, 41, 82, 8, 20]]),
array([[70, 84, 75, 67, 39, 53, 32, 31, 20, 37],
[20, 51, 54, 30, 5, 71, 59, 51, 96, 68],
[52, 31, 73, 56, 60, 3, 81, 2, 7, 69]])]
np.split(a,indices_or_sections=2,axis =1)
[array([[73, 62, 19, 9, 90],
[64, 34, 80, 81, 77],
[29, 79, 49, 87, 15],
[ 0, 89, 36, 54, 11],
[26, 84, 21, 94, 18],
[45, 0, 61, 18, 16],
[10, 63, 81, 38, 44],
[67, 50, 24, 15, 42],
[61, 0, 67, 41, 13],
[32, 69, 72, 33, 56],
[49, 45, 68, 15, 24],
[77, 58, 93, 79, 89],
[70, 84, 75, 67, 39],
[20, 51, 54, 30, 5],
[52, 31, 73, 56, 60]]),
array([[98, 38, 91, 14, 72],
[83, 69, 84, 5, 42],
[ 9, 98, 68, 33, 72],
[12, 27, 39, 14, 11],
[32, 64, 31, 64, 39],
[29, 77, 34, 18, 86],
[47, 28, 96, 54, 53],
[10, 18, 92, 41, 42],
[87, 82, 57, 54, 50],
[12, 12, 66, 55, 94],
[51, 18, 18, 59, 47],
[63, 41, 82, 8, 20],
[53, 32, 31, 20, 37],
[71, 59, 51, 96, 68],
[ 3, 81, 2, 7, 69]])]
np.split(a,indices_or_sections=[1,5,9])
[array([[73, 62, 19, 9, 90, 98, 38, 91, 14, 72]]),
array([[64, 34, 80, 81, 77, 83, 69, 84, 5, 42],
[29, 79, 49, 87, 15, 9, 98, 68, 33, 72],
[ 0, 89, 36, 54, 11, 12, 27, 39, 14, 11],
[26, 84, 21, 94, 18, 32, 64, 31, 64, 39]]),
array([[45, 0, 61, 18, 16, 29, 77, 34, 18, 86],
[10, 63, 81, 38, 44, 47, 28, 96, 54, 53],
[67, 50, 24, 15, 42, 10, 18, 92, 41, 42],
[61, 0, 67, 41, 13, 87, 82, 57, 54, 50]]),
array([[32, 69, 72, 33, 56, 12, 12, 66, 55, 94],
[49, 45, 68, 15, 24, 51, 18, 18, 59, 47],
[77, 58, 93, 79, 89, 63, 41, 82, 8, 20],
[70, 84, 75, 67, 39, 53, 32, 31, 20, 37],
[20, 51, 54, 30, 5, 71, 59, 51, 96, 68],
[52, 31, 73, 56, 60, 3, 81, 2, 7, 69]])]
np.hsplit(a,indices_or_sections=2)
[array([[73, 62, 19, 9, 90],
[64, 34, 80, 81, 77],
[29, 79, 49, 87, 15],
[ 0, 89, 36, 54, 11],
[26, 84, 21, 94, 18],
[45, 0, 61, 18, 16],
[10, 63, 81, 38, 44],
[67, 50, 24, 15, 42],
[61, 0, 67, 41, 13],
[32, 69, 72, 33, 56],
[49, 45, 68, 15, 24],
[77, 58, 93, 79, 89],
[70, 84, 75, 67, 39],
[20, 51, 54, 30, 5],
[52, 31, 73, 56, 60]]),
array([[98, 38, 91, 14, 72],
[83, 69, 84, 5, 42],
[ 9, 98, 68, 33, 72],
[12, 27, 39, 14, 11],
[32, 64, 31, 64, 39],
[29, 77, 34, 18, 86],
[47, 28, 96, 54, 53],
[10, 18, 92, 41, 42],
[87, 82, 57, 54, 50],
[12, 12, 66, 55, 94],
[51, 18, 18, 59, 47],
[63, 41, 82, 8, 20],
[53, 32, 31, 20, 37],
[71, 59, 51, 96, 68],
[ 3, 81, 2, 7, 69]])]
np.vsplit(a,indices_or_sections=[3,7,11])
[array([[73, 62, 19, 9, 90, 98, 38, 91, 14, 72],
[64, 34, 80, 81, 77, 83, 69, 84, 5, 42],
[29, 79, 49, 87, 15, 9, 98, 68, 33, 72]]),
array([[ 0, 89, 36, 54, 11, 12, 27, 39, 14, 11],
[26, 84, 21, 94, 18, 32, 64, 31, 64, 39],
[45, 0, 61, 18, 16, 29, 77, 34, 18, 86],
[10, 63, 81, 38, 44, 47, 28, 96, 54, 53]]),
array([[67, 50, 24, 15, 42, 10, 18, 92, 41, 42],
[61, 0, 67, 41, 13, 87, 82, 57, 54, 50],
[32, 69, 72, 33, 56, 12, 12, 66, 55, 94],
[49, 45, 68, 15, 24, 51, 18, 18, 59, 47]]),
array([[77, 58, 93, 79, 89, 63, 41, 82, 8, 20],
[70, 84, 75, 67, 39, 53, 32, 31, 20, 37],
[20, 51, 54, 30, 5, 71, 59, 51, 96, 68],
[52, 31, 73, 56, 60, 3, 81, 2, 7, 69]])]
七、广播机制
arr1 = np.array([0,1,2,3]*3)
arr1.sort()
arr1 = arr1.reshape(4,3)
arr1
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
arr2 = np.array([1,2,3])
display(arr1,arr2)
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
array([1, 2, 3])
arr1 + arr2
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
arr3 = np.array([[1],[2],[3],[4]])
display(arr1,arr3)
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
array([[1],
[2],
[3],
[4]])
arr1 + arr3
array([[1, 1, 1],
[3, 3, 3],
[5, 5, 5],
[7, 7, 7]])
a = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2)
a
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]]])
b = np.array([0,1,2,3,4,5,6,7]).reshape(4,2)
b
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
display(a.shape,b.shape)
(3, 4, 2)
(4, 2)
a * b
array([[[ 0, 1],
[ 4, 9],
[16, 25],
[36, 49]],
[[ 0, 1],
[ 4, 9],
[16, 25],
[36, 49]],
[[ 0, 1],
[ 4, 9],
[16, 25],
[36, 49]]])
八、通用函数
8.1 元素级数字级别的方法
a = np.array([-1,-3,-5,1,5,8,9])
c = np.random.randint(-5,10,size = 7)
display(a,c)
array([-1, -3, -5, 1, 5, 8, 9])
array([ 2, -3, -4, 7, 7, 7, -2])
np.abs(a)
array([1, 3, 5, 1, 5, 8, 9])
b = np.array([1,4,9,16,36,49])
b
array([ 1, 4, 9, 16, 36, 49])
np.sqrt(b)
array([1., 2., 3., 4., 6., 7.])
np.square(b)
np.exp(3)
np.log(20.085536)
np.sin(np.pi/2)
np.cos(0)
np.tan(np.pi/6)
np.maximum(a,c)
np.minimum(a,c)
array([-1, -3, -5, 1, 5, 7, -2])
nd1 = np.array([1,3,0])
nd2 = np.array([-1,-3,4,8])
display(nd1,nd2)
array([1, 3, 0])
array([-1, -3, 4, 8])
nd1.any()
True
nd1.all()
False
nd2.all()
True
a = np.array([1,2,3,4,5])
b = np.array([1,2,3,4,6])
np.inner(a,b)
60
nd1 = np.random.randint(0,100,size = 30)
nd1
array([61, 15, 31, 92, 91, 24, 20, 59, 6, 41, 46, 8, 30, 37, 53, 70, 59,
24, 66, 43, 36, 95, 14, 7, 67, 86, 53, 26, 70, 62])
np.clip(nd1,10,80)
array([61, 15, 31, 80, 80, 24, 20, 59, 10, 41, 46, 10, 30, 37, 53, 70, 59,
24, 66, 43, 36, 80, 14, 10, 67, 80, 53, 26, 70, 62])
nd2 = np.random.randn(20)
nd2
array([ 0.5020347 , -0.60462332, -1.17663827, -0.37269518, -1.42816971,
0.023136 , 0.782975 , 0.30537136, -0.43498012, -1.08404953,
0.85617269, 1.16387006, -0.01622137, 1.21683923, -0.29338921,
-2.78006002, 0.07321195, 1.11264868, -0.70013613, -1.2466278 ])
nd2.round(2)
array([ 0.5 , -0.6 , -1.18, -0.37, -1.43, 0.02, 0.78, 0.31, -0.43,
-1.08, 0.86, 1.16, -0.02, 1.22, -0.29, -2.78, 0.07, 1.11,
-0.7 , -1.25])
np.ceil(np.array([2.7,2.1,2.05]))
array([3., 3., 3.])
np.floor(np.array([2.99,2.9999,2.1]))
array([2., 2., 2.])
a = np.random.randint(0,10,size = (3,3))
a
array([[6, 5, 1],
[7, 3, 6],
[9, 1, 3]])
np.trace(a)
12
8.2 where函数
import numpy as np
nd1 = np.array([1,3,5,7,9])
nd2 = np.array([2,4,6,8,10])
cond = np.array([True,False,False,True,True])
np.where(cond,nd1,nd2)
array([1, 4, 6, 7, 9])
a = np.random.randint(0,100,size = 50)
display(a)
np.where(a > 50,a,a + 20)
array([86, 27, 93, 30, 55, 13, 37, 60, 1, 6, 44, 77, 74, 11, 71, 95, 45,
32, 0, 40, 13, 23, 38, 4, 36, 96, 81, 10, 79, 90, 13, 19, 15, 50,
90, 20, 51, 56, 56, 42, 86, 41, 35, 33, 33, 46, 69, 35, 50, 15])
array([86, 47, 93, 50, 55, 33, 57, 60, 21, 26, 64, 77, 74, 31, 71, 95, 65,
52, 20, 60, 33, 43, 58, 24, 56, 96, 81, 30, 79, 90, 33, 39, 35, 70,
90, 40, 51, 56, 56, 62, 86, 61, 55, 53, 53, 66, 69, 55, 70, 35])
a = np.random.randint(0,100,size = 50)
a
array([39, 55, 20, 11, 12, 5, 46, 55, 37, 90, 93, 33, 24, 53, 91, 82, 82,
64, 25, 12, 74, 64, 73, 10, 50, 35, 87, 81, 59, 62, 85, 0, 53, 0,
49, 99, 54, 82, 26, 52, 28, 66, 63, 79, 57, 70, 82, 38, 95, 58])
cond = (a >=50) & (a < 60)
np.where(cond,a + 10,a)
array([39, 65, 20, 11, 12, 5, 46, 65, 37, 90, 93, 33, 24, 63, 91, 82, 82,
64, 25, 12, 74, 64, 73, 10, 60, 35, 87, 81, 69, 62, 85, 0, 63, 0,
49, 99, 64, 82, 26, 62, 28, 66, 63, 79, 67, 70, 82, 38, 95, 68])
8.3 排序
a = np.random.randint(0,100,size = 20)
a
array([45, 66, 26, 25, 69, 35, 31, 24, 73, 3, 14, 82, 21, 86, 56, 7, 36,
39, 9, 28])
b = np.sort(a)
b
array([ 3, 7, 9, 14, 21, 24, 25, 26, 28, 31, 35, 36, 39, 45, 56, 66, 69,
73, 82, 86])
a.sort()
a
array([ 3, 7, 9, 14, 21, 24, 25, 26, 28, 31, 35, 36, 39, 45, 56, 66, 69,
73, 82, 86])
index = a.argsort()
display(index)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19], dtype=int64)
a[index][::-1]
array([86, 82, 73, 69, 66, 56, 45, 39, 36, 35, 31, 28, 26, 25, 24, 21, 14,
9, 7, 3])
8.4 集合操作
a = np.random.randint(0,30,size = 15)
b = np.random.randint(0,30,size = 15)
display(a,b)
array([28, 9, 20, 24, 10, 7, 18, 23, 6, 28, 4, 13, 14, 6, 6])
array([16, 25, 4, 23, 23, 4, 24, 26, 0, 8, 15, 6, 5, 23, 11])
np.intersect1d(a,b)
array([ 4, 6, 23, 24])
np.union1d(a,b)
array([ 0, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 18, 20, 23, 24,
25, 26, 28])
np.setdiff1d(a,b)
array([ 7, 9, 10, 13, 14, 18, 20, 28])
8.5 数学和统计函数
min、max、mean、median、sum、std、var、cumsum、cumprod、argmin、argmax、argwhere、cov、corrcoef
a = np.random.randint(0,100,size = (3,5))
a
array([[30, 38, 14, 95, 19],
[11, 32, 55, 94, 33],
[59, 64, 41, 12, 1]])
a.min()
1
a.max(axis = 0)
array([59, 64, 55, 95, 33])
a.max(axis = 1)
array([95, 94, 64])
a.mean()
39.86666666666667
np.median(a)
33.0
a.sum()
598
a.std()
27.789366471528076
a.var()
772.2488888888888
a.cumsum()
array([ 30, 68, 82, 177, 196, 207, 239, 294, 388, 421, 480, 544, 585,
597, 598], dtype=int32)
b = np.array([1,2,3,4,5,6,7])
b.cumprod()
array([ 1, 2, 6, 24, 120, 720, 5040], dtype=int32)
a.argmin()
14
a.argmax()
3
a
array([[30, 38, 14, 95, 19],
[11, 32, 55, 94, 33],
[59, 64, 41, 12, 1]])
index = np.argwhere((a > 50) | (a < 20))
index
array([[0, 2],
[0, 3],
[0, 4],
[1, 0],
[1, 2],
[1, 3],
[2, 0],
[2, 1],
[2, 3],
[2, 4]], dtype=int64)
for i,j in index:
print(a[i,j])
14
95
19
11
55
94
59
64
12
1
np.cov(a)
array([[1060.7 , 763.25, -250.85],
[ 763.25, 992.5 , -463. ],
[-250.85, -463. , 784.3 ]])
np.corrcoef(a)
array([[ 1. , 0.74388409, -0.27502788],
[ 0.74388409, 1. , -0.5247768 ],
[-0.27502788, -0.5247768 , 1. ]])
九、线性代数
A = np.random.randint(0,10,size = (3,3))
B = np.random.randint(0,10,size = (3,4))
display(A,B)
array([[3, 1, 2],
[5, 4, 8],
[8, 6, 5]])
array([[3, 6, 2, 0],
[6, 2, 8, 2],
[0, 1, 4, 5]])
A.dot(B)
array([[15, 22, 22, 12],
[39, 46, 74, 48],
[60, 65, 84, 37]])
np.dot(A,B)
array([[15, 22, 22, 12],
[39, 46, 74, 48],
[60, 65, 84, 37]])
A @ B
array([[15, 22, 22, 12],
[39, 46, 74, 48],
[60, 65, 84, 37]])
C = np.random.randint(0,10,size = (4,5))
C
array([[1, 3, 1, 5, 7],
[0, 0, 1, 1, 3],
[9, 2, 3, 4, 5],
[2, 7, 3, 6, 2]])
A.dot(C)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-384-f96e5078725a> in <module>
----> 1 A.dot(C) # 形状不对应,无法进行矩阵乘法
ValueError: shapes (3,3) and (4,5) not aligned: 3 (dim 1) != 4 (dim 0)
B.dot(C)
array([[21, 13, 15, 29, 49],
[82, 48, 38, 76, 92],
[46, 43, 28, 47, 33]])
C.dot(B)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-386-0ff241fbe642> in <module>
1 # C.shape = (4,5);B.shape = (3,4)
----> 2 C.dot(B) # 矩阵乘法不满足交换律!!!
ValueError: shapes (4,5) and (3,4) not aligned: 5 (dim 1) != 3 (dim 0)
结束语
本篇博文的代码是在jupyter上运行的,不过具体在哪运行都没什么大的区别。
感谢收看,祝学业和工作进步! 需要本文资料的话,欢迎关注评论留下你的邮箱。
推荐关注的专栏
👨?👩?👦?👦 机器学习:分享机器学习实战项目和常用模型讲解 👨?👩?👦?👦 数据分析:分享数据分析实战项目和常用技能整理
往期内容回顾
💚 学习Python全套代码【超详细】Python入门、核心语法、数据结构、Python进阶【致那个想学好Python的你】 ?? 学习pandas全套代码【超详细】数据查看、输入输出、选取、集成、清洗、转换、重塑、数学和统计方法、排序 💙 学习pandas全套代码【超详细】分箱操作、分组聚合、时间序列、数据可视化
关注我,了解更多相关知识!
CSDN@报告,今天也有好好学习
|