python回归分析预测模型_Python与线性回归模型预测房价
目錄提出問(wèn)題
理解數(shù)據(jù)
數(shù)據(jù)清洗
構(gòu)建模型
模型評(píng)估
總結(jié)
1. 提出問(wèn)題
房?jī)r(jià)和什么因素相關(guān)?進(jìn)而得知如何挑選房子?
2. 理解數(shù)據(jù)
2.1 導(dǎo)入數(shù)據(jù)
從Kaggle 中下載
2.2 導(dǎo)入數(shù)據(jù)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
導(dǎo)入訓(xùn)練數(shù)據(jù)與測(cè)試數(shù)據(jù)批量進(jìn)行數(shù)據(jù)清洗。
#訓(xùn)練數(shù)據(jù)集
df_train= pd.read_csv('./train.csv')
#測(cè)試數(shù)據(jù)集
df_test= pd.read_csv('./test.csv')
print("訓(xùn)練集數(shù)據(jù):",df_train.shape,"測(cè)試訓(xùn)練集:", df_test.shape)
訓(xùn)練集數(shù)據(jù): (1460, 81) 測(cè)試訓(xùn)練集: (1459, 80)
#合并數(shù)據(jù)集,以便同步數(shù)據(jù)清洗
full = df_train.append(df_test,ignore_index=True)
print("合并后的數(shù)據(jù)集:",full.shape)
合并后的數(shù)據(jù)集: (2919, 81)
2.3 查看數(shù)據(jù)集信息
full.columns
full.head()
full.info()由于數(shù)據(jù)項(xiàng)太多,這里只展示節(jié)選數(shù)據(jù)
3. 數(shù)據(jù)清洗
3.1 處理缺失值
數(shù)據(jù)有明顯缺失值,其中數(shù)據(jù)項(xiàng)Alley, Fence, MiscFeature, PoolQC 等缺失率高達(dá)90%,直接刪除它們。
full.drop(['Alley','Fence','MiscFeature','PoolQC'],axis=1,inplace=True)
3.2 處理分類特征 —— One-hot encoding
為了提高數(shù)據(jù)分析的可信度,此處把所有的分類數(shù)據(jù)降維,批量進(jìn)行獨(dú)熱編碼。
object_type=[]
for col in full.columns:
if full[col].dtype =="object":
object_type.append(col)
full_dummied_object = full
for ob in object_type:
objectDf=pd.DataFrame()
objectDf=pd.get_dummies(full_dummied_object[ob],prefix=ob)
full_dummied_object=pd.concat([full_dummied_object,objectDf],axis=1)
full_dummied_object.drop(ob,axis=1,inplace=True)
full_dummied_object.head()
3.3 數(shù)據(jù)相關(guān)性
數(shù)據(jù)太多,先找出和“標(biāo)簽”關(guān)聯(lián)性最大的10個(gè)“特征”再進(jìn)行下一步分析。
Correlation Martix (heatmap style)
通過(guò)熱力圖形象看出數(shù)據(jù)項(xiàng)之間的相關(guān)關(guān)系。
cor_mart=full_dummied_object.corr()
k=10
cols=cor_mart.nlargest(k,'SalePrice')['SalePrice'].index
cm=np.corrcoef(full_dummied_object[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()
顯示前10個(gè)相關(guān)性最強(qiáng)的特征。
cor_mart['SalePrice'].sort_values(ascending =False)[0:11]
# 特征選擇
full_X = full_dummied_object[['OverallQual','GrLivArea','GarageCars','GarageArea',
'TotalBsmtSF','1stFlrSF','FullBath','BsmtQual_Ex','TotRmsAbvGrd','YearBuilt']]
full_X.head()
4.構(gòu)建模型
4.1 建立訓(xùn)練數(shù)據(jù)集和測(cè)試數(shù)據(jù)集
#原始數(shù)據(jù)集有1460行
sourceRow=1460
#原始數(shù)據(jù)集:特征
source_X = full_X.loc[0:sourceRow-1,:]
#原始數(shù)據(jù)集:標(biāo)簽
source_y = full_dummied_object.loc[0:sourceRow-1,'SalePrice']
#預(yù)測(cè)數(shù)據(jù)集:特征
pred_X = full_X.loc[sourceRow:,:]
#原始數(shù)據(jù)集有多少行
print('原始數(shù)據(jù)集有多少行:',source_X.shape[0])
#預(yù)測(cè)數(shù)據(jù)集大小
print('原始數(shù)據(jù)集有多少行:',pred_X.shape[0])
原始數(shù)據(jù)集有多少行: 1460
原始數(shù)據(jù)集有多少行: 1459
from sklearn.model_selection import train_test_split
#建立模型用的訓(xùn)練數(shù)據(jù)集和測(cè)試數(shù)據(jù)集
train_X, test_X, train_y, test_y = train_test_split(source_X, source_y, train_size=.8)
4.2 選擇機(jī)器學(xué)習(xí)方法 --線性回歸
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(train_X , train_y)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
model.score(test_X, test_y)
0.8269044883096861
5. 總結(jié)
經(jīng)數(shù)據(jù)清洗后,建立的線性回歸模型準(zhǔn)確率為82.7%。
總結(jié)
以上是生活随笔為你收集整理的python回归分析预测模型_Python与线性回归模型预测房价的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 系统运维手册_如何摆脱“背锅侠”,做一个
- 下一篇: python给太阳花添加茎叶_pytho