栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/997297.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号