1 Star 0 Fork 51

fosterkong/Book5_Essentials-of-Probability-and-Statistics

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Bk5_Ch17_02.py 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
Visualize-ML 提交于 2023-01-04 01:55 +08:00 . Add files via upload
###############
# Authored by Weisheng Jiang
# Book 6 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import load_iris
plt.close('all')
iris = load_iris()
# A copy from Sklearn
X = iris.data
y = iris.target
feature_names = ['Sepal length, $X_1$','Sepal width, $X_2$',
'Petal length, $X_3$','Petal width, $X_4$']
# Convert X array to dataframe
X_df = pd.DataFrame(X, columns=feature_names)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2)
axs = [ax1, ax2, ax3, ax4]
for name, ax in zip(feature_names, axs):
df = X_df[name]
KDE = sm.nonparametric.KDEUnivariate(df)
KDE.fit(bw=0.5) # 0.1, 0.2, 0.4
ax.fill_between(KDE.support, KDE.density, facecolor = '#DBEEF4')
ax.plot(KDE.support, KDE.density)
ax.scatter(df,0.03*np.abs(np.random.randn(df.size)),marker = 'x')
ax.grid()
ax.autoscale(enable=True, axis='x', tight=True)
ax.autoscale(enable=True, axis='y', tight=True)
ax.set_ylim([0,1])
ax.set_xlim([0,8])
ax.set_xlabel(name)
# Cumulative distribution
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2)
axs = [ax1, ax2, ax3, ax4]
for name, ax in zip(feature_names, axs):
df = X_df[name]
KDE = sm.nonparametric.KDEUnivariate(df)
KDE.fit(bw=0.5) # 0.1, 0.2, 0.4
ax.fill_between(KDE.support, KDE.cdf, facecolor = '#DBEEF4')
ax.plot(KDE.support, KDE.cdf)
ax.plot(KDE.support, KDE.density)
ax.grid()
ax.autoscale(enable=True, axis='x', tight=True)
ax.autoscale(enable=True, axis='y', tight=True)
ax.set_ylim([0,1])
ax.set_xlim([0,8])
ax.set_xlabel(name)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/fosterkong/Book5_Essentials-of-Probability-and-Statistics.git
git@gitee.com:fosterkong/Book5_Essentials-of-Probability-and-Statistics.git
fosterkong
Book5_Essentials-of-Probability-and-Statistics
Book5_Essentials-of-Probability-and-Statistics
main

搜索帮助