numpy中定义数组和数组的属性
- 定义数组
- 代码
import numpy as np
array = np.array([[1,2,3],
[2,3,4])
print(array)
- 输出结果
[[1 2 3]
[2 3 4]]
//(输出结果中无逗号)
- 显示数组维度
- 代码
print('number of dim:'array.ndim)
- 输出结果
number of dim: 2
- 显示数组形状
- 代码
print('shape:', array.shape)
- 输出结果
shape: (2,3)
- 显示数组大小
- 代码
print('size:', array.size)
- 输出结果
size: 6
numpy中数组值的设置
- 设置和显示数据类型
- 代码(不设置输出数据类型的长度时)
import numpy as np
a = np.array([2,23,4], dtype=np.int)
print(a.dtype)
- 输出结果
int64
//(不输入数据长度时默认为64位)
- 代码(设置输出数据类型的长度时)
import numpy as np
a = np.array([2,23,4], dtype=np.int32)
print(a.dtype)
- 输出结果
int32
- 若需要存储空间小,则设置小的数据长度(e.g.16位);
- 若需要存储空间大,则设置大的数据长度(e.g. 64位)
- 创建全0矩阵、全1矩阵和空矩阵
(1)全0矩阵:
- 代码
a = np.zero( (3,4) )
print(a)
- 输出结果
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
(2)全1矩阵:
- 代码
a = np.ones( (3,4), dtype=np.int16 )
print(a)
- 输出结果
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
(3)空矩阵:
- 代码
a = np.empty( (3,4) )
print(a)
- 输出结果
[[6.23042070e-307 1.89146896e-307 1.37961302e-306 1.05699242e-307]
[8.01097889e-307 1.78020169e-306 7.56601165e-307 1.02359984e-306]
[1.29060531e-306 1.24611741e-306 1.11261027e-306 3.01468662e+161]]
//(是十分接近0的数)
- 生成有序序列或矩阵(相当于python中的range功能)
- 代码
a = np.arange(10,20,2)
print(a)
b = np.arange(12).reshape((3,4))
print(b)
- 输出结果
[10 12 14 16 18]
[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]]
- 创建linspace()
- 代码
a = np.linspace(1,10,5)
print(a)
b = np.linspace(1,10,6).reshape((2,3))
print(b)
- 输出结果
[1. 3.25 5.5 7.75 10.]
[[1. 2.8 4.6]
[6.4 8.2 10.]]
numpy的运算
- 加减法运算
- 代码
import namoy as np
a = np.array([10,20,30,40])
b = np.arange(4)
print(a,b)
c = a+b
print(c)
- 输出结果
[10 20 30 40] [0 1 2 3]
[10 21 32 43]
- 平方运算:
- 代码
c = b**2
print(c)
- 输出结果:
[0 1 4 9]
- sin/cos:
c = 10*np.cos(a)
print(c)
- 输出结果:
[-8.39071529 4.08082062 1.5425145 -6.66938062]
- 判断序列中数值大小是否符合条件:
- 代码
print(b)
print(b==3)
- 输出结果:
[0 1 2 3]
[False False False True]
- 矩阵乘法:
(1)逐个相乘:
- 代码
a = np.array([[1,1],
[0,1]])
b = np.arrange(4).reshape(2,2)
c = a*b
print(c)
- 输出结果:
[[0 1]
[0 3]]
(2)矩阵相乘:
- 代码
a = np.array([[1,1],
[0,1]])
b = np.arrange(4).reshape(2,2)
c_dot = np.dot(a,b)
//或:c_dot = a.dot(b)
print(c_dot)
- 输出结果:
[[2 4]
[2 3]
- 求和
- 代码
import numpy as np
a = np.random.random((2,4))
print(a)
print(np.sum(a))
- 输出结果
[[0.54856051 0.75547687 0.97634788 0.03362161]
[0.03198332 0.70799137 0.07809093 0.76455108]]
3.8966235679462295
- 求最小值
- 代码
print(np.min(a))
- 输出结果
0.03198332442659946
//可以在后面加axis限制计算范围(axis=0表示在列数中的结果,axis=1表示在行数中的结果)
- 求最大值
- 代码
print(np.max(a))
- 输出结果:
0.9763478806201845
- 索引
(1)输出最小值的索引
- 代码
A = np.arange(2,14).reshape((3,4))
print(np.argmin(A))
- 输出结果
0
(2)输出最大值的索引
- 代码
print(np.argmax(A))
- 输出结果
11
(3)输出非零的数的位置(输出的 第一个array代表行数,第二个array代表列数)
- 代码
print(np.nonzero(A))
- 输出结果
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
(4)一维序列索引
- 代码
import numpy as np
A = np.arrange(3,15)
*输出结果:
6
(5)二维数组索引
- 代码
A = np.arange(3,15).reshape((3,4))
print(A[2])
print(A[1][1])
print(A[2,1])
- 输出结果:
[11 12 13 14]
8
12
(6)用冒号输出所有数
- 代码
print(A[:,1])
print(A[1, 1:3])
- 输出结果:
[4 8 12]
[8 9]
(7)用for迭代每一行的数
- 代码
for row in A:
print (row)
- 输出结果:
[2 3 4 5]
[6 7 8 9]
[10 11 12 13]
(8)用for循环迭代每一列的数
- 代码
for column in A.T:
print (column)
- 输出结果:
[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
(9)迭代每一个项目
- 代码
for item in A.flat:
print(item)
- 输出结果:
2
3
4
5
6
7
8
9
10
11
12
13
- 计算平均值
- 代码
//方法1
print(np.mean(A))
//或:print(A.mean())
//方法2:
print(np.average(A))
- 输出结果
7.5
- 计算中位数
- 代码
print(np.median(A))
- 累加
- 代码
print(np.cumsum(A))
- 输出结果:
[ 2 5 9 14 20 27 35 44 54 65 77 90]
- 累差
- 代码
print(np.diff(A))
- 输出结果:
[[1 1 1]
[1 1 1]
[1 1 1]]
- 逐行从小到大排序
- 代码
print(np.sort(A))
- 输出结果:
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
- 矩阵的转置
- 代码
print(np.transpose(A))
//或:print(A.T)
//矩阵转置和矩阵相乘
print((A.T).dot(A))
- 输出结果:
[[ 2 6 10]
[ 3 7 11]
[ 4 8 12]
[ 5 9 13]]
[[140 158 176 194]
[158 179 200 221]
[176 200 224 248]
[194 221 248 275]]
- 设置一个范围,矩阵中大于该范围的值都用范围的最大值表示,小于该范围的值都用范围的最小值表示
- 代码
print(np.clip(A,5,9))
- 输出结果:
[[5 5 5 5]
[6 7 8 9]
[9 9 9 9]]
数组的拼接
- 序列的垂直拼接
- 代码
import numpy as np
A = np.array([1,1,1])
B = np.array([2,2,2])
C = np.vstack((A,B))
print(C)
print(A.shape, C.shape)
- 输出结果:
[[1 1 1]
[2 2 2]]
(3,) (2, 3)
- 序列的水平拼接
- 代码
A = np.array([1,1,1])
B = np.array([2,2,2])
D = np.hstack((A,B))
print(D)
print(A.shape, D.shape)
- 输出结果:
[1 1 1 2 2 2]
(3,) (6,)
(3)将横向序列转变为竖向序列:通过np.newaxis改变维度
- 代码
A = np.array([1,1,1])
A = A[:, np.newaxis]
//或:A = np.array([1,1,1])[:, np.newaxis]
print(A)
- 输出结果:
[[1]
[1]
[1]]
(4)合并时定义合并的维度
- 代码
A = np.array([1,1,1])[:, np.newaxis]
B = np.array([2,2,2])[:, np.newaxis]
C = np.concatenate((A,B,B,A),axis =1)
print(C)
- 输出:
[[1 2 2 1]
[1 2 2 1]
[1 2 2 1]]
数组的分割
- 等量分割
(1)横向分割
- 代码
import numpy as np
A = np.arange(12).reshape((3,4))
print(np.split(A,3,axis=0))
//或:print(np.vsplit(A,3))
- 输出结果:
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
(2)纵向分割
- 代码
print(np.split(A,2,axis=1))
//或:print(np.hsplit(A,2))
- 输出结果:
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
- 不等量分割
- 代码
print(np.array_split(A,3,axis=0))
- 输出结果:
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
数组的赋值
(1)使用“=”赋值:当一个数组的值改变时,被赋值的任一数组的值相应改变
(2)若不想要这种关联性,则采用.copy()方法赋值