大家好,我又来给大家分享新知识了,最近几篇博客(文末有链接)都致力于将以前的学习资源整理成博客的形式分享给大家,真心干货满满,希望能给大家带来收获。
那么本篇博客将会给出大家平时使用NumPy的时候经常需要用到的功能代码,同时也会给出运行结果,以帮助大家更进一步的理解。
另外,我也以注释的形式更进一步的补充说明代码的功能及其作用,需要本篇博文中用到的文档文件以及代码的朋友,也可以三连支持一下,并评论留下你的邮箱,我会在看到后的第一时间发送给你。
当然啦,你也可以把本篇博文当作一本小小的NumPy书籍,当需要用到pandas哪些知识的时候,Ctrl+F就可以搜索到啦,现在不看的话就先收藏着。
- 一、基本操作
- 1.1 数组创建
- 1.2 查看数组属性
- 1.3 文件读写
- 二、数据类型
- 三、数组运算
- 3.1 基本运算
- 3.2 逻辑运算
- 3.3 数组与标量计算
- 3.4 -= += *=直接改变原数组
- 四、复制和视图
- 4.1 完全没有复制
- 4.2 视图、查看或者浅拷贝
- 4.3 深拷贝
- 五、索引、切片和迭代
- 5.1 基本索引和切片
- 5.2 花式索引和索引技巧
- 六、形状操作
- 6.1 数组变形
- 6.2 数组转置
- 6.3 数据堆叠合并
- 6.4 数组拆分
- 七、广播机制
- 八、通用函数
- 8.1 元素级数字级别的方法
- 8.2 where函数
- 8.3 排序
- 8.4 集合操作
- 8.5 数学和统计函数
- 九、线性代数
- 结束语
import numpy as np # Shift + Enter
# 创建可以将Python,中list列表转换成NumPy数组 l = [1,2,3,4,5] # NumPy数组 nd1 = np.array(l) # 输入一部分arr + tab(命令中自动补全,按键) 代码提示,自动补全 print(nd1) display(nd1) # 显示
[1 2 3 4 5] array([1, 2, 3, 4, 5])
nd2 = np.zeros(shape = (3,4),dtype = np.int16) # shift + tab提示方法的属性,使用 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 # juppyter中执行程序,代码,最后一行,默认就是输出
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) # 从0,到100,生成随机数字,int,整数 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) # 生成0~1之间随机数 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) # 正态分布,平均值是0,标准差是1 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)) # 正态分布,平均值是175,标准差是10 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) # 等差数列,左闭右开,100取不到 nd9
array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])
nd10 = np.linspace(1,100,num = 19) # 等差数列,左闭右闭,num表示生成等差数列长度 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]])
# 查看数组形状,返回了形状 shape = (5,3) nd.shape
(5, 3)
nd.dtype # 告诉数组的数据类型 float64 位,一位占一个0或者一个1
dtype('float64')
nd.size # 尺寸,数组可以是多维的,请问,里面共有多少数据 3*5 = 15
15
nd.ndim # 数组维度
2
nd.itemsize # 条目 尺寸长度 8 字节 # 数据类型是float64 64位 -----> 1个字节8位-----> 64/8 = 8 字节
81.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') # 默认添加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) # 保存数据是起名:a,abc,称为key,自己命名
data = np.load('./data.npz')
data
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]])
# data['www'] # 没有保存,无法获取
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.]])
二、数据类型
# int8,int16,int32,int64,uint8无符号 # float16,float32,float64 # str字符串类型 # int8 表示 2**8个数字 256个 -128 ~ 127 有符号 # uint8 表示256个数字,无符号,表明只有正数:0 ~ 255 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) # 默认数据类型是float64
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=' nd2.dtypedtype('三、数组运算 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 # nd3数组操作后,接收的对象array([ 7, 5, -2, 0, 7])nd1 * nd2 # 乘法array([ 0, 36, 24, 36, 0])nd1 / nd2 # 除法d:pythonlibsite-packagesipykernel_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**38np.power(2,3) # 表示2的3次幂8np.power(nd1,nd2) # 表示nd1的nd2次幂,对应位置,进行计算array([ 1, 6561, 4096, 46656, 1], dtype=int32)np.log(100) # 底数是自然底数e 2.7184.605170185988092np.log10(1000) # 对数运算返回结果是:33.0np.log2(1024) # 返回结果就是:1010.03.2 逻辑运算display(nd1,nd2)array([7, 9, 4, 6, 7]) array([0, 4, 6, 6, 0])nd1 > nd2array([ True, True, False, False, True])nd1 < nd2array([False, False, True, False, False])nd1 >= nd2 # 表示nd1数组中的数据,是否大于等于nd2中的对应位置的数据,如果大于等于,放回Truearray([ True, True, False, True, True])nd1 == nd2 # 两个等号表示逻辑判断,问,是否相等array([False, False, False, True, False])3.3 数组与标量计算nd1array([7, 9, 4, 6, 7])# 数字3,4,5……都是标量 nd1 + 10 # 所有的位置都加了10,广播array([17, 19, 14, 16, 17])nd1 - 1024array([-1017, -1015, -1020, -1018, -1017])nd1 * 256array([1792, 2304, 1024, 1536, 1792])nd1 / 1024array([0.00683594, 0.00878906, 0.00390625, 0.00585938, 0.00683594])# 数组可以做分母,注意不能有0 1/nd1array([0.14285714, 0.11111111, 0.25 , 0.16666667, 0.14285714])1/np.array([1,3,0,5]) # 0不能作为分母计算结果:infd:pythonlibsite-packagesipykernel_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 # 没有打印输出,说明,改变了原来的数组nd1array([-93, -91, -96, -94, -93])nd2 +=100 nd2array([100, 104, 106, 106, 100])nd1 *= 3 nd1array([-279, -273, -288, -282, -279])# nd1 /= 10 数组不支持 /=四、复制和视图 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和b一回事Truea[0] = 1024 # 改变a那么b也发生了变化 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 # 说明a和b不一样Falsea.flags.owndata # a数组数据是自己的Trueb.flags.owndata # b是浅拷贝a的数据,也就是b并不拥有自己的数据Falsea[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() # 深拷贝,此时,a和b没有关系了 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) # b 对象拥有自己的数据False True Truea[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) # 0 ~ 1亿,数据量非常多的 aarray([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 # 删除原来的数组,内存优化barray([ 1., 3., 5., 7., 9., 99.])五、索引、切片和迭代 5.1 基本索引和切片a = np.random.randint(0,30,size = 10) aarray([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] # 如果冒号前面不写,默认从0开始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] # 从索引3开始,每三个数中,取一个array([26, 14, 3])a[::-1] # 倒着数,数组进行了颠倒array([ 3, 8, 24, 14, 4, 28, 26, 9, 19, 28])aarray([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] # 从索引1开始到7结束,每两个中取一个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]24b[3,[2,5,6]] # 多维数组,不怕,我们可以用逗号,分割array([18, 16, 14])b[2:7,1::3] # 行:从2到索引7。列:从1开始,每3个中取一个数字array([[24, 24, 4], [ 0, 9, 19], [18, 7, 13], [28, 7, 16], [ 9, 14, 5]])b[-1,-1] # 给-1表示倒着数27b[-2,[-2,-3,-4]]array([12, 21, 25])5.2 花式索引和索引技巧a = np.arange(20) aarray([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])b = a[3:7] # 切片时,返回的数据,不是深拷贝 barray([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)) # 100名学生,参加了3门考试:Python、Math、En aarray([[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 # 逻辑运算 # 根据条件,筛选数据,只要大于120,返回,一门大于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])# boolean True = 1;False = 0 # 三门科目的条件进行相乘 # 三个科目都是 大于120的同学 cond2 = cond[:,0]*cond[:,1]*cond[:,2] a[cond2]array([[141, 145, 121], [121, 141, 147]])# 大于等于120,小于等于30找到 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)) aarray([[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) # -1表示数据,3自动计算-1 = 5array([[0, 2, 3], [7, 1, 2], [1, 9, 3], [0, 3, 3], [8, 7, 1]])6.2 数组转置# 转置,行变列,列变行 a.T # 矩阵 shape = (5,3)array([[0, 2, 3], [2, 1, 3], [3, 9, 8], [7, 3, 7], [1, 0, 1]])np.transpose(a,(1,0)) # 行0,列1。默认情况下(0,1)----->调整(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)) # shape = (0,1,2) barray([[[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)) # 就是调整维度结构2和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]])# 修改axis参数调整数据合并方向 np.concatenate([nd1,nd2],axis = 1) # axis 轴,方向 0 = 行,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)) # 堆叠,摞起来,增多,合并 h表示水平,列增多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)) # v 竖直,行增加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)in 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)6.4 数组拆分in ----> 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) a = np.random.randint(0,100,size = (15,10)) aarray([[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) # 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]) # 0~1,1~5,5~9,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) # h水平,列方向上分割成了两份[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]) # v表示竖直,行切片,行分割[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) arr1array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]])arr2 = np.array([1,2,3]) display(arr1,arr2) # 形状不对应,依然可以进行运算:NumPy底层,为我们进行了广播array([[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]]) array([1, 2, 3])# 行不够,广播行 arr1 + arr2 # arr2 和arr1 中每一行,进行相加:广播机制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 # 广播,arr2和arr1中每一列,进行相加:广播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) aarray([[[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) barray([[0, 1], [2, 3], [4, 5], [6, 7]])display(a.shape,b.shape)(3, 4, 2) (4, 2)a * b # b形状(4,2),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 元素级数字级别的方法# abs、sqrt、square、exp、log、sin、cos、tan,maxinmum、minimum、 # all、any、inner、clip、round、trace、ceil、floora = 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]) barray([ 1, 4, 9, 16, 36, 49])np.sqrt(b) # 开平发array([1., 2., 3., 4., 6., 7.])np.square(b) # 平方 np.exp(3) # 自然底数e的多少次幂 np.log(20.085536) # 自然底数e对数求解 np.sin(np.pi/2) # 90度sin正弦值 np.cos(0) # 余弦值 np.tan(np.pi/6) # 正切,30度正切值 # 给两个数组,从中选取大的,或者选取小的 np.maximum(a,c) # 从a和c中选取最大的值 np.minimum(a,c) # 选取最小的值array([-1, -3, -5, 1, 5, 7, -2])nd1 = np.array([1,3,0]) # 出现0那么对应False,非零True nd2 = np.array([-1,-3,4,8]) display(nd1,nd2)array([1, 3, 0]) array([-1, -3, 4, 8])nd1.any() # 只要有一个True,返回TrueTruend1.all() # 所有True,返回TrueFalsend2.all() # 所有的都是True,返回TrueTruea = np.array([1,2,3,4,5]) b = np.array([1,2,3,4,6]) np.inner(a,b) # 1*1 + 2*2 + 3*3 + 4*4 + 5*6 = 60 # 返回的是两个数组的内积,对应位置相乘加和60nd1 = np.random.randint(0,100,size = 30) nd1array([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) # 数据裁剪,将小于10变成10,将大于80的变成80array([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) nd2array([ 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)) aarray([[6, 5, 1], [7, 3, 6], [9, 1, 3]])np.trace(a) # 就算对角线上的和128.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) # 条件如果是True,那么返回nd1中数据,如果Flase返回nd2中数据array([1, 4, 6, 7, 9])a = np.random.randint(0,100,size = 50) display(a) # 数据展示 np.where(a > 50,a,a + 20) # 大于50,返回多少;不然返回-100array([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])# 如果分数50~59分之间,自动加10分 a = np.random.randint(0,100,size = 50) aarray([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) aarray([45, 66, 26, 25, 69, 35, 31, 24, 73, 3, 14, 82, 21, 86, 56, 7, 36, 39, 9, 28])b = np.sort(a) # 打印输出,那么,原数组,没有改变 barray([ 3, 7, 9, 14, 21, 24, 25, 26, 28, 31, 35, 36, 39, 45, 56, 66, 69, 73, 82, 86])a.sort() # 没有输出,说明,原数组上进行了排序aarray([ 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) # 交集:a和b中都有array([ 4, 6, 23, 24])np.union1d(a,b) # 并集: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) # 差集,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)) aarray([[30, 38, 14, 95, 19], [11, 32, 55, 94, 33], [59, 64, 41, 12, 1]])a.min()1a.max(axis = 0) # axis 轴,方向。axis = 0 行,axis = 1列array([59, 64, 55, 95, 33])a.max(axis = 1)array([95, 94, 64])a.mean() # 平均值39.86666666666667np.median(a) # 中位数33.0a.sum() # 求和598a.std() # 标准差27.789366471528076a.var() # 方差,数据内部波动772.2488888888888a.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() # 返回最小值的索引14a.argmax() # 返回最大值的索引3aarray([[30, 38, 14, 95, 19], [11, 32, 55, 94, 33], [59, 64, 41, 12, 1]])index = np.argwhere((a > 50) | (a < 20)) # 返回就是符合条件的索引 indexarray([[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# cov 协方差(属性之间进行计算),方差概念类似(数据内部,属性内部计算) # 举例子:一个男生受女生欢迎程度,和这名男生萎缩程度,是否成正比,什么关系 np.cov(a)array([[1060.7 , 763.25, -250.85], [ 763.25, 992.5 , -463. ], [-250.85, -463. , 784.3 ]])np.corrcoef(a) # 相关性系数,1(正相关) ~ -1(负相关) # 0 表示,没有关系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) # 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) # dot矩阵乘法 ,点乘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)) Carray([[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)in ----> 1 A.dot(C) # 形状不对应,无法进行矩阵乘法 ValueError: shapes (3,3) and (4,5) not aligned: 3 (dim 1) != 4 (dim 0) # B.shape = (3,4);C.shape = (4,5) B.dot(C)array([[21, 13, 15, 29, 49], [82, 48, 38, 76, 92], [46, 43, 28, 47, 33]])# C.shape = (4,5);B.shape = (3,4) C.dot(B) # 矩阵乘法不满足交换律!!!--------------------------------------------------------------------------- ValueError Traceback (most recent call last)结束语in 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@报告,今天也有好好学习



