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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kaggle房价预测特征意思_Kaggle实战-波士顿房价预测

發布時間:2025/3/19 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kaggle房价预测特征意思_Kaggle实战-波士顿房价预测 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文數據集來自Kaggle波士頓房價預測項目https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data
1、數據導入

import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np from scipy.stats import norm from sklearn.preprocessing import StandardScaler from scipy import stats import warnings warnings.filterwarnings('ignore') %matplotlib inlinetrain = pd.read_csv('input/train.csv') test = pd.read_csv('input/test.csv')


2、數據相關性分析

sample = pd.read_csv('input/sample_submission.csv')corrmat = train.corr() f, ax = plt.subplots(figsize=(12, 9)) sns.heatmap(corrmat, vmax=.8, square=True)

3、挑選相關變量

k = 10 cols = corrmat.nlargest(k, 'SalePrice')['SalePrice'].index cm = np.corrcoef(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()

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

train_Id = train['Id'] test_Id = test['Id'] len_train = train.shape[0] len_test = test.shape[0] housing = pd.concat([train, test], sort=False) housing.shape (2919, 81) housing[cols].columns Index(['SalePrice', 'OverallQual', 'GrLivArea', 'GarageCars', 'TotalBsmtSF','FullBath', 'YearBuilt'],dtype='object') housing = housing[cols]

4、處理缺失值

housing.isnull().sum() SalePrice 1459 OverallQual 0 GrLivArea 0 GarageCars 1 TotalBsmtSF 1 FullBath 0 YearBuilt 0 dtype: int64 housing.GarageCars = housing.GarageCars.fillna(housing.GarageCars.mean()) housing.TotalBsmtSF = housing.TotalBsmtSF.fillna(housing.TotalBsmtSF.mean()) train = housing[:len_train] test = housing[len_train:]

5、訓練模型

xtrain=train.drop("SalePrice",axis=1) ytrain=train['SalePrice'] xtest=test.drop("SalePrice", axis=1) from math import sqrt train_X, val_X, train_y, val_y = train_test_split(xtrain, ytrain, random_state=1) my_pipeline = XGBRegressor(n_estimators=1000, learning_rate=0.05) my_pipeline.fit(train_X, train_y) val_preds = my_pipeline.predict(val_X) msel = mean_squared_error(np.log(val_preds), np.log(val_y)) print("RMSE: %2f" %sqrt(msel)) RMSE: 0.173459 test_preds = my_pipeline.predict(xtest)output = pd.DataFrame({'Id': test_Id,'SalePrice': test_preds}) output.set_index('Id').to_csv('submission.csv')

6、異常值剔除

var = 'GrLivArea' data = pd.concat([train['SalePrice'], train[var]], axis=1) data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000)); 'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'. Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.

train = train.drop(train[train.index == 1298].index) train = train.drop(train[train.index == 523].index) var = 'GrLivArea' data = pd.concat([train['SalePrice'], train[var]], axis=1) data.plot.scatter(x=var, y='SalePrice', ylim=(0,800000)); 'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'. Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.

xtrain=train.drop("SalePrice",axis=1) ytrain=train['SalePrice'] xtest=test.drop("SalePrice", axis=1) from math import sqrt train_X, val_X, train_y, val_y = train_test_split(xtrain, ytrain, random_state=1)my_pipeline = XGBRegressor(n_estimators=1000, learning_rate=0.05) my_pipeline.fit(train_X, train_y)val_preds = my_pipeline.predict(val_X) msel = mean_squared_error(np.log(val_preds), np.log(val_y)) print("RMSE: %2f" %sqrt(msel)) RMSE: 0.162534 test_preds = my_pipeline.predict(xtest)output = pd.DataFrame({'Id': test_Id,'SalePrice': test_preds}) output.set_index('Id').to_csv('submission2.csv')

最終結果0.169,排名75%。后續優化方向,變量選擇、特征工程、模型選擇。

總結

以上是生活随笔為你收集整理的kaggle房价预测特征意思_Kaggle实战-波士顿房价预测的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。