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 XGBRegressor
train_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 = 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)
key = [col for col in X
if 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 X
if 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)
xgb = XGBRegressor()
xgb.fit(X,y,verbose=False)
ans = xgb.predict(Xtest)
myAns = pd.DataFrame({'Id':test['Id'],'SalePrice':ans})
myAns.to_csv(r"C:\Users\cbattle\Desktop\out.csv", index=False)
print('ok')
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步