创建数组
数据类型与操作数组形状数组的计算 读取本地数据和索引
读取数据索引和切片数值修改三元运算符 numpy中的nan和常用统计方法
NAN常用统计方法
缺失值均值填充 数组的拼接与交换
拼接交换 numpy生成随机数需要注意的点
创建数组一个在Python中做科学计算的基础库,重在数值计算,也是大部分PYTHON科学计算库的基础库,多用于在大型、多维数组上执行数值运算
创建数组有以下三种方式:
1、array([1,2,3,4,5,])
2、array(range(10))
3、arange(10)
方法2、3一样
数组的类名为:
查看数据类型为(以代码为例):
t3.dtype
import numpy as np t1 = np.array([1,2,3,4,5,]) print(t1) print(type(t1)) t2 = np.array(range(10)) print(t2) t3 = np.arange(10) print(t3) print(t3.dtype) #结果为: [1 2 3 4 5]数据类型与操作[0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9] int32
创建数组类型时,有两种方法:
1、dtype=np.int8,即后面为:np.数据类型
2、dtype=‘i1’,即直接用数据类型的代码
调整数据类型:
import numpy as np
t1 = np.array([1,2,3,4,5,])
print(t1)
print(type(t1))
t5 = t1.astype('f')
print(t5)
#结果为:
[1. 2. 3. 4. 5.]
小数位数:
t6 = np.array([random.random() for i in range(5)]) print(t6) print(t6.dtype) t8 = np.round(t6,3) print(t8) #结果为: [0.08225965 0.91074882 0.70192436 0.98622247 0.29171038] float64 [0.082 0.911 0.702 0.986 0.292]数组形状
t = np.array([[1,2,3],[4,5,6]]) t1 = t.reshape(3,2) print(t) print() print(t1) #结果为: [[1 2 3] [4 5 6]] [[1 2] [3 4] [5 6]]
reshape是创建了一个新的数组,因此t无改变。
# 三维数组 t = np.array([[1,2,3],[4,5,6]]) t1 = t.reshape(2,1,3) print(t) print() print(t1) #结果为: [[1 2 3] [4 5 6]] [[[1 2 3]] [[4 5 6]]]
维数:
t = np.array(range(5)) # 当元组内只有一个数时,为一维数组 t1 = t.reshape((5,)) t2 = t.reshape((5,1)) t3 = t.reshape((1,5)) print(t1) print(t2) print(t3) #结果为: [0 1 2 3 4] [[0] [1] [2] [3] [4]] [[0 1 2 3 4]]
一维:
flatten():展开成一维
t = np.array(range(6)) t1 = t.reshape(2,3) print(t1) t2 = t1.flatten() print(t2) #结果为: [[0 1 2] [3 4 5]] [0 1 2 3 4 5]数组的计算
数组和数的计算:
数组和形状相同的数组的计算:
t1 = np.array([[1,2,3],[4,5,6]]) t2 = np.array(([1,1,1],[5,6,4])) print(t1+t2) print(t1*t2) #结果为: [[ 2 3 4] [ 9 11 10]] [[ 1 2 3] [20 30 24]]
数组和形状不同的数组的计算:
二维数组的轴:
三维数组的轴:
即:有行或列一样时一定可以计算
t1 = np.array([[1,2,3],[4,5,6]]) print(t1) print() t2 = np.array([[1],[2]]) print(t2) print() print(t1+t2) print() print(t1*t2) #结果为: [[1 2 3] [4 5 6]] [[1] [2]] [[2 3 4] [6 7 8]] [[ 1 2 3] [ 8 10 12]]
例:
shape为(3,3,3)的数组不能够和(3,2)的数组进行计算
shape为(3,3,2)的数组能够和(3,2)的数组进行计算
shape为(3,3,2)的数组不能够和(3,3)的数组进行计算
sum:
t = np.array([i for i in range(24)]) t1 = t.reshape(4,6) print(t1) t2 = np.sum(t1,axis=0) t3 = np.sum(t1,axis=1) print() print(t2) print() print(t3) #结果为: [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] [36 40 44 48 52 56] [ 15 51 87 123]读取本地数据和索引 读取数据
np.loadtxt(fname,dtype=np.float,delimiter=None,skiprows=0,usecols=None,unpack=False)
转置:
t为数组,t.transpose()、t.T、t.swapaxes()
t.swapaxes()中要加入轴参数,即t.swapaxes(1,0),将0轴与1轴交换
取不连续的多行时:
a[[1,3,5]],即为取2、4、6行,因为只能传送一个数据,我们以列表形式传输。
取不连续的多列时:
a[:,[0,2]],即为取每一行的第一列和第三列。
取行列交叉点的结果:
a[2:5,1:4],逗号前为行,后为列。
去多个不相邻的点:
a[[0,2,5],[1,5,3]],选出来的位置为(0,1),(2,5),(5,3)
重新赋值即可:
将小于10的值变为3:
t[t<10]=3
t = np.array([random.random() for i in range(24)]) t1 = t.reshape(4,6) t2 = np.round(t1,2) print(t2) t2[t2<0.5] = 0 print(t2) #结果为: [[0.99 0.01 0.09 0.97 0.17 0.38] [0.22 1. 0.3 0.92 0.83 0.73] [0.09 0.6 0.04 0.39 0.87 0.67] [0.21 0.31 0.68 0.64 0.66 0.13]] [[0.99 0. 0. 0.97 0. 0. ] [0. 1. 0. 0.92 0.83 0.73] [0. 0.6 0. 0. 0.87 0.67] [0. 0. 0.68 0.64 0.66 0. ]]三元运算符
where:
t小于10时为0,否则为10。
clip:
小于10的替换成10,大于18的替换成18,但nan不会被替换。
nan(NAN,Nan):not a number表示不是一个数字
什么时候numpy中会出现nan:
当我们读取本地的文件为float的时候,如果有缺失,就会出现nan
当做了一个不合适的计算的时候(比如无穷大(inf)减去无穷大)
inf(-inf,inf):infinity,inf表示正无穷,-inf表示负无穷
什么时候回出现inf包括(-inf,+inf)
比如一个数字除以0,(python中直接会报错,numpy中是一个inf或者-inf)
上面两个的类型均为float。
注:
注:
单纯的把nan替换为0,如果替换之前的平均值如果大于0,替换之后的均值肯定会变小,因此,更一般的方式是把缺失的数值替换为均值(中值)或者是直接删除有缺失值的一行。
求和:t.sum(axis=None)
均值:t.mean(a,axis=None) 受离群点的影响较大
中值:np.median(t,axis=None)
最大值:t.max(axis=None)
最小值:t.min(axis=None)
极值:np.ptp(t,axis=None) 即最大值和最小值只差
标准差:t.std(axis=None)
def demo(t1):
for i in range(t1.shape[1]):
temp_col = t1[:,i]
num = np.count_nonzero(temp_col != temp_col)
if num != 0:
not_nan = temp_col[temp_col == temp_col]
temp_col[np.isnan(temp_col)] = not_nan.mean()
return t1
if __name__ == '__main__':
# 创建一个有nan的数组
t = np.array([i for i in range(24)])
t1 = t.reshape(4, 6)
t1 = t1.astype("float") # 必须要转化为浮点型才可以将数值换为nan,因为nan的type为float
t1[1, 4:] = np.nan
print(t1)
print()
t = demo(t1)
print(t)
#结果为:
[[ 0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. nan nan]
[12. 13. 14. 15. 16. 17.]
[18. 19. 20. 21. 22. 23.]]
[[ 0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 14. 15.]
[12. 13. 14. 15. 16. 17.]
[18. 19. 20. 21. 22. 23.]]
数组的拼接与交换
拼接
交换
numpy生成随机数
np.random.seed(10) t = np.random.randint(1,20,(3,4)) print(t) #结果为: [[10 5 16 1] [18 17 18 9] [10 1 11 9]]需要注意的点
1、数组中的广播机制
# 创建一个有nan的数组
t = np.array([i for i in range(24)])
t1 = t.reshape(4,6)
print(t1)
t1 = t1.astype("float") # 必须要转化为浮点型才可以将数值换为nan,因为nan的type为float
t1[1,4:] = np.nan
print(t1)
# 取出有nan的那一行
t3 = t1[1,:]
# 取出这一行不为nan的值
not_nan = t3[t3 == t3]
print(not_nan)
#结果为:
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
[[ 0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. nan nan]
[12. 13. 14. 15. 16. 17.]
[18. 19. 20. 21. 22. 23.]]
[6. 7. 8. 9.]
7.5
即not_nan = t3[t3 == t3]表示这一行的每个数值都会进行该操作,最后得到一个满足条件的列表,而并非只将其中的某个数进行该操作。
2、将数值变为nan时需要先将数值变为float型,否则结果不对。
3、小结(学完这一章节需要了解的地方):
4、一个例子(numpy与matplotlib)
GB_file_path = 'GB_video_data_numbers.csv' t_uk = np.loadtxt(GB_file_path,delimiter=',',dtype='int') t_uk = t_uk[t_uk[:,1]<=500000] t_uk_comment = t_uk[:,-1] t_uk_like = t_uk[:,1] plt.figure(figsize=(10,5),dpi=80) plt.scatter(t_uk_like,t_uk_comment) plt.show()
#结果为:
5、更多方法
获取最大值最小值的位置
np.argmax(t,axis=0)
np.argmin(t,axis=1)创建一个全0的数组: np.zeros((3,4))创建一个全1的数组:np.ones((3,4))创建一个对角线为1的正方形数组(方阵):np.eye(3)
6、copy和view
a=b 完全不复制,a和b相互影响a = b[:],视图的操作,一种切片,会创建新的对象a,但是a的数据完全由b保管,他们两个的数据变化是一致的a = b.copy(),复制,a和b互不影响



