文档 https://numpy.org/doc/stable/index.html
下载
pip install numpy
导入
import numpy as np
ndarray比python原生list运算快很多
创建数组
-
直接创建
a=np.array([1,2,3,4]) a >array([1, 2, 3, 4]) a.sharp
(4,) # 4行1列
-
指定值和步长(前闭后开)
d=np.arange(0,1,0.1) d
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
-
指定值和元素个数(等差数列)
e=np.linspace(0,1,10) e
array([0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ]) -
指定值和元素个数(等比数列)
#从1到100 f=np.logspace(0,2,10) f
array([ 1. , 1.66810054, 2.7825594 , 4.64158883,
7.74263683, 12.91549665, 21.5443469 , 35.93813664,
59.94842503, 100. ])
轴
一维数组只有一个0轴
可指定计算某个轴方向上的数字
a = np.array([[1,2,3],[4,5,6]])
print(np.sum(a,axis=0))
print(np.sum(a,axis=1))
[5 7 9]
[ 6 15]
a=np.arange(27).reshape((3,3,3))
print(a)
print("-"*20)
b=np.sum(a, axis=0)
print(b)
print("-"*20)
c=np.sum(a, axis=1)
print(c)
print("-"*20)
d=np.sum(a, axis=2)
print(d)
数据格式
Int8 等于Byte, 占1个字节.
Int16 意思是16位整数(16bit integer),相当于short 占2个字节
Int32 意思是32位整数(32bit integer), 相当于 int 占4个字节
Int64 意思是64位整数(64bit interger), 相当于 long 占8个字节
import random
f = np.array([1,2,3,4,5], dtype = np.int16) # dtype设置数据类型,默认为int32
# 返回数组中每个元素的字节单位长度
print(f.itemsize)
# 获取数据类型
print(f.dtype)
# 调整数据类型
f1 = f.astype(np.int64)
print(f1.dtype)
print(f1.itemsize)
# 随机生成小数
# round()方法返回浮点数四舍五入的值
print(round(random.random(),2))
arr = np.array([random.random() for i in range(10)])
print(arr)
print(arr.itemsize)
print(arr.dtype)
# 取小数点后两位
print(np.round(arr,2))
2
int16
int64
8
0.71
[0.65654548 0.6164441 0.28577322 0.5186117 0.71748233 0.79910097
0.50977869 0.87602431 0.58216741 0.39614424]
8
float64
[0.66 0.62 0.29 0.52 0.72 0.8 0.51 0.88 0.58 0.4 ]
nan与inf
inf 表示无穷大,需要使用 float(‘inf’) 函数来转化,对应的有float(’-inf’) 表示无穷小。
当一个数字除以0,Python中会报错,但是numpy中会用inf或者-inf表示
nan,常用来表示缺失的数据。任何与其做运算结果都是 nan
a = np.nan
b = np.inf
print(a,type(a))
print(b,type(b))
nan <class ‘float’>
inf <class ‘float’>
-
判断nan的个数
可以使用np.count_nonzero()
来判断非零的个数
np.nan != np.nan
结果是TRUE
,可以结合使用判断nan的个数t = np.arange(24,dtype=float).reshape(4,6) t[3,4] = np.nan print(np.count_nonzero(t)) print(t!=t) print(np.count_nonzero(t != t))
23
[[False False False False False False]
[False False False False False False]
[False False False False False False]
[False False False False True False]]
1 -
将nan替换为0
t[np.isnan(t)] = 0 print(t)
[[ 0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10. 11.]
[12. 13. 14. 15. 16. 17.]
[18. 19. 20. 21. 0. 23.]]
例:
处理数据中的nan值
全部替换为0会对结果造成较大的影响,一般替换为均值(中值)或直接删掉有缺失值的一行
t[1,3:] = np.nan
print(t)
print('-------------------')
print(t.shape)
# 遍历每一列,然后判断每一列是否有nan
for i in range(t.shape[1]):
#获取当前列数据
temp_col = t[:,i]
# 判断当前列的数据中是否含有nan
nan_num = np.count_nonzero(temp_col != temp_col)
# 条件成立说明含有nan
if nan_num != 0:
# 将这一列不为nan的数据拿出来
temp_col_not_nan = temp_col[temp_col == temp_col]
# 将nan替换成这一列的平均值
temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_nan)
print(t)
[[ 0. 1. 2. 3. 4. 5.]
[ 6. 7. 8. nan nan nan]
[12. 13. 14. 15. 16. 17.]
[18. 19. 20. 21. 0. 23.]]
-------------------
(4, 6)
[[ 0. 1. 2. 3. 4. 5. ]
[ 6. 7. 8. 13. 6.66666667 15. ]
[12. 13. 14. 15. 16. 17. ]
[18. 19. 20. 21. 0. 23. ]]
运算
-
使用np.dot()计算矩阵乘积
a=np.arange(12).reshape