我在 2019 年 1 月 15 日从 Amazon.ca 买的《Python for Data Analysis - Data Wrangling with Pandas, Numpy, and iPython》这本书,买来就闲置着,没有看过。这次 COVID-19 大家都得呆在家里,没什么事干,决定把这本书干下来。
作者是 Wes McKinney,他是 Pandas 开源软件包的创建者。他在 2012 年 10 月首次出版了这本书,在 2017 年出了第二版,第二版又发行了两次,9 月 25 日版本和 10 月 20 日版本。我买的是 10 月 20 日纸质平装本。
这本书比较权威,因此很容易搜到它的电子书,不过我喜欢实实在在的书。
前言 Preface
前言基本上是废话,但需要记住下面两点:
- 这本书的 github 网址 https://github.com/wesm/pydata-book。书中的例子和资源都在这上面。
- 这本书的勘误表网址 http://www.oreilly.com/catalog/errata.csp?isbn=0636920050896。
第一章 Preliminaries
这一章介绍了一些预备知识。
如果你以前没接触过 Numpy、pandas、matplotlib、Jupyter、SciPy、scikit-learn 和 statsmodels,1.1 到 1.3 读了也是白读,建议直接略过。
1.5 和 1.6 是废话,建议直接略过。
1.4 讲安装和设置,因为Ananconda 自带 Python,这节就是教你在 Windows、macOS 和 Linux 上如何安装 Anaconda,如何安装和更新软件包。它写得太简单了,不要读它了。网上很多更好的最新的教程,自己去搜。这是本章你唯一需要做的事。
在 macOS 上安装 Anaconda 请注意看我写的这篇文章。
在 Windows 上,可以按照 Anaconda 的官方安装指南来安装和启动。
第二章 Python 语言基础,IPython 和 Jupyter Notebook
我相信你以前肯定学习过 Python 语言。如果没学过,建议你找本书学习一下。如果你学习过其它程序语言,看完第二章和第三章也应该可以。
我学习过最基本的 Python,算是个初级者,但是没用过 IPython shell,更没用过 Jupyter Notebook。如果你也和我一样,那么你要好好读读 2.1 到 2.2 节,因为全书都是基于 Jupyter Notebook 的。
如果你正常安装了 Anaconda,那么 python shell,ipython shell 就能运行起来,jupyter notebook 也能运行起来。如果运行不起来,根据出现的错误提示网上搜索解决方法。
请注意:在 Windows 下需要先启动 Anaconda prompt,然后才能运行 python、ipython 和 jupyter notebook。在 Windows 下的 cmd、powershell 或 terminal 下运行的 python 可能是你安装的其它版本的 python。
2.3 是 Python 语言基础知识。根据你自身情况决定略过与否。
此章重点:
- 启动 Anaconda 的 python interpreter/shell
- 启动 ipython interpreter/shell
- 启动 jupyter notebook,执行 python 代码,关闭 jupyter notebook。
- 掌握 ipython 和 jupyter notebook 下的 Tab Completion、Introspection、%run Command、Magic Command。
- 掌握 ipython 和 jupyter notebook 下的 matplotlib integration。对比一下 %matplotlib inline 和 %matplotlib 显示结果的不同。
以后的章节都在 jupyter notebook 上运行。
体验一下在 Windows 下 shut down Jupyter Notebook 有时十分慢,是件痛苦的事情。
第三章 Python 语言内置数据结构,函数和文件
这一章还是讲 Python 语言的。如果你和我一样是 Python 语言初级者,值得复习一下这部分,否则略过。
自我学习
看到不是十分明白的地方,要善于自我学习。IPython、NumPy 和 pandas 官方网站上都有搜索框,可以帮助你深入学习。
- IPython Documentation: https://ipython.readthedocs.io/en/stable/
- NumPy Documentation: https://numpy.org/devdocs/index.html
- pandas Documentation: https://pandas.pydata.org/docs/
- Jupyter Documentation: https://jupyter.readthedocs.io/en/latest/
另外,在网上也可以搜到对某个知识点的深度解释和举例。
在读这本书的过程中,Jupyter Notebook 要一直开着,Google 要一直开着。
举一个例子
比如在第 18 页,有下面这样两行代码:
import numpy as np
data = {i : np.random.randn() for i in range(7)}
NumPy 在第四章才开始讲,但是你想知道 numpy.random.randn() 做什么的,你可以把它复制到 NumPy Documentation 上去搜出参考文献。同时你也会看到这样一个提示:New code should use the standard_normal method of a default_rng() instance instead; see random-quick-start。这样你就会搜到 Random sampling (numpy.random) 和 numpy.random.Generator.standard_normal 等参考文献。
已经不建议使用 numpy.random.randn() 了。如果使用新的代码,第二行这样写比较好:
rng = np.random.default_rng()
data = {i : rng.standard_normal() for i in range(7)}
更多例子
比如上面提到的 %matplotlib 和 %matplotlib inline 的区别(在书中第 30 页)。你就会搜到 IPython 官方文献对 %matplotlib 这个 magic command 的详细解释,和网上其他人的答疑。
还有第 32 和 33 页提到的 Python 的变量赋值和函数的参数传递机制,如果不是很明白,Google 更多的例子看看,或者回头去看看专门的 Python 书籍。
No comments:
Post a Comment