numpy 数组array
1、数组的创建和类型dtype
#可以用arange创建数组
import numpy as np
t1 = np.arange(12)
t1
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
#用array创建numpy
t2 = np.array([[1,2,3],[4,5,6]])
t2
array([[1, 2, 3],
[4, 5, 6]])
#dtype
print(t1.dtype)
int32
t2 = np.array([[2.,3.5,4],[6,7,8]],dtype='float') #创建的时候指定数组中的数字类型
t2
array([[2. , 3.5, 4. ],
[6. , 7. , 8. ]])
t2.dtype
dtype('float64')
2、数组的形状
#shape,形状
t1.shape #t1形状 一行12列
(12,)
t2.shape #2行三列
(2, 3)
#改变形状 reshape()
t1.reshape((12,1))
array([[ 0],
[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11]])
t3 = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
t3
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
t3.shape #由2个二维数组(2行3列)组成
(2, 2, 3)
t3.reshape((12,)) #这里数组只有1个中括号(代表一维数组)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
t3.reshape((12,1)) #这里有2个中括号(代表二维数组)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12]])
t4 = np.arange(24).reshape((2,3,4)) #reshape()内部是个元组
t4 #这里有3个中括号,所以是三维数组,立体的
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]]])
t4.reshape((4,6)) #能直接输出,说明reshape()方法是由返回值的,返回的是一个新的数组
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]])
t5 = t4.reshape((2,12)) #用返回值赋值
t5
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]])
t5.shape[0] #shape[0]代表形状元组的第一个数 (2,12) 第一个数是2
2
t5.shape[1]
12
t4.shape[2] #t4是三维的,所以元组有三个数
4
#如果我不知道t4是几维的但是我就想把它变成一维的怎么办
t6 = np.array([t4.shape[i] for i in range(len(t4.shape))]) #列表推导式创建np的数组
t6.prod() #np的数组array元素累计求积
t4.reshape((t6.prod(),)) #array还有个属性是size 也可以这样写,t4.reshape((t4.size,))
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])
t4.flatten() #简单的方法 ,哈哈哈!调皮 flatten是压扁,变平的意思,就是把t4压扁了,返回一维的数组
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])
3、数组计算(广播)
#数组和数字
t5*2 #每个元素都乘以2,广播
array([[ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22],
[24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46]])
t5/0 #有除数为0的警告,nan 是非数字(not a number) inf是无穷大(infinity)
<ipython-input-40-cc2c150ad88b>:1: RuntimeWarning: divide by zero encountered in true_divide
t5/0
<ipython-input-40-cc2c150ad88b>:1: RuntimeWarning: invalid value encountered in true_divide
t5/0
array([[nan, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf],
[inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf, inf]])
#数组和数组
t6 = np.arange(6).reshape(2,3)
t6
array([[0, 1, 2],
[3, 4, 5]])
t7 = np.arange(10,16).reshape((2,3)) #reshape()里面可以直接写数字,也可以写元组,都一样,如果加2层括号呢
t7
array([[10, 11, 12],
[13, 14, 15]])
t8 = np.arange(10,16).reshape(((2,3))) #效果也一样
t8
array([[10, 11, 12],
[13, 14, 15]])
t7+t8 #两个数组都是2行3列的,对应的位置相加
array([[20, 22, 24],
[26, 28, 30]])
t9 = np.arange(20,26)
t9
array([20, 21, 22, 23, 24, 25])
t6+t9 #报错,两个shape不一样,不能相加
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-47-6750e94be23e> in <module>
----> 1 t6+t9
ValueError: operands could not be broadcast together with shapes (2,3) (6,)
t6*t9 #相乘也不行
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-216d1124308b> in <module>
----> 1 t6*t9
ValueError: operands could not be broadcast together with shapes (2,3) (6,)
t10 = np.arange(0,3)
t10
array([0, 1, 2])
t6
array([[0, 1, 2],
[3, 4, 5]])
t6+t10 #一个2行3列的 可以和 一个1行3列的相加 ,逐行按列的位置相加
array([[0, 2, 4],
[3, 5, 7]])
t6*t10 #相乘也可以,逐行按列的位置相乘
array([[ 0, 1, 4],
[ 0, 4, 10]])
#那么如果是2行1列的是否可以和2行2列的计算?
t11 = np.array([[3],[3]])
t11
array([[3],
[3]])
t11+t6 #逐列按行的位置相加(相运算)
array([[3, 4, 5],
[6, 7, 8]])
#那么如果是3行3列和t6(2行3列)的能否计算?
t12 = np.arange(9).reshape(3,3)
t12+t6 #报错,所以要么是单行,要么是单列而且行数和列数要相同,是不是可以理解成,单行的可以转变成每行都一样的多行,然后再计算?
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-60-6e6e08fb96e7> in <module>
1 #那么如果是3行3列和t6(2行3列)的能否计算?
2 t12 = np.arange(9).reshape(3,3)
----> 3 t12+t6
ValueError: operands could not be broadcast together with shapes (3,3) (2,3)
#那么如果是t6(2行3列)和一个3行2列的可以计算么?
t13 = np.arange(6).reshape(3,2)
t13
array([[0, 1],
[2, 3],
[4, 5]])
t6*t13 #也不行
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-63-76630e4424cc> in <module>
----> 1 t6*t13
ValueError: operands could not be broadcast together with shapes (2,3) (3,2)
4、广播兼容总结
#如果一个数组形状A是(4,3,2)那么是可以和形状B(3,2)运算的,后面的形状B 可以理解成(1,3,2)
t14 = np.arange(24).reshape(4,3,2)
t14
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]]])
t15 = np.arange(6).reshape(3,2)
t15
array([[0, 1],
[2, 3],
[4, 5]])
t14*t15
array([[[ 0, 1],
[ 4, 9],
[ 16, 25]],
[[ 0, 7],
[ 16, 27],
[ 40, 55]],
[[ 0, 13],
[ 28, 45],
[ 64, 85]],
[[ 0, 19],
[ 40, 63],
[ 88, 115]]])
#如果(4,3,2) 和(4,3)呢?报错,(4,3)相当于(1,4,3),如果维度不一样只能前面补1,(4,3,2)和(1,4,3)从后往前一个都对不上
t16 = np.arange(12).reshape(4,3)
t16*t14
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-67-5643830d637d> in <module>
1 #如果(4,3,2) 和(4,3)呢?
2 t16 = np.arange(12).reshape(4,3)
----> 3 t16*t14
ValueError: operands could not be broadcast together with shapes (4,3) (4,3,2)
#总结:如果是(4,3,2)和(4,1,2)呢? 可以计算,把其中的任何一个元组的值换成1都行,也可以和(4,3,1)计算,当然也可以和(1,3,1)也就是(3,1)计算
t17 = np.arange(8).reshape(4,1,2)
t17*t14
array([[[ 0, 1],
[ 0, 3],
[ 0, 5]],
[[ 12, 21],
[ 16, 27],
[ 20, 33]],
[[ 48, 65],
[ 56, 75],
[ 64, 85]],
[[108, 133],
[120, 147],
[132, 161]]])
t18 = np.arange(3).reshape(3,1) #可以计算
t18*t14
array([[[ 0, 0],
[ 2, 3],
[ 8, 10]],
[[ 0, 0],
[ 8, 9],
[20, 22]],
[[ 0, 0],
[14, 15],
[32, 34]],
[[ 0, 0],
[20, 21],
[44, 46]]])
5、读取文件 loadtext
t1 = np.loadtxt('C:/Users/Administrator/Desktop/20200827.csv',delimiter=',',dtype='int') #找一个csv文件load一下
t1
array([[ 10523, 1, 4],
[ 36975, 1, 4],
[120775, 1, 4],
...,
[333628, 9, 4],
[812211, 9, 5],
[813162, 9, 5]])
#还可以转置一下(沿着左上右下对角线翻转)
t1 = np.loadtxt('C:/Users/Administrator/Desktop/20200827.csv',delimiter=',',dtype='int',unpack='True')
t1
array([[ 10523, 36975, 120775, ..., 333628, 812211, 813162],
[ 1, 1, 1, ..., 9, 9, 9],
[ 4, 4, 4, ..., 4, 5, 5]])
6、类比矩阵的相乘
#2行3列的矩阵可以和3行2列的矩阵相乘 ,用dot方法
t1 = np.arange(6).reshape(2,3)
t2 = np.arange(6).reshape(3,2)
t1.dot(t2) #结果是2行2列 ,如果是(3,2)*(2,3)的话,结果是(3,3)
array([[10, 13],
[28, 40]])
t1 = np.arange(8).reshape(2,4)
t2 = np.arange(4).reshape(4,1)
t1.dot(t2) #(2,4)X(4,1)结果是(2,1),可以发现,两个矩阵相乘挨着的2个维度值要相同
array([[14],
[38]])