日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python回归分析预测模型_Python与线性回归模型预测房价

發(fā)布時(shí)間:2025/4/5 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python回归分析预测模型_Python与线性回归模型预测房价 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄提出問(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 處理分類(lèi)特征 —— One-hot encoding

為了提高數(shù)據(jù)分析的可信度,此處把所有的分類(lèi)數(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。