本节知识:
1、numpy读取数据
2、索引、切片的使用
3、numpy中三元运算符
4、numpy中的clip(裁剪)
1、numpy读取数据
由于csv便于展示,读取和写入,所以很多地方也是用csv的格式存储和传输中小型的数据,为了方便教学,我们使用csv格式的文件(当然,操作数据库中的数据也是很容易的实现的)
方法:loadtxt
csv文件大致内容如下图:
读取文件
#numpy读取数据
GB_path = 'data/GB_video_data_numbers.csv'
GB_data = np.loadtxt(GB_path,delimiter=',') #delimiter:以‘,’分隔
print(GB_data)
输出:
[[7.426393e+06 7.824000e+04 1.354800e+04 7.050000e+02]
[4.942030e+05 2.651000e+03 1.309000e+03 0.000000e+00]
[1.428190e+05 1.311900e+04 1.510000e+02 1.141000e+03]
...
[1.092220e+05 4.840000e+03 3.500000e+01 2.120000e+02]
[6.262230e+05 2.296200e+04 5.320000e+02 1.559000e+03]
[9.922800e+04 1.699000e+03 2.300000e+01 1.350000e+02]]
这里可以看到输出数值为科学计数,可通过dtype指定数据类型
#numpy读取数据
GB_path = 'data/GB_video_data_numbers.csv'
GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int) #delimiter:以‘,’分隔
print(GB_data)
输出:
[[7426393 78240 13548 705]
[ 494203 2651 1309 0]
[ 142819 13119 151 1141]
...
[ 109222 4840 35 212]
[ 626223 22962 532 1559]
[ 99228 1699 23 135]]
跳过前几行,skiprows
#numpy读取数据
GB_path = 'data/GB_video_data_numbers.csv'
GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int) #delimiter:以‘,’分隔
print(GB_data)
print('*'*100)
GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int,skiprows=2) #delimiter:以‘,’分隔 skiprows:跳过前2行
print(GB_data)
输出:
[[7426393 78240 13548 705]
[ 494203 2651 1309 0]
[ 142819 13119 151 1141]
...
[ 109222 4840 35 212]
[ 626223 22962 532 1559]
[ 99228 1699 23 135]]
****************************************************************************************************
[[ 142819 13119 151 1141]
[1580028 65729 1529 3598]
[ 40592 5019 57 490]
...
[ 109222 4840 35 212]
[ 626223 22962 532 1559]
[ 99228 1699 23 135]]
读取第几列,usecols
#numpy读取数据
GB_path = 'data/GB_video_data_numbers.csv'
GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int) #delimiter:以‘,’分隔
print(GB_data)
print('*'*100)
# GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int,skiprows=2) #delimiter:以‘,’分隔 skiprows:跳过前2行
GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int,usecols=(1,2)) #usecols:读取1、2列
print(GB_data)
输出:
[[7426393 78240 13548 705]
[ 494203 2651 1309 0]
[ 142819 13119 151 1141]
...
[ 109222 4840 35 212]
[ 626223 22962 532 1559]
[ 99228 1699 23 135]]
****************************************************************************************************
[[78240 13548]
[ 2651 1309]
[13119 151]
...
[ 4840 35]
[22962 532]
[ 1699 23]]
unpack:默认False(0) 即有多少数据就有多少行,为True(1)时,每一列数据会组成一行,原始数据有多少列就会转成多少行,相当于转置(转置是一种变换,对于numpy中的数组来说,就是在对角线方向交换数据,即行变成列,列变成行,目的也是为了更方便的去处理数据)
#numpy读取数据
GB_path = 'data/GB_video_data_numbers.csv'
GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int) #delimiter:以‘,’分隔
print(GB_data)
print('*'*100)
# GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int,skiprows=2) #delimiter:以‘,’分隔 skiprows:跳过前2行
# GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int,usecols=(1,2)) #usecols:读取1、2列
GB_data = np.loadtxt(GB_path,delimiter=',',dtype=int,unpack=True) #unpack:转置
print(GB_data)
输出:
[[7426393 78240 13548 705]
[ 494203 2651 1309 0]
[ 142819 13119 151 1141]
...
[ 109222 4840 35 212]
[ 626223 22962 532 1559]
[ 99228 1699 23 135]]
****************************************************************************************************
[[7426393 494203 142819 ... 109222 626223 99228]
[ 78240 2651 13119 ... 4840 22962 1699]
[ 13548 1309 151 ... 35 532 23]
[ 705 0 1141 ... 212 1559 135]]
补充:
这里既然已经提到了转置,就顺便说一下numpy中转置的三种方法:swapaxes、transpose,T
#转置
a = np.array([[1,2,3],[4,5,6]])
print(a)
print('*'*100)
print(a.swapaxes(1,0)) #1、0表示轴
print('*'*100)
print(a.transpose())
print('*'*100)
print(a.T)
输出:
[[1 2 3]
[4 5 6]]
****************************************************************************************************
[[1 4]
[2 5]
[3 6]]
****************************************************************************************************
[[1 4]
[2 5]
[3 6]]
****************************************************************************************************
[[1 4]
[2 5]
[3 6]]
2、索引、切片的使用
2.1、numpy索引和切片
其实操作很简单,和python中列表的操作一样(切片取一部分,索引取一个值)
取一行、一列
#索引
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
print('*'*100)
print(a[1]) #取一行
print('*'*100)
print(a[:,2]) #取一列
输出:
[[1 2 3]
[4 5 6]
[7 8 9]]
****************************************************************************************************
[4 5 6]
****************************************************************************************************
[3 6 9]
取多行、多列
#索引
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
print('*'*100)
print(a[1:3]) #取多行
print('*'*100)
print(a[:,1:3]) #取多列
输出:
[[1 2 3]
[4 5 6]
[7 8 9]]
****************************************************************************************************
[[4 5 6]
[7 8 9]]
[[2 3]
[5 6]
[8 9]]
#索引
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
print('*'*100)
print(a[[1,2],:]) #取多行
print('*'*100)
print(a[:,[1,2]]) #取多列
输出:
[[1 2 3]
[4 5 6]
[7 8 9]]
****************************************************************************************************
[[4 5 6]
[7 8 9]]
****************************************************************************************************
[[2 3]
[5 6]
[8 9]]
2.2、numpy中数值的修改
简单的赋值,和python赋值一样
#numpy中数值的修改
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a[:,[1,2]] = 0
print(a)
输出:
[[1 0 0]
[4 0 0]
[7 0 0]]
为赋值添加条件
#numpy中数值的修改
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a>3)
a[a>3] = 0
print(a)
输出:
[[False False False]
[ True True True]
[ True True True]]
[[1 2 3]
[0 0 0]
[0 0 0]]
3、numpy中三元运算符
方法:where
#numpy中数值的修改
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(np.where(a>3,0,10)) #大于3赋值0,否则赋值10
输出:
[[10 10 10]
[ 0 0 0]
[ 0 0 0]]
4、numpy中的clip(裁剪)
a = np.array([[1,2,3],[np.nan,4,5]])
print(a)
print('*'*100)
print(a.clip(10,18)) #小于10的替换为10,大于18的替换为18,nan不能替换
输出:
[[ 1. 2. 3.]
[nan 4. 5.]]
****************************************************************************************************
[[10. 10. 10.]
[nan 10. 10.]]