1-1 数据导入和数组转置
np.loadtxt(framme,dtype='dataType',delimmiter='分隔符',skiprows=''(跳过的行数'),usecols=''需要用到的行数',unpack='Ture/Flase(是否转置)'
:加载文本文件数据
loadtxt参数意义.png
- numpy数组转置的是4种方法
- np.loadtxt中的参数unpack值设置为TRUE
- 使用数组的.T属性进行转置
- 使用数组的transpose()方法进行转置
- 使用numpy数组的swapaxes方法
实例如下:
import numpy as np filepath = './doubantop250.csv' t1 = np.loadtxt(filepath,usecols=(1,2,3),delimiter=',',dtype='float') print(t1) # 转置的四种方式 # first method:Set the value of parameter "unpack" —— True t2 = np.loadtxt(filepath,usecols=(1,2,3),delimiter=',',dtype='float',unpack=True) # second method: use the '.T' attributions of array's t3 = t1.T print(t3) # third method: use the method of 'transpose' t4 = t1.transpose() print(t4) # forth method: swapaxes(arguments:axes needed swapped) t5 = t1.swapaxes(0,1) print(t5)
运行结果:
运行结果.png
1-2 numpy数组索引与切片
import numpy as np filename = './doubantop250.csv' t1 = np.loadtxt(filename,delimiter=',',dtype='float',usecols=(1,2,3)) # print(t1) # 取行操作 print(t1[0]) print(t1[0,:]) # 取连续的多行 print(t1[3:]) print(t1[3:,:]) # 取不连续的多行 print(t1[[1,3,13,19]]) print(t1[[1,2,4,6],:]) # 取列 print(t1[:,0]) # 取连续的列 print(t1[:,2:]) # 取不连续的列 print(t1[:,[1,2]]) # 取第2-5行,2-3列 # 取多个位置的交叉数据 print(t1[1:5,1:3]) # 取不相邻的位置的数据信息 print(t1[[1,4,6],[0,1,2]])
import numpy as np filepath = './doubantop250.csv' t1 = np.loadtxt(filepath,delimiter=',',usecols=(1,2,3)) print(t1<9.5) t1[t1 < 9.5] = 0 print(t1[:,1]) # if-else操作 np.where(t1>=9.6,10,0) print(t1) # clip(m,n)把数组中小于m的替换成m,大于n的替换成n