kaggle房价预测特征意思_Kaggle实战-波士顿房价预测
生活随笔
收集整理的這篇文章主要介紹了
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、數據相關性分析
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实战-波士顿房价预测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java参数传入泛型类型_Java 5.
- 下一篇: cordova打开文件_Cordova开