NNDL 实验一 numpy

numpy 的array操作

1.导入numpy库

import numpy as np

2.建立一个一维数组 a 初始化为[4,5,6], (1)输出a 的类型(type)(2)输出a的各维度的大小(shape)(3)输出 a的第一个元素(值为4)

a=np.narry([4, 5, 6])
print(a.dtype)
print(a.shape)
print(a[0])

运行结果:

a2e62201202f4c6cbec511bc528aef63.png

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.narry([4, 5, 6],[1, 2, 3])
print(b.shape)
print(b[0,0],b[0,1],b[1,1])

 运行结果:

11cb173ade52435198c9debcc1c6c21a.png

4. (1)建立一个全0矩阵 a, 大小为 3x3; 类型为整型(提示: dtype = int)(2)建立一个全1矩阵b,大小为4x5; (3)建立一个单位矩阵c ,大小为4x4; (4)生成一个随机数矩阵d,大小为 3x2.

a = np.zeros( (3, 3),  dtype = int )
b = np.ones( (4, 5),  dtype = int )
c = np.identity( (4),  dtype = int )
d = np.random.randint( 1,  10,  dtype = int,  size = (3, 2) )
print(a)
print(b)
print(c)
print(d)

运行结果:

889590b1018b46678a6d739e47790278.png

解注:d中的1和10表示生成的随机数的范围是1-10。

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], a[0, 0])

 运行结果:

dd081d3c931d423dbe39000bc33a23b1.png

6.把上一题的 a数组的 0到1行 2到3列,放到b里面去,(此处不需要从新建立a,直接调用即可)(1),输出b;(2) 输出b 的(0,0)这个元素的值

b = a[0:2, 1:3]
print(b)
print(b[0][0])

运行结果:

解注:将a的第0行到第1行,第2列到第3列放到b里面去,这里需要0-1行,而0:2表示0,1,并不代表2,所以这里使用的时0:2

7. 把第5题中数组a的最后两行所有元素放到 c中,(提示: a[1:2, :])(1)输出 c ; (2) 输出 c 中第一行的最后一个元素(提示,使用 -1 表示最后一个元素)

c = a[1:3]
print(c)
print(c[-1][-1])

运行结果:

532591138f704523b544ba42b8c08eae.png

8.建立数组a,初始化a为[[1, 2], [3, 4], [5, 6]],输出 (0,0)(1,1)(2,0)这三个元素(提示: 使用 print(a[[0, 1, 2], [0, 1, 0]]) )、

a=np.array([[1, 2],[3, 4],[5, 6]])
print(a[[0, 1, 2], [0, 1, 0]])

运行结果:

9a5dedf8c4e148baafa7fdacc7953450.png

9.建立矩阵a ,初始化为[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],输出(0,0),(1,2),(2,0),(3,1) (提示使用 b = np.array([0, 2, 0, 1]) print(a[np.arange(4), b]))

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])

 运行结果:

c8398b5b45cb46969bcb9975fd279ae8.png

解注:np.arange()函数当参数为一个时,表示终点,起点默认为0,步长为1。当参数为两个时,表示起点和终点,步长为1。当参数为三个时,分别表示起点,终点,步长。

10.对9 中输出的那四个元素,每个都加上10,然后重新输出矩阵a.(提示: a[np.arange(4), b] += 10 )

a[np.arange(4),b] += 10;
print(a)

运行结果:

4235331601c84970bdb1c74ebbbd238a.png

array 的数学运算

11. 执行 x = np.array([1, 2]),然后输出 x 的数据类型

x = np.array([1, 2])
print(x.dtype)

 运行结果:

b68f712737554209b921ad3666cca2c9.png

12.执行 x = np.array([1.0, 2.0]) ,然后输出 x 的数据类类型 

x = np.array([1.0, 2.0])
print(x.dtype)

运行结果:

ff3ef72b64624c53bd35548eb93a3d0e.png

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))

 运行结果:

282fd73bfb704db5b4d21fd860734af5.png

14. 利用 13题目中的x,y 输出 x-y 和 np.subtract(x,y)

print(x - y)
print(np.subtract(x, y))

运行结果:

babb94d45e7c487f8e291629044c3af6.png

15. 利用13题目中的x,y 输出 x*y ,和 np.multiply(x, y) 还有 np.dot(x,y),比较差异。然后自己换一个不是方阵的试试。

print(x * y)
print(np.multiply(x, y))
print(np.dot(x, y))

 运行结果:
2e8cf5e6db7b4292b9db3d7619ac6561.png

解注:np.dot()可以用来进行向量内积,矩阵乘法,向量和矩阵的乘法等运算。

16. 利用13题目中的x,y,输出 x / y .(提示 : 使用函数 np.divide())

print(x / y)
print(np.divide(x, y))

运行结果:

518626339ccb417080a9fe23eecf8654.png

17. 利用13题目中的x,输出 x的 开方。(提示: 使用函数 np.sqrt() )

print( np.sqrt(x) )

 运行结果:

bc5e90bd76ae414e90110eb26b7c4155.png

18.利用13题目中的x,y ,执行 print(x.dot(y)) 和 print(np.dot(x,y)) 

print( x.dot(y) ) 
print( np.dot(x,y) )

 运行结果:

8aba29b4d5334086bb564f3c4205d648.png

19.利用13题目中的 x,进行求和。提示:输出三种求和 (1)print(np.sum(x)): (2)print(np.sum(x,axis =0 )); (3)print(np.sum(x,axis = 1))

print( np.sum(x) )
print( np.sum(x, axis =0))
print( np.sum(x, axis =1))

 运行结果:

df42aaa2e2cb43febe9db6621db3fcce.png

解注:axis=0表示按列求和,axis=1表示按行求和。下同。

20.利用13题目中的 x,进行求平均数(提示:输出三种平均数(1)print(np.mean(x)) (2)print(np.mean(x,axis = 0))(3) print(np.mean(x,axis =1)))

print( np.mean(x) )
print( np.mean(x,axis = 0) )
print( np.mean(x,axis = 1) )

 运行结果:

8c815fab33af43df8a8fdaa619871209.png

 21.利用13题目中的x,对x 进行矩阵转置,然后输出转置后的结果,(提示: x.T 表示对 x 的转置)

x = x.T
print(x)

 运行结果:

a1f1eec0d0b542c9a1b82cdc08e04527.png

22.利用13题目中的x,求e的指数(提示: 函数 np.exp())

print( np.exp(x) )

 运行结果:

d61d7a02601b4b4ca772366b3619dcae.png

23.利用13题目中的 x,求值最大的下标(提示(1)print(np.argmax(x)) ,(2) print(np.argmax(x, axis =0))(3)print(np.argmax(x),axis =1))

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()

运行结果: 

e9fee64ffb8841918202bd1ad2dfe88e.png

解注:plt.plot()表示画出图像,plt.show()表示显示图像。

25.画图。画正弦函数和余弦函数, x = np.arange(0, 3 * np.pi, 0.1)(提示:这里用到 np.sin() np.cos() 函数和 matplotlib.pyplot 库)

x = np.arange(0, 3*np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
y = np.cos(x)
plt.plot(x, y)
plt.show()

运行结果: 

931511cdcc214b20b747f7fddafea430.png

21182844a1694cda8a3c03fbc2955029.png

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI-2 刘子豪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值