一、NumPy 的array操作
1.numpy库的导入
import numpy as np
(注:在后面给出的例题代码基础上,均需要先导入numpy库)
2.建立一个一维数组 a 初始化为[4,5,6], (1)输出a 的类型(type)(2)输出a的各维度的大小(shape)(3)输出 a的第一个元素(值为4)
a = np.array([4, 5, 6])
print(type(a))
print(a.shape)
print(a[0])
输出结果:
3.建立一个二维数组 b,初始化为 [ [4, 5, 6],[1, 2, 3]] (1)输出各维度的大小(shape)(2)输出 b(0,0),b(0,1),b(1,1) 这三个元素(对应值分别为4,5,2)
b = np.array([[4, 5, 6], [1, 2, 3]])
print(b.shape)
print(b[0][0])
print(b[0][1])
print(b[1][1])
输出结果:
4. (1)建立一个全0矩阵 a, 大小为 3x3; 类型为整型;(2)建立一个全1矩阵b,大小为4x5; (3)建立一个单位矩阵c ,大小为4x4; (4)生成一个随机数矩阵d,大小为 3x2.
a = np.zeros((3, 3), dtype=int)
b = np.ones((4, 5))
c = np.identity(4)
d = np.random.rand(3, 2)
print(a)
print(b)
print(c)
print(d)
输出结果:
5. 建立一个数组 a,(值为[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] ) ,(1)打印a; (2)输出 下标为(2,3),(0,0) 这两个数组元素的值
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
print(a[2][3])
print(a[0][0])
输出结果:
6.把上一题的 a数组的 0到1行 2到3列,放到b里面去,(此处不需要从新建立a,直接调用即可)(1),输出b;(2) 输出b 的(0,0)这个元素的值
b = a[0:2, 2:4]
print(b)
print(b[0, 0])
输出结果:
7. 把第5题中数组a的最后两行所有元素放到 c中,(提示: a[1:2, :])(1)输出 c ; (2) 输出 c 中第一行的最后一个元素
c = a[1:3, :]
print(c)
print(c[0][-1])
输出结果:
(-1表示最后一个元素)
8.建立数组a,初始化a为[[1, 2], [3, 4], [5, 6]],输出 (0,0)(1,1)(2,0)这三个元素
a = np.array([[1, 2], [3, 4], [5, 6]])
print(a[[0, 1, 2], [0, 1, 0]])
输出结果:
(a[[0,1,2],[0,1,0]]中[0,1,2]分别表示三个点的横坐标;[0,1,0]表示三个点的纵坐标)
9.建立矩阵a ,初始化为[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],输出(0,0),(1,2),(2,0),(3,1)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
b = np.array([0, 2, 0, 1])
print(a[np.arange(4), b])
输出结果:
10.对9 中输出的那四个元素,每个都加上10,然后重新输出矩阵a.(提示: a[np.arange(4), b] += 10 )
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
b = np.array([0, 2, 0, 1])
a[np.arange(4), b] += 10
print(a)
输出结果:
二、array 的数学运算
11. 执行 x = np.array([1, 2]),然后输出 x 的数据类型
x = np.array([1, 2])
print(x.dtype)
输出结果:
12.执行 x = np.array([1.0, 2.0]) ,然后输出 x 的数据类类型
x = np.array([1.0, 2.0])
print(x.dtype)
输出结果:
13.执行 x = np.array([[1, 2], [3, 4]], dtype=np.float64) ,y = np.array([[5, 6], [7, 8]], dtype=np.float64),然后输出 x+y ,和 np.add(x,y)
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
y = np.array([[5, 6], [7, 8]], dtype=np.float64)
print(x+y)
print(np.add(x, y))
输出结果:
14. 利用 13题目中的x,y 输出 x-y 和 np.subtract(x,y)
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
y = np.array([[5, 6], [7, 8]], dtype=np.float64)
print(x-y)
print(np.subtract(x, y))
输出结果:
15. 利用13题目中的x,y 输出 x*y ,和 np.multiply(x, y) 还有 np.dot(x,y),比较差异。然后自己换一个不是方阵的试试。
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
y = np.array([[5, 6], [7, 8]], dtype=np.float64)
print(x * y)
print(np.multiply(x, y))
print(np.dot(x, y))
# *和multiply是对应数相乘;dot是矩阵乘
输出结果:
x = np.array([[10, 2], [3, 40]], dtype=np.float64)
y = np.array([[5, 60], [7, 80]], dtype=np.float64)
print(x * y)
print(np.multiply(x, y))
print(np.dot(x, y))
输出结果:
(运算符号和multiply函数是对应数相乘;dot函数是矩阵乘)
16. 利用13题目中的x,y,输出 x / y .
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
y = np.array([[5, 6], [7, 8]], dtype=np.float64)
print(np.divide(x, y))
输出结果:
17. 利用13题目中的x,输出 x的 开方.
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
y = np.array([[5, 6], [7, 8]], dtype=np.float64)
print(np.sqrt(x))
输出结果:
18.利用13题目中的x,y ,执行 print(x.dot(y)) 和 print(np.dot(x,y))
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
y = np.array([[5, 6], [7, 8]], dtype=np.float64)
print(x.dot(y))
print(np.dot(x, y))
输出结果:
19.利用13题目中的 x,进行求和。提示:输出三种求和 (1)print(np.sum(x)): (2)print(np.sum(x,axis =0 )); (3)print(np.sum(x,axis = 1))
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
print(np.sum(x))
print(np.sum(x, axis=0))
print(np.sum(x, axis=1))
输出结果:
20.利用13题目中的 x,进行求平均数(提示:输出三种平均数(1)print(np.mean(x)) (2)print(np.mean(x,axis = 0))(3) print(np.mean(x,axis =1)))
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
print(np.mean(x))
print(np.mean(x, axis=0))
print(np.mean(x, axis=1))
输出结果:
21.利用13题目中的x,对x 进行矩阵转置,然后输出转置后的结果.
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
print(x.T)
输出结果:
22.利用13题目中的x,求e的指数
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
print(np.exp(x))
输出结果:
23.利用13题目中的 x,求值最大的下标(提示(1)print(np.argmax(x)) ,(2) print(np.argmax(x, axis =0))(3)print(np.argmax(x),axis =1))
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
print(np.argmax(x))
print(np.argmax(x, axis=0))
print(np.argmax(x, axis=1))
输出结果:
24,画图,y=x*x 其中 x = np.arange(0, 100, 0.1) (提示这里用到 matplotlib.pyplot 库)
import matplotlib.pyplot as plt
x = np.arange(0, 100, 0.1)
y = x * x
plt.plot(x, y)
plt.show()
输出结果:
25.画图。画正弦函数和余弦函数, x = np.arange(0, 3 * np.pi, 0.1)
import matplotlib.pyplot as plt
x = np.arange(0, 3*np.pi, 0.1)
sx = np.sin(x)
cx = np.cos(x)
plt.plot(x, sx)
plt.plot(x, cx)
plt.show()
输出结果: