广播(原则)机制:可以对数组的每一个元素进行操作,同一个形状的数组会在对应相同的位置进行加减乘除计算。
2.不同维度的数组进行计算
t1
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]])
>>> t2 = np.arange(4).reshape(4,1)
>>> t2
array([[0],
[1],
[2],
[3]])
>>> t1-t2
array([[ 0, 1, 2, 3, 4, 5],
[ 5, 6, 7, 8, 9, 10],
[10, 11, 12, 13, 14, 15],
[15, 16, 17, 18, 19, 20]])
>>>
如上面代码所示,若是二维,会在不同的维度,但其中必须有维度长度(比如列))相等,进行自动扩展,进行计算。但若是三维,则从末尾开始计算起维度的轴长相符或其中一方长度为1,某一方向上(多维数组上,每一维可以看作一个方向)?不理解,比如(3,3,3)不能和(3,2)计算,但(3,3,2)可以和(3,2)计算。
轴:在numpy中可以理解为方向,使用0,1,2...数字表示,对于一个一维数组,只有一个0轴,对于2维数组(shape(2,2)),有0轴和1轴,对于三维数组(shape(2,2, 3)),有0,1,2轴
回顾np.arange(0,10).reshape((2,5)),reshpe中2表示0轴长度(包含数据的条数)为2,1轴长度为5,2X5一共10个数据
3.numpy读取本地数据
csv:逗号分隔值文件
np.loadtxt(fname,dtype=np.float,delimiter=None,skiprows=0,usecols=None,unpack=False)
upack=True,将矩阵沿对角线交换数据,相当于矩阵转置。矩阵转置:
t2 = np.arange(24).reshape(4,6)
>>> t2
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]])
>>> t2.transpose()
array([[ 0, 6, 12, 18],
[ 1, 7, 13, 19],
[ 2, 8, 14, 20],
[ 3, 9, 15, 21],
[ 4, 10, 16, 22],
[ 5, 11, 17, 23]])
>>> t2.T
array([[ 0, 6, 12, 18],
[ 1, 7, 13, 19],
[ 2, 8, 14, 20],
[ 3, 9, 15, 21],
[ 4, 10, 16, 22],
[ 5, 11, 17, 23]])
>>> t2.swapaxes(1,0)
array([[ 0, 6, 12, 18],
[ 1, 7, 13, 19],
[ 2, 8, 14, 20],
[ 3, 9, 15, 21],
[ 4, 10, 16, 22],
[ 5, 11, 17, 23]])
3.numpy取某一行或者某一列的数据
若读取某一行的数据,直接根据某一行行的索引值,就可以读取
import numpy as np
us_file_path = "./youtube_video_data/US_video_data_numbers.csv"
uk_file_path = "./youtube_video_data/GB_video_data_numbers.csv"
t1 = np.loadtxt(us_file_path,delimiter=",",dtype="int")
print(t1)
print("*"*100)
print(t1[2])#直接根据行的索引就可以读取到数据
#取行
取不连续的多行
#取不连续的多行
print("*"*100)
print(t1[[2,8,10]])#
[[5845909 576597 39774 170708]
[1338533 69687 678 5643]
[ 859289 34485 726 1914]]
取某一列,:,,冒号加逗号
#取某一列
print(t1[:,0])#只是将列的数据打印成一行。
[4394029 7860119 5845909 ... 142463 2162240 515000]
取连续的多列
#取连续的多列
print(t1[:,2:])
输出:
[[ 5931 46245]
[ 26679 0]
[ 39774 170708]
...
[ 148 279]
取不连续的多列
#取不连续的多列
print(t1[:,[0,2]])
输出:
[[4394029 5931]
[7860119 26679]
[5845909 39774]
...
取行和列
#取行和列
print(t1[2,3])
输出:
170708
取多行和多列
#取第三行到第五行,第二列到第四列的结果
b = t1[2:5,1:4]
print(b)
[[576597 39774 170708]
[ 24975 4542 12829]
[ 96666 568 6666]]
取多个不相邻的点,交叉
#是(0,0)、(2,1)、(2,3)组合
c = t1[[0,2,2],[0,1,3]]
print(c)
输出:
[4394029 576597 170708]
4.numpy的三元运算符,相当于if,else语句,if t1<10:t1=0 elset1=10
t1 = np.where(t1<10,0,10)
输出:
[[ 0 0 0 0 0 0]
[ 0 0 0 0 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]]
5.numpy中的clip,也是相当于if,else
t1.clip(10,18),<=10换为10,其他则换为18
6.行和列交换
import numpy as np
t = np.arange(12,24).reshape(3,4)
print(t)
#行和列交换
t[[[1,2]],:] = t[[2,1],:]
print(t)
t[:,[0,2]] = t[:,[2,0]]
print(t)
输出:
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
[[12 13 14 15]
[20 21 22 23]
[16 17 18 19]]
[[14 13 12 15]
[22 21 20 23]
[18 17 16 19]]
7.添加全是0和全是1的数组
import numpy as np
us_data = "./youtube_video_data/US_video_data_numbers.csv"
uk_data = "./youtube_video_data/GB_video_data_numbers.csv"
us_data = np.loadtxt(us_data,delimiter=",",dtype=int)
uk_data = np.loadtxt(uk_data,delimiter=",",dtype=int)
zeros_data = np.zeros((us_data.shape[0],1)).astype(int) #shape[0]表示行,1表示列
ones_data = np.ones((uk_data.shape[0],1)).astype(int)#shape[1]表示列
shape[0],shape[1]表示行和列
8.np.argmax(t,axis=0)#axis=0表示行
9.numpy生成随机数,np.random,np.random.seeds,随机种子,cv经常用到
创作不易,随意转载