import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df_train = pd.read_csv('train.csv')
df_test = pd.read_csv('test.csv')
# 查看各列数据类型
df_train.dtypes.value_counts()
object 43
int64 35
float64 3
dtype: int64
# 把训练集和训练集合并一起处理,保证后期特征逻辑一致,注意不要随便drop行,drop行后顺序会变,后期拆分测试集会有问题
df_train_and_test = pd.concat([df_train.loc[:, 'MSSubClass': 'SaleCondition'], df_test.loc[:, 'MSSubClass': 'SaleCondition']], axis=0, ignore_index=True)
# 发现数据MSSubClass应为字符类型,进行转换
df_train_and_test['MSSubClass'] = df_train_and_test['MSSubClass'].astype('str')
# 查看训练集的相关性矩阵,如果值高说明相关性高,存在共线性,只保留共性特征中的1个
corrmat = df_train.corr()
f, ax = plt.subplots(figsize=(20, 10))
sns.heatmap(corrmat, vmax=.8, square=True)
<matplotlib.axes._subplots.AxesSubplot at 0x1fc3b1737b8>
# 查看系数矩阵和Y值最相关的10个特征
cols = corrmat.nlargest(n=10, columns='SalePrice').index
# 将10个特征相关系统矩阵按图形展示
cm = np.corrcoef(df_train[cols].values.T)
plt.figure(figsize=(20, 10))
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()
# 删除共线性特征,保留与y值相关性高的特征
df_train_and_test = df_train_and_test.drop('GarageArea', axis=1)
df_train_and_test = df_train_and_test.drop('1stFlrSF', axis=1)
df_train_and_test = df_train_and_test.drop('TotRmsAbvGrd', axis=1)
# 统计展示缺失数据情况
missing = df_train_and_test.isnull().sum()
missing = missing[missing>0]
missing.sort_values(ascending=False, inplace=True)
missing_percent = df_train_and_test[missing.index].isnull().sum()/df_train_and_test[missing.index].isnull().count()
missing_types = df_train_and_test[missing.index]
missing_data = pd.concat([missing, missing_percent, missing_types.dtypes], axis=1, keys=['Total', 'Percent', 'Types'])
missing_data.sort_values(by='Total', ascending=False, inplace=True)
missing_data
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Total</th>
<th>Percent</th>
<th>Types</th>
</tr>
</thead>
<tbody>
<tr>
<th>PoolQC</th>
<td>2909</td>
<td>0.996574</td>
<td>object</td>
</tr>
<tr>
<th>MiscFeature</th>
<td>2814</td>
<td>0.964029</td>
<td>object</td>
</tr>
<tr>
<th>Alley</th>
<td>2721</td>
<td>0.932169</td>
<td>object</td>
</tr>
<tr>
<th>Fence</th>
<td>2348</td>
<td>0.804385</td>
<td>object</td>
</tr>
<tr>
<th>FireplaceQu</th>
<td>1420</td>
<td>0.486468</td>
<td>object</td>
</tr>
<tr>
<th>LotFrontage</th>
<td>486</td>
<td>0.166495</td>
<td>float64</td>
</tr>
<tr>
<th>GarageCond</th>
<td>159</td>
<td>0.054471</td>
<td>object</td>
</tr>
<tr>
<th>GarageQual</th>
<td>159</td>
<td>0.054471</td>
<td>object</td>
</tr>
<tr>
<th>GarageFinish</th>
<td>159</td>
<td>0.054471</td>
<td>object</td>
</tr>
<tr>
<th>GarageYrBlt</th>
<td>159</td>
<td>0.054471</td>
<td>float64</td>
</tr>
<tr>
<th>GarageType</th>
<td>157</td>
<td>0.053786</td>
<td>object</td>
</tr>
<tr>
<th>BsmtCond</th>
<td>82</td>
<td>0.028092</td>
<td>object</td>
</tr>
<tr>
<th>BsmtExposure</th>
<td>82</td>
<td>0.028092</td>
<td>object</td>
</tr>
<tr>
<th>BsmtQual</th>
<td>81</td>
<td>0.027749</td>
<td>object</td>
</tr>
<tr>
<th>BsmtFinType2</th>
<td>80</td>
<td>0.027407</td>
<td>object</td>
</tr>
<tr>
<th>BsmtFinType1</th>
<td>79</td>
<td>0.027064</td>
<td>object</td>
</tr>
<tr>
<th>MasVnrType</th>
<td>24</td>
<td>0.008222</td>
<td>object</td>
</tr>
<tr>
<th>MasVnrArea</th>
<td>23</td>
<td>0.007879</td>
<td>float64</td>
</tr>
<tr>
<th>MSZoning</th>
<td>4</td>
<td>0.001370</td>
<td>object</td>
</tr>
<tr>
<th>BsmtFullBath</th>
<td>2</td>
<td>0.000685</td>
<td>float64</td>
</tr>
<tr>
<th>Utilities</th>
<td>2</td>
<td>0.000685</td>
<td>object</td>
</tr>
<tr>
<th>BsmtHalfBath</th>
<td>2</td>
<td>0.000685</td>
<td>float64</td>
</tr>
<tr>
<th>Functional</th>
<td>2</td>
<td>0.000685</td>
<td>object</td>
</tr>
<tr>
<th>Exterior1st</th>
<td>1</td>
<td>0.000343</td>
<td>object</td>
</tr>
<tr>
<th>Exterior2nd</th>
<td>1</td>
<td>0.000343</td>
<td>object</td>
</tr>
<tr>
<th>SaleType</th>
<td>1</td>
<td>0.000343</td>
<td>object</td>
</tr>
<tr>
<th>BsmtFinSF1</th>
<td>1</td>
<td>0.000343</td>
<td>float64</td>
</tr>
<tr>
<th>BsmtFinSF2</th>
<td>1</td>
<td>0.000343</td>
<td>float64</td>
</tr>
<tr>
<th>BsmtUnfSF</th>
<td>1</td>
<td>0.000343</td>
<td>float64</td>
</tr>
<tr>
<th>Electrical</th>
<td>1</td>
<td>0.000343</td>
<td>object</td>
</tr>
<tr>
<th>KitchenQual</th>
<td>1</td>
<td>0.000343</td>
<td>object</td>
</tr>
<tr>
<th>GarageCars</th>
<td>1</td>
<td>0.000343</td>
<td>float64</td>
</tr>
<tr>
<th>TotalBsmtSF</th>
<td>1</td>
<td>0.000343</td>
<td>float64</td>
</tr>
</tbody>
</table>
# 图形展示缺失数据
missing.plot.bar(figsize=(20, 5))
<matplotlib.axes._subplots.AxesSubplot at 0x1fc3b141438>
## 同时是相关性列,也是缺失数据的有2个
missing_data.index.intersection(cols)
Index(['GarageCars', 'TotalBsmtSF'], dtype='object')
# 查看这2个特征的缺失情况
missing_data.loc[missing_data.index.intersection(cols)]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Total</th>
<th>Percent</th>
<th>Types</th>
</tr>
</thead>
<tbody>
<tr>
<th>GarageCars</th>
<td>1</td>
<td>0.000343</td>
<td>float64</td>
</tr>
<tr>
<th>TotalBsmtSF</th>
<td>1</td>
<td>0.000343</td>
<td>float64</td>
</tr>
</tbody>
</table>
</div>
# drop掉其它缺失数据列
df_train_and_test = df_train_and_test.drop(missing_data[missing_data['Total']>1].index, axis=1)
df_train_and_test.isnull().sum().max()
1
# 对于剩余的数据按列平均值进行填充
df_train_and_test = df_train_and_test.fillna(df_train_and_test.mean())
# 剩余一个特殊为字符的后续转化成one_hot处理
df_train_and_test.isnull().sum()
MSSubClass 0
LotArea 0
Street 0
LotShape 0
LandContour 0
LotConfig 0
LandSlope 0
Neighborhood 0
Condition1 0
Condition2 0
BldgType 0
HouseStyle 0
OverallQual 0
OverallCond 0
YearBuilt 0
YearRemodAdd 0
RoofStyle 0
RoofMatl 0
Exterior1st 1
Exterior2nd 1
ExterQual 0
ExterCond 0
Foundation 0
BsmtFinSF1 0
BsmtFinSF2 0
BsmtUnfSF 0
TotalBsmtSF 0
Heating 0
HeatingQC 0
CentralAir 0
Electrical 1
2ndFlrSF 0
LowQualFinSF 0
GrLivArea 0
FullBath 0
HalfBath 0
BedroomAbvGr 0
KitchenAbvGr 0
KitchenQual 1
Fireplaces 0
GarageCars 0
PavedDrive 0
WoodDeckSF 0
OpenPorchSF 0
EnclosedPorch 0
3SsnPorch 0
ScreenPorch 0
PoolArea 0
MiscVal 0
MoSold 0
YrSold 0
SaleType 1
SaleCondition 0
dtype: int64
# 将字符值特征转换成one_hot类型
df_train_and_test = pd.get_dummies(df_train_and_test, drop_first=True, dummy_na=True)
df_train_and_test.isnull().sum().max()
0
# 查看所有特殊的相关系数矩阵,对于值超过0.8的只取其中1个
corrmat = df_train_and_test.corr()
droplist = []
n_col = len(corrmat.columns)
n_row = len(corrmat.index)
for i in range(n_col):
for j in range(n_row):
x = corrmat.iloc[j, i]
idx = corrmat.index[j]
cln = corrmat.columns[i]
if np.abs(x) > 0.8 and idx != cln:
print(idx, cln, x)
droplist.append(idx)
HouseStyle_2Story 2ndFlrSF 0.8102595976
BldgType_2fmCon MSSubClass_190 0.97511848656
HouseStyle_1.5Unf MSSubClass_45 0.864323350941
HouseStyle_SLvl MSSubClass_80 0.958427756655
BldgType_Duplex MSSubClass_90 1.0
MSSubClass_190 BldgType_2fmCon 0.97511848656
MSSubClass_90 BldgType_Duplex 1.0
MSSubClass_45 HouseStyle_1.5Unf 0.864323350941
2ndFlrSF HouseStyle_2Story 0.8102595976
MSSubClass_80 HouseStyle_SLvl 0.958427756655
RoofStyle_Hip RoofStyle_Gable -0.939468653735
RoofStyle_Gable RoofStyle_Hip -0.939468653735
Exterior2nd_CmentBd Exterior1st_CemntBd 0.983410908223
Exterior2nd_HdBoard Exterior1st_HdBoard 0.887998938432
Exterior2nd_MetalSd Exterior1st_MetalSd 0.969711678996
Exterior2nd_VinylSd Exterior1st_VinylSd 0.978177709604
Exterior2nd_Wd Sdng Exterior1st_Wd Sdng 0.861610287457
Exterior2nd_nan Exterior1st_nan 1.0
Exterior1st_CemntBd Exterior2nd_CmentBd 0.983410908223
Exterior1st_HdBoard Exterior2nd_HdBoard 0.887998938432
Exterior1st_MetalSd Exterior2nd_MetalSd 0.969711678996
Exterior1st_VinylSd Exterior2nd_VinylSd 0.978177709604
Exterior1st_Wd Sdng Exterior2nd_Wd Sdng 0.861610287457
Exterior1st_nan Exterior2nd_nan 1.0
ExterQual_TA ExterQual_Gd -0.89966850725
ExterQual_Gd ExterQual_TA -0.89966850725
ExterCond_TA ExterCond_Gd -0.871903195903
ExterCond_Gd ExterCond_TA -0.871903195903
KitchenQual_TA KitchenQual_Gd -0.825028510061
KitchenQual_Gd KitchenQual_TA -0.825028510061
SaleCondition_Partial SaleType_New 0.986572970445
SaleType_New SaleCondition_Partial 0.986572970445
df_train_and_test = df_train_and_test.drop(droplist, axis=1)
# 把数据集拆分成训练集和测试集
train_number = df_train.shape[0]
X = df_train_and_test[:train_number]
y = df_train['SalePrice']
test = df_train_and_test[train_number:]
# 查看训练集的数据情况
sns.set()
cols = ['SalePrice', 'OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF', 'FullBath', 'YearBuilt']
sns.pairplot(df_train[cols], size=2.5)
plt.show()
# 单独查看特征和y值情况
var = 'GrLivArea'
data = pd.concat([y, X[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice')
<matplotlib.axes._subplots.AxesSubplot at 0x1fc3e6dda58>
# 查看右下2个异常点
data[data[var]>4500]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>SalePrice</th>
<th>GrLivArea</th>
</tr>
</thead>
<tbody>
<tr>
<th>523</th>
<td>184750</td>
<td>4676</td>
</tr>
<tr>
<th>1298</th>
<td>160000</td>
<td>5642</td>
</tr>
</tbody>
</table>
</div>
# drop这2个异常点
X = X.drop(data[data[var]>4500].index)
y = y.drop(data[data[var]>4500].index)
var = 'GrLivArea'
data = pd.concat([y, X[var]], axis=1)
data.plot.scatter(x=var, y='SalePrice')
<matplotlib.axes._subplots.AxesSubplot at 0x1fc3d329860>
# 查看y值的分布情况,图形显示正偏度
from scipy.stats import norm
import scipy.stats as stats
sns.distplot(y, fit=norm)
fig = plt.figure()
res = stats.probplot(y, plot=plt)
C:\Program Files\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.
warnings.warn("The 'normed' kwarg is deprecated, and has been "
# 取对数处理正偏度情况
y = np.log(y)
from scipy.stats import norm
import scipy.stats as stats
sns.distplot(y, fit=norm)
fig = plt.figure()
res = stats.probplot(y, plot=plt)
C:\Program Files\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.
warnings.warn("The 'normed' kwarg is deprecated, and has been "
# 拆分训练集和验证集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=.33)
# 使用线性回归进行拟合,使用验证集进行评估
from sklearn import linear_model
lr = linear_model.LinearRegression()
model = lr.fit(X_train, y_train)
pred = model.predict(X_test)
from sklearn.metrics import mean_squared_error
print('RMSE is: \n', mean_squared_error(y_test, pred))
RMSE is:
0.0168625962345
# 预测测试集数据,生成结果
test_pred = model.predict(test)
test_pred = np.exp(test_pred)
output = pd.DataFrame({'Id': df_test['Id'], 'SalePrice': test_pred})
output.to_csv('output.csv', index=False)