# （原创）竞赛-关于房价预测模型的数据预分析1

2018/05/08 23:44

import numpy as np
import pandas as pd


df_train.columns#展示各列的名字



###2、对特定列进行分析

df_train['SalePrice'].describe()#研究房价（target），即结果一列，会显示该列的均值 方差 标准差 四分位数 最值等信息


and

#对最后一列的数据做可视化
import seaborn as sns
import matplotlib.pyplot as plt
sns.distplot(df_train['SalePrice'])
plt.show()


#skewness and kurtosis
print("Skewness: %f" % df_train['SalePrice'].skew())
print("Kurtosis: %f" % df_train['SalePrice'].kurt())
#Skewness: 1.882876     Kurtosis: 6.536282


###3、分析某个元素与结果列（saleprice）的关系

#scatter plot grlivarea/saleprice即研究某个特征与房价的关系
var = 'GrLivArea'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000));
plt.show()


#检查另一个特征与房价的关系
var='TotalBsmtSF'
data=pd.concat([df_train['SalePrice'],df_train[var]],axis=1)
data.plot.scatter(x=var,y='SalePrice',ylim=(0,800000))
plt.show()


#以上都是数值型数据与房价的分布，下面时类别型数据与房价的分布描述
#box plot overallqual/saleprice
var = 'OverallQual'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
f, ax = plt.subplots(figsize=(8, 6))
fig = sns.boxplot(x=var, y="SalePrice", data=data)
fig.axis(ymin=0, ymax=800000);
plt.show()


#类别型数据
var = 'YearBuilt'
data = pd.concat([df_train['SalePrice'], df_train[var]], axis=1)
f, ax = plt.subplots(figsize=(16, 8))
fig = sns.boxplot(x=var, y="SalePrice", data=data)
fig.axis(ymin=0, ymax=800000)
plt.xticks(rotation=90)
plt.show()


#correlation matrix相关矩阵
corrmat = df_train.corr()
f, ax = plt.subplots(figsize=(12, 9))
sns.heatmap(corrmat, vmax=.8, square=True)
plt.show()


#saleprice correlation matrix 相关矩阵
k = 10 #number of variables for heatmap
cols = corrmat.nlargest(k, 'SalePrice')['SalePrice'].index
cm = np.corrcoef(df_train[cols].values.T)
sns.set(font_scale=1.25)
hm = sns.heatmap(cm, cbar=True, annot=True, square=True, fmt='.2f', annot_kws={'size': 10}, yticklabels=cols.values, xticklabels=cols.values)
plt.show()


#scatterplot
sns.set()
cols = ['SalePrice', 'OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF', 'FullBath', 'YearBuilt']
sns.pairplot(df_train[cols], size = 2.5)
plt.show()


0
0 收藏

0 评论
0 收藏
0