一. 多变量分析概述
观测两个变量之间的分布关系最好用散点图
代码1:
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x = np.random.gamma(6, size=200)
mean, cov = [0, 1], [(1, .5), (.5, 1)]
data = np.random.multivariate_normal(mean, cov, 200)
df = pd.DataFrame(data, columns=["x", "y"])
sns.jointplot(x="x", y="y", data=df)
plt.show()
测试记录1:

代码2:
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x = np.random.gamma(6, size=200)
mean, cov = [0, 1], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, 1000).T
with sns.axes_style("white"):
sns.jointplot(x=x, y=y, kind="hex", color="k")
plt.show()
测试记录2:

二. 多变量分析实例
2.1 数据集概述
我们这边使用鸢尾花数据集:
- 数据集包含150个样本(数据集的行)
- 数据集包含4个属性(数据集的列):Sepal Length,Sepal Width,Petal Length,Petal Width:‘feature_names’
- 利用numpy.ndarray存储这150x4的数据:‘data’
分类标签取自array[‘Setosa’,‘Versicolour’,‘Virginica’]:‘target_names’
Setosa,Versicolour,Virginica是数据集所包含的3中品种的鸢尾花
这3个分类标签(即150x1数据)用np.ndarray存储:‘target’
总之,这个数据存储了150x4的特征数据和150x1的分类标签,其中特征数据又放在‘data’里,分类标签放在‘target’里
2.2 实例
代码:
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset("iris")
sns.pairplot(iris)
plt.show()
print ("test")
测试记录:
鸢尾花数据集总共有4个变量,4*4=16,所以直接生成了16个图
如果是柱形代表自身与自身

网友评论