练习1:创建一个形为5×3的二维数组,以包含5到10之间的随机数。
import numpy as np
x = np.random.randint(5, 10, [5, 3])
print(x)
x = np.random.uniform(5, 10, [5, 3])
print(x)
输出:
[[9 7 6]
[9 9 6]
[7 9 9]
[9 7 6]
[9 9 9]]
[[5.47536258 8.95851263 9.51371799]
[6.16938053 6.55711402 8.06057446]
[9.2695685 5.17397916 7.95509877]
[7.81996958 7.53957949 5.09317672]
[6.92367854 9.71356062 5.70666551]]
(随机数)
练习2:分别用一组长方形柱和填充面积的方式模仿画出下图,函数 y = -1 * (x – 2) * (x – 8) +10 在区间[2,9]的积分面积
import re
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,10,0.1)
y = -1*(x-2)*(x-8)+10
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.plot(x, y, color='red')
for i in range(20,90):
rect = plt.Rectangle((x[i],0), width=(x[i+1]-x[i]-0.05), height=y[i], color='grey', alpha=0.5)
ax1.add_patch(rect)
plt.ylim(0.0,20.0)
ax2 = fig.add_subplot(212)
ax2.plot(x, y, color='red')
x1 = np.arange(2,9,0.1)
y1 = -1*(x1-2)*(x1-8)+10
ax2.fill_between(x1, y1,y2=0,color="g", alpha=0.3)
plt.ylim(0.0,20.0)
plt.show()
练习3:一般的矩阵乘法根据公式,可以由三重循环写出,请将其改写为列表推导式的形式。
import numpy as np
M1 = np.random.rand(2,3)
M2 = np.random.rand(3,4)
print(M1)
print(M2)
M1@M2
输出:
[[0.63614198 0.55564072 0.60652888]
[0.58383854 0.84944698 0.87793689]]
[[0.07545791 0.84147725 0.21683321 0.43259567]
[0.27567823 0.99484949 0.12589561 0.47955779]
[0.80435229 0.53008645 0.08230188 0.50390242]]
Original: https://blog.csdn.net/m0_52964135/article/details/125488726
Author: LittleFive-5
Title: Python数据分析——NumPy、Matplotlib、Pandas
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/769880/
转载文章受原作者版权保护。转载请注明原作者出处!