Python数据分析——NumPy、Matplotlib、Pandas

练习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]]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值