Skip to Main Content
Python程序设计:人工智能案例实践
book

Python程序设计:人工智能案例实践

by 保罗 戴特尔, 哈维 戴特尔
August 2021
Intermediate to advanced content levelIntermediate to advanced
450 pages
13h 28m
Chinese
Pearson
Content preview from Python程序设计:人工智能案例实践

5.17.2 实现掷骰子中不同点数出现次数和百分比的可视化

在本节中,将以交互的方式开发上一节中显示的柱状图。

启动IPython实现交互式Matplotlib开发

IPython内置支持交互式开发Matplotlib图表,后面还需要开发Seaborn图表。我们只需使用以下命令启动IPython:

    ipython --matplotlib

导入库

首先,让我们导入将要用到的库:

1. matplotlib.pyplot模块包含Matplotlib库的图形功能。导入此模块后通常用缩写plt来表示。

2. NumPy(Numerical Python)库包含函数unique,用于汇总掷骰子点数。导入此模块后通常用缩写np来表示。

3. random模块包含Python的随机数生成函数。

4. seaborn模块包含Seaborn库的图形功能。导入此模块后通常用缩写sns来表示。读者可以搜索一下用这种奇特缩写的原因。

掷骰子并统计点数出现频率

接下来,让我们使用列表推导式来创建包含600个随机点数的列表,然后使用NumPy的unique函数来对点数去重(很可能六个面都有)并且统计每个点数出现的次数:

NumPy库提供高性能的ndarray合集,它通常比列表快很多[1]。虽然我们不直接在这里使用 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

数据科学中的实用统计学(第2版)

数据科学中的实用统计学(第2版)

Peter Bruce, Andrew Bruce, Peter Gedeck
Python算法交易实战

Python算法交易实战

Posts & Telecom Press, Sebastien Donadio
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111678458