Python常用库之一:Numpy

Numpy支持大量的维度数组和矩阵运算,对数组运算提供了大量的数学函数库!

Numpy简单创建数组

1 a = [1, 2, 3]
2 b = np.array(a)
3 c = np.array([[0, 1, 2, 10],
4               [12, 13, 100, 101],
5               [102, 110, 112, 113]], int)
6 print(c)
7 print(b)

创建数值为1的矩阵

Numpy.ones(shape, dtype=None, order='C')

1 array_one = np.ones([10, 10], dtype=np.int)
2 print(array_one)

创建数值为0的矩阵

Numpy.zeros(shape, dtype=None, order='C')

1 array_zero = np.zeros([10, 9], dtype=np.float)
2 print(array_zero)

Numpy查看数组属性

数组元素个数:b.size

数组形状:b.shape

数组维度:b.ndim

数组元素类型:b.dtype

# 数组元素个数:3
print(b.size)
# 数组形状:(3,)
print(b.shape)
# 数组维度:1
print(b.ndim)
# 数组元素类型:int32
print(b.dtype)

矩阵第一维的长度:shape[0]  # 行

矩阵第二维的长度:shape[1]  # 列

.......

1 array_rand = np.random.rand(10, 10, 4)
2 print(array_rand)
3 print(array_rand.ndim)
4 print(array_rand.shape[0])
5 print(array_rand.shape[1])
6 print(array_rand.shape[2])

Numpy创建随机数组(np.random

均匀分布

创建指定形状的数组,数值范围在0~1之间

1 array_rand = np.random.rand(10, 10, 4)
2 print(array_rand)
3 print(array_rand.ndim)

创建指定范围内的一个数:Numpy.random.uniform(low, high, size=None)

1 array_uniform = np.random.uniform(0, 100, size=5)
2 print(array_uniform)

创建指定范围的一个整数:Numpy.random.randint(low, high, size=None)

1 array_int = np.random.randint(0, 100, size=3)
2 print(array_int)
3 print(array_int.size)

正态分布

创建给定均值、标准差、维度的正态分布:Numpy.random.normal(loc, scale, size)

1 # 正态生成4行5列的二位数组
2 array_normal = np.random.normal(loc=1.75, scale=0.1, size=[4, 5])
3 print(array_normal)
4 print(array_normal.ndim)

Numpy数组操作

数组的索引

1 # 截取第0至第3行,第2至第4列(从第0行第0列算起)
2 after_array = array_normal[:3, 2:4]
3 print(after_array)

数组的改变

数组转置

1 array_normal.T

reshape():把指定的数组改变形状,但是元素个数不变;有返回值,即不对原始多维数组进行修改

 1 c = np.array([[[0, 1, 2],
 2                [10, 12, 13]],
 3               [[100, 101, 102],
 4                [110, 112, 113]]])
 5 """
 6 [[[  0   1]
 7   [  2  10]]
 8 
 9  [[ 12  13]
10   [100 101]]
11 
12  [[102 110]
13   [112 113]]]
14 """
15 print(c.reshape(3, 2, 2))
16 """
17 [[  0   1   2  10]
18  [ 12  13 100 101]
19  [102 110 112 113]]
20 """
21 # 某一维指定为-1时,自动计算维度
22 print(c.reshape(3, -1))
23 """[[[  0   1]
24     [  2  10]
25     [ 12  13]]
26     
27     [[100 101]
28     [102 110]
29     [112 113]]]"""
30 print(c.reshape(2, -1, 2))

resize():把指定的数组改变形状,但是元素个数可变,不足补0;无返回值,即对原始多维数组进行修改

 1 a = np.array([[[0, 1, 2],
 2                [10, 12, 13]],
 3               [[100, 101, 102],
 4                [110, 112, 113]]])
 5 b = np.array([[[0, 1, 2],
 6                [10, 12, 13]],
 7               [[100, 101, 102],
 8                [110, 112, 113]]])
 9 '''[[0]
10     [1]
11     [2]]'''
12 a.resize((3, 1))
13 '''[[  0   1   2  10  12]
14     [ 13 100 101 102 110]
15     [112 113   0   0   0]]'''
16 b.resize((3, 5))
17 print(a)
18 print(b)

*Numpy计算

条件运算

Numpy.where(condition, x, y):三目运算满足condition,为x;不满足condition,则为y

1 score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
2 # 如果数值小于80,替换为0,如果大于等于80,替换为90
3 re_score = np.where(score < 80, 0, 90)
4 print(re_score)

统计运算

指定轴最大值:amax(参数1:数组;参数2:axis=0/1,0表示行1表示列)

1 # 求整个矩阵的最大值
2 result = np.amax(score)
3 print(result)
4 # 求每一列的最大值(0表示列)
5 result = np.amax(score, axis=0)
6 print(result)
7 # 求每一行的最大值(1表示行)
8 result = np.amax(score, axis=1)
9 print(result)

指定轴最小值:amin(参数1:数组;参数2:axis=0/1,0表示行1表示列)

1 # 求整个矩阵的最小值
2 result = np.amin(score)
3 print(result)
4 # 求每一列的最小值(0表示列)
5 result = np.amin(score, axis=0)
6 print(result)
7 # 求每一行的最小值(1表示行)
8 result = np.amin(score, axis=1)
9 print(result)

指定轴平均值:mean(参数1:数组;参数2:axis=0/1,0表示行1表示列;参数3:dtype,输出数据类型)

1 # 求整个矩阵的平均值
2 result = np.mean(score, dtype=np.int)
3 print(result)
4 # 求每一列的平均值(0表示列)
5 result = np.mean(score, axis=0)
6 print(result)
7 # 求每一行的平均值(1表示行)
8 result = np.mean(score, axis=1)
9 print(result)

指定轴方差:std(参数1:数组;参数2:axis=0/1,0表示行1表示列;参数3:dtype,输出数据类型)

1 # 求整个矩阵的方差
2 result = np.std(score)
3 print(result)
4 # 求每一列的方差(0表示列)
5 result = np.std(score, axis=0)
6 print(result)
7 # 求每一行的方差(1表示行)
8 result = np.std(score, axis=1)
9 print(result)

数组运算

数组与数的运算(加、减、乘、除、取整、取模)

 1 # 循环数组行和列,每一个数值都加5
 2 score[:, :] = score[:, :]+5
 3 print(score)
 4 # 循环数组行和列,每一个数值都减5
 5 score[:, :] = score[:, :]-5
 6 print(score)
 7 # 循环数组行和列,每一个数值都乘以5
 8 score[:, :] = score[:, :]*5
 9 print(score)
10 # 循环数组行和列,每一个数值都除以5
11 score[:, :] = score[:, :]/5
12 print(score)
13 # 循环数组行和列,每一个数值除以5取整
14 score[:, :] = score[:, :] // 5
15 print(score)
16 # 循环数组行和列,每一个数值除以5取模
17 score[:, :] = score[:, :] % 5
18 print(score)

数组间运算(加、减、乘、除),前提是两个数组的shape一样

1 c = score + score
2 d = score - score
3 e = score * score
4 # 分母数组保证每个数值不能为0
5 b = score / score

矩阵运算(一种特殊的二维数组)

计算规则

(M行,N列)*(N行,Z列)=(M行,Z列)

1 st_score = np.array([[80, 88], [82, 81], [84, 75], [86, 83], [75, 81]])
2 # 平时成绩占40% 期末成绩占60%, 计算结果
3 q = np.array([[0.4], [0.6]])
4 result = np.dot(st_score, q)
5 print(result)

矩阵拼接

矩阵垂直拼接(前提两个两个矩阵列数相同,行数随意):vstack(参数:tuple)

1 v1 = [[0, 1, 2, 3, 4, 5],
2       [6, 7, 8, 9, 10, 11]]
3 v2 = [[12, 13, 14, 15, 16, 17],
4       [18, 19, 20, 21, 22, 23],
5       [18, 19, 20, 21, 22, 23]]
6 result = np.vstack((v1, v2))
7 print(result)

矩阵水平拼接(前提两个两个矩阵行数相同,列数随意):hstack(参数:tuple)

1 v1 = [[0, 1, 2, 3, 4, 5],
2       [6, 7, 8, 9, 10, 11]]
3 v2 = [[12, 13, 14, 15, 16, 17],
4       [18, 19, 20, 21, 22, 23]]
5 result = np.hstack((v1, v2))
6 print(result)

猜你喜欢

转载自www.cnblogs.com/reaptomorrow-flydream/p/9173161.html