Numpy函数总结
基础属性
- 引入模块
>>> import numpy as np
- 创建一个list并转化为numpy数组
# 创建简单的列表
>>> a = [1, 2, 3, 4]
# 将列表转换为数组
>>> b = np.array(b)
- 查看数组元素个数,输出数组中全部元素的个数
>>> b.size()
5
- 查看数组形状
>>> b.shape
(5,)
>>> np.random.rand(10,10,10,10).shape
(10, 10, 10, 10)
- 查看数组维度
>>> b.ndim
1
>>> np.random.rand(10,10,10,10).ndim
4
- 查看数组元素类型
>>> b.dtype
dtype('int32')
>>> np.random.rand(10,10,10,10).dtype
dtype('float64')
快速创建数组
- 创建10行10列的数值为***浮点***1的矩阵
>>> array_one = np.ones([10, 10])
- 创建10行10列的数值为***浮点***0的矩阵
>>> array_zero = np.zeros([10, 10])
- 从现有的数据创建数组
# 用array拷贝np数组时会使用浅拷贝
>>> c=np.array(b)
>>> c
array([0, 2, 3, 4, 5])
>>> b[0]=111
>>> c
array([0, 2, 3, 4, 5])
# 用asarray拷贝np数组时会使用浅拷贝
>>> c=np.asarray(b)
>>> c
array([10, 2, 3, 4, 5])
>>> b[0]=0
>>> c
array([0, 2, 3, 4, 5])
# 但是在拷贝list时用asarray也会发生数据拷贝
>>> b = np.asarray(a)
>>> b
array([5, 2, 3, 4, 5])
>>> a[0]=10
>>> b
array([5, 2, 3, 4, 5])
- 创建随机分布的数组
>>> np.random.rand(10, 10)#创建指定形状(示例为10行10列)的数组(范围在0至1之间)
array([[0.69333313, 0.61543411, 0.24352898],
[0.98936748, 0.80206565, 0.89985464],
[0.63651806, 0.50788342, 0.90844882]])
>>> np.random.uniform(0, 100)#创建指定范围内的一个数
31.927113606173563
>>> np.random.randint(0, 100)#创建指定范围内的一个整数
76
- 创建正态分布数组
>>> np.random.normal(1.75, 0.1, (2, 3))#给定均值/标准差/维度
- 数组的索引与切片
# 正态生成4行5列的二维数组
>>> arr = np.random.normal(1.75, 0.1, (4, 5))
>>> arr
array([[1.74553912, 1.7578923 , 1.6917525 , 1.62288087, 1.69857024],
[1.68818903, 1.74662893, 1.85876854, 1.85899816, 1.74549678],
[1.77705314, 1.66302891, 1.67503608, 1.74476541, 1.90163481],
[1.77360089, 1.96398122, 1.91652254, 1.66228769, 1.8457283 ]])
# 截取第1至2行的第2至3列(从第0行算起)
>>> after_arr = arr[1:3, 2:4]
>>> after_arr
array([[1.85876854, 1.85899816],
[1.67503608, 1.74476541]])
- 改变数组形状
>>> a = np.random.normal(1.75, 0.1, (1, 10))
array([[1.68214847, 1.8638204 , 1.7543202 , 1.64583187, 1.6624949 ,
1.6476275 , 1.6107775 , 1.79918245, 1.7978823 , 1.76299561]])
>>> a.reshape([2,5])
array([[1.68214847, 1.8638204 , 1.7543202 , 1.64583187, 1.6624949 ],
[1.6476275 , 1.6107775 , 1.79918245, 1.7978823 , 1.76299561]])
Numpy计算
1. 条件运算
- 布尔运算
>>> a = np.random.normal(1.75, 0.1, (3, 4))
>>> a
array([[1.60478283, 1.81222661, 1.51358821, 1.49465472],
[1.75107931, 1.81525897, 1.59777299, 1.69916906],
[1.70823284, 1.6959937 , 1.69148525, 1.75496314]])
>>> a > 1.5
array([[ True, True, True, False],
[ True, True, True, True],
[ True, True, True, True]])
- 三目运算
# 如果数值小于1.5,则替换为0,若不是则替换为1.5
>>> np.where(a<1.5,0,1.5)
array([[1.5, 1.5, 1.5, 0. ],
[1.5, 1.5, 1.5, 1.5],
[1.5, 1.5, 1.5, 1.5]])
# 若只有条件,则输出符合条件的坐标
>>> a = np.arange(27).reshape(3,3,3)
>>> a
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],
[24, 25, 26]]])
>>> np.where(a > 5)
# 将三个数组中相同位的数值组合则可获得符合条件的位
# 如符合条件的位分别为[0][2][0]、[0][2][1]。。。。。。
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]),
array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]))
# 遍历符合条件的元素
>>> result = np.where(a > 5)
>>> print(result)
>>> for i in range(result[0].size):
... print(a[result[0][i]][result[1][i]][result[2][i]])
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2],
dtype=int64), array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2],
dtype=int64), array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2],
dtype=int64))
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2.统计运算
求每一轴-
最大值用:amax()
最小值用:amin()
平均值用:mean()
求方差用:std()
import numpy as np
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 求每一列的最大值(0表示列)
print("每一列的最大值为:")
result = np.amax(stus_score, axis=0)
print(result)
print("每一行的最大值为:")
result = np.amax(stus_score, axis=1)
print(result)
Output:
每一列的最大值为:
[86 88]
每一行的最大值为:
[88 82 84 86 81]
3. 数组运算
- 数组与数的运算
import numpy as np
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
print("加分前:")
print(stus_score)
# 为所有平时成绩都加5分
# 此处索引的意义为:第一维的所有数据的第0个数据
# Python的索引有两种写法:array[x][y][z]和array[x,y,z],两者等价
stus_score[:, 0] = stus_score[:, 0]+5
print("加分后:")
print(stus_score)
- 数组间运算
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([10, 20, 30, 40])
c = a + b
d = a - b
e = a * b
f = a / b
print("a+b为", c)
print("a-b为", d)
print("a*b为", e)
print("a/b为", f)
Output:
a+b为 [11 22 33 44]
a-b为 [ -9 -18 -27 -36]
a*b为 [ 10 40 90 160]
a/b为 [0.1 0.1 0.1 0.1]
4.矩阵运算(np.dot())
- 计算规则:
(M行, N列) * (N行, Z列) = (M行, Z列)
import numpy as np
stus_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
# 平时成绩占40% 期末成绩占60%, 计算结果
q = np.array([[0.4], [0.6]])
result = np.dot(stus_score, q)
print("最终结果为:")
print(result)
Output:
最终结果为:
[[84.8]
[81.4]
[78.6]
[84.2]
[78.6]]
- 矩阵拼接
垂直拼接:
import numpy as np
print("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.vstack((v1, v2))
print("v1和v2垂直拼接的结果为")
print(result)
Output:
v1为:
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
v2为:
[[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
v1和v2垂直拼接的结果为
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
水平拼接:
import numpy as np
print("v1为:")
v1 = [[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11]]
print(v1)
print("v2为:")
v2 = [[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]
print(v2)
# 垂直拼接
result = np.hstack((v1, v2))
print("v1和v2水平拼接的结果为")
print(result)
Output:
v1为:
[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
v2为:
[[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]
v1和v2水平拼接的结果为
[[ 0 1 2 3 4 5 12 13 14 15 16 17]
[ 6 7 8 9 10 11 18 19 20 21 22 23]]
5.Numpy读取数据(np.genfromtxt())
该函数可以从csv中读取文件
如果数值据有无法识别的值出现,会以nan显示,nan相当于np.nan,为float类型
data.csv:
1,2,3,4,5
2,3,4,,6
2,1,1,1,3
2,3,1,2,5
import numpy as np
print(np.genfromtxt('data.csv',delimiter=","))
Output:
[[ 1. 2. 3. 4. 5.]
[ 2. 3. 4. nan 6.]
[ 2. 1. 1. 1. 3.]
[ 2. 3. 1. 2. 5.]]