Python数据科学入门(source code & ebook) — 适合Python零基础

这篇内容主要分享一本Python数据科学的入门书籍(包含书中源码),比较适合Python的零基础学习者。本书主要介绍了 Python 在数据科学领域的基础工具,包括 IPython、Jupyter、NumPy、Pandas、Matplotlib(可视化包) 和 Scikit-Learn(机器学习(machine learning))。

Python 作为科学计算的一流(stream)工具已经有几十年的历史了,它还被应用于大型数据集(Dataset)的分析和可视化。NumPy 可以处理同类型数组型数据,Pandas 可以处理多种类型带标签的数据,SciPy 可以解决常见的科学计算问题,Matplotlib 可以绘制可用于论文发表的可视化图形,IPython 可以实现交互式编程和快速分享代码,Scikit-Learn可以进行机器学习(machine learning),当然还有许多其他工具。

书中内容:

在这里插入图片描述

书中源码内容:

在这里插入图片描述

接下来展现书中部分code与可视化内容:
==Example 1:Density-and-Contour-Plots ==

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
def f(x, y):
return np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 40)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
contours = plt.contour(X, Y, Z, 3, colors='black')
plt.clabel(contours, inline=True, fontsize=8)
plt.imshow(Z, extent=[0, 5, 0, 5], origin='lower',
           cmap='RdGy', alpha=0.5)
plt.savefig('code1.png',dpi = 600, bbox_inches='tight')
plt.colorbar()

在这里插入图片描述

Example 2:Text-and-Annotation

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
plt.style.use('seaborn-whitegrid')
import numpy as np
import pandas as pd
births = pd.read_csv('./births.csv')
quartiles = np.percentile(births['births'], [25, 50, 75])
mu, sig = quartiles[1], 0.74 * (quartiles[2] - quartiles[0])
births = births.query('(births > @mu - 5 * @sig) & (births < @mu + 5 * @sig)')
births['day'] = births['day'].astype(int)
births.index = pd.to_datetime(10000 * births.year +
                              100 * births.month +
                              births.day, format='%Y%m%d')
births_by_date = births.pivot_table('births',
                                    [births.index.month, births.index.day])
births_by_date.index = [pd.datetime(2012, month, day)
                        for (month, day) in births_by_date.index]
# plot
fig, ax = plt.subplots(figsize=(12, 4))
births_by_date.plot(ax=ax)
# Add labels to the plot
style = dict(size=10, color='gray')
ax.text('2012-1-1', 3950, "New Year's Day", **style)
ax.text('2012-7-4', 4250, "Independence Day", ha='center', **style)
ax.text('2012-9-4', 4850, "Labor Day", ha='center', **style)
ax.text('2012-10-31', 4600, "Halloween", ha='right', **style)
ax.text('2012-11-25', 4450, "Thanksgiving", ha='center', **style)
ax.text('2012-12-25', 3850, "Christmas ", ha='right', **style)
# Label the axes
ax.set(title='USA births by day of year (1969-1988)',
       ylabel='average daily births')
# Format the x axis with centered month labels
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
ax.xaxis.set_minor_locator(mpl.dates.MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%h'))
plt.savefig('code2.png',dpi = 600, bbox_inches='tight')

在这里插入图片描述

==Example 3:Histograms-and-Binnings ==

from numpy.random import beta
import matplotlib.pyplot as plt
plt.style.use('bmh')
def plot_beta_hist(ax, a, b):
    ax.hist(beta(a, b, size=10000), histtype="stepfilled",
            bins=25, alpha=0.8, density=True)
fig, ax = plt.subplots()
plot_beta_hist(ax, 10, 10)
plot_beta_hist(ax, 4, 12)
plot_beta_hist(ax, 50, 12)
plot_beta_hist(ax, 6, 55)
ax.set_title("'bmh' style sheet")
plt.savefig('code3.png',dpi = 600, bbox_inches='tight')
plt.show()

在这里插入图片描述

Example 4:Visualization-With-Seaborn

import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
import numpy as np
import pandas as pd
import seaborn as sns
sns.set()
iris = sns.load_dataset("iris")
sns.pairplot(iris, hue='species', size=2.5)
plt.savefig('code4.png',dpi = 600, bbox_inches='tight')
plt.show()

在这里插入图片描述

# source code & ebook
# https://mianbaoduo.com/o/author-aGuZmW5pZw==

版权声明:本文为博主CaiBirdHu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/weixin_45492560/article/details/121430429

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2021年11月22日 下午10:04
下一篇 2021年11月22日 下午10:35

相关推荐