kaggle House_Price_XGBoost
生活随笔
收集整理的這篇文章主要介紹了
kaggle House_Price_XGBoost
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
kaggle House_Price_final
代碼
import numpy as np import pandas as pd from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_absolute_error from sklearn.model_selection import train_test_split from sklearn.preprocessing import Imputer from sklearn.linear_model import LinearRegression from sklearn.tree import DecisionTreeRegressor import matplotlib.pyplot as plt from sklearn.preprocessing import Imputer from xgboost import XGBRegressortrain_path = r"C:\Users\cbattle\Desktop\train.csv" test_path = r"C:\Users\cbattle\Desktop\test.csv" out_path = r"C:\Users\cbattle\Desktop\out.csv"# 讀入數據 train = pd.read_csv(train_path) test = pd.read_csv(test_path) print('train:',train.shape) print('test :',test.shape)# 劃分X,y X = train.drop(['Id','SalePrice'],axis=1) y = train['SalePrice'] Xtest = test.drop(['Id'],axis=1) print('X :',X.shape) print('y :',y.shape) print('Xtest:',Xtest.shape) # for col in X: # print(X[col].dtype,col)key = [col for col in Xif X[col].dtype in ['int64','float64']or X[col].dtype == 'object' and X[col].nunique()<10] X = X[key] Xtest = Xtest[key]# 獨熱編碼 key = [col for col in Xif X[col].dtype in ['int64','float64']or X[col].dtype == 'object' and X[col].nunique()<10] X = X[key] Xtest = Xtest[key]print(X.shape, Xtest.shape) X = pd.get_dummies(X) Xtest = pd.get_dummies(Xtest) X, Xtest = X.align(Xtest, join = 'left', axis=1) print(X.shape, Xtest.shape)# 填補空值 my_imputer = Imputer() X = my_imputer.fit_transform(X) Xtest = my_imputer.transform(Xtest) print(X.shape, Xtest.shape)# 決策樹 # decisionTree = DecisionTreeRegressor() # decisionTree.fit(X,y) # ans = decisionTree.predict(Xtest)# XG boost xgb = XGBRegressor() xgb.fit(X,y,verbose=False) ans = xgb.predict(Xtest)# my_model = XGBRegressor(n_estimators=1000) # my_model.fit(train_X, train_y, early_stopping_rounds=5, # eval_set=[(val_X, val_y)], verbose=False) # ans = my_model.predict(Xtest)# 輸出 myAns = pd.DataFrame({'Id':test['Id'],'SalePrice':ans}) myAns.to_csv(r"C:\Users\cbattle\Desktop\out.csv", index=False) print('ok')轉載于:https://www.cnblogs.com/cbattle/p/8810851.html
總結
以上是生活随笔為你收集整理的kaggle House_Price_XGBoost的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可穿戴计算之父:成都能成为可穿戴技术未来
- 下一篇: 小何的酷