代码拉取完成,页面将自动刷新
同步操作将从 Higkoo/Book5_Essentials-of-Probability-and-Statistics 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
###############
# Authored by Weisheng Jiang
# Book 5 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from statsmodels.distributions.empirical_distribution import ECDF
from scipy.stats import norm
import scipy
import seaborn as sns
# Load the iris data
iris_sns = sns.load_dataset("iris")
# A copy from Seaborn
#%% with no class labels
SIGMA = iris_sns.cov()
SIGMA = np.array(SIGMA)
MU = iris_sns.mean()
MU = np.array(MU)
g = sns.pairplot(iris_sns, diag_kind = 'kde',
kind='scatter', plot_kws={'alpha':0.5})
g.axes[0][0]
for i in [0, 1, 2, 3]:
for j in [0, 1, 2, 3]:
if i == j:
pass
else:
ax = g.axes[i][j]
mu_x = MU[j]
mu_y = MU[i]
ax.axvline(x = mu_x, color = 'r', linestyle = '--')
ax.axhline(y = mu_y, color = 'r', linestyle = '--')
ax.plot(mu_x,mu_y, color = 'k', marker = 'x', markersize = 12)
sigma_X = np.sqrt(SIGMA[j][j])
sigma_Y = np.sqrt(SIGMA[i][i])
x = np.linspace(mu_x - 3.5*sigma_X,mu_x + 3.5*sigma_X,num = 201)
y = np.linspace(mu_y - 3.5*sigma_Y,mu_y + 3.5*sigma_Y,num = 201)
xx,yy = np.meshgrid(x,y);
cov_X_Y = SIGMA[i][j]
rho = cov_X_Y/sigma_X/sigma_Y
ellipse = (((xx - mu_x)/sigma_X)**2 -
2*rho*((xx - mu_x)/sigma_X)*((yy - mu_y)/sigma_Y) +
((yy - mu_y)/sigma_Y)**2)/(1 - rho**2);
ellipse = np.sqrt(ellipse)
ax.contour(xx,yy,ellipse,levels = [1,2,3], colors = 'r')
#%% with class labels
dimensions = ['sepal_length',
'sepal_width',
'petal_length',
'petal_width']
g = sns.pairplot(iris_sns, hue="species",
kind='scatter', plot_kws={'alpha':0.5})
g.axes[0][0]
colors = ['b','r','g']
for i, i_dim in enumerate(dimensions):
for j, j_dim in enumerate(dimensions):
if i == j:
pass
else:
ax = g.axes[i][j]
for k, label in enumerate(iris_sns['species'].unique()):
data = iris_sns.loc[iris_sns['species'] == label,
[i_dim,j_dim]]
mu_i_j = data.mean()
mu_x = mu_i_j[1]
mu_y = mu_i_j[0]
SIGMA_i_j = data.cov()
CORR_i_j = data.corr()
SIGMA_i_j = np.array(SIGMA_i_j)
CORR_i_j = np.array(CORR_i_j)
ax.plot(mu_x,mu_y, color = colors[k], marker = 'x', markersize = 12)
sigma_X = np.sqrt(SIGMA_i_j[1][1])
sigma_Y = np.sqrt(SIGMA_i_j[0][0])
x = np.linspace(mu_x - 3.5*sigma_X,mu_x + 3.5*sigma_X,num = 201)
y = np.linspace(mu_y - 3.5*sigma_Y,mu_y + 3.5*sigma_Y,num = 201)
xx,yy = np.meshgrid(x,y);
rho = CORR_i_j[0][1]
ellipse = (((xx - mu_x)/sigma_X)**2 -
2*rho*((xx - mu_x)/sigma_X)*((yy - mu_y)/sigma_Y) +
((yy - mu_y)/sigma_Y)**2)/(1 - rho**2);
ellipse = np.sqrt(ellipse)
print(str(i_dim) + '_' + str(j_dim) + '_' + str(rho))
ax.contour(xx,yy,ellipse,levels = [1,2,3], colors = colors[k])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。