基于xgboost的房价预测

# -*- coding: utf-8 -*-
"""
Created on Wed Aug 23 13:36:42 2017

@author: Administrator
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import BaggingRegressor
from sklearn.ensemble import AdaBoostRegressor
from xgboost import XGBRegressor

# 文件的组织形式是house price文件夹下面放house_price.py和input文件夹
# input文件夹下面放的是从https://www.kaggle.com/c/house-prices-advanced-regression-techniques/data下载的train.csv  test.csv  sample_submission.csv 和 data_description.txt 四个文件


train_df = pd.read_excel("train.xls",index_col = 0)
test_df = pd.read_excel('test.xls',index_col = 0)

y_train = np.log1p(train_df.pop('SalePrice'))
all_df = pd.concat((train_df,test_df),axis = 0)



mean_cols = all_df.mean()
print (mean_cols.head(10))
all_dummy_df=all_df
all_dummy_df = all_dummy_df.fillna(mean_cols)
print (all_dummy_df.isnull().sum().sum())

# 标准化numerical数据
numeric_cols = all_df.columns[all_df.dtypes != 'object']
print (numeric_cols)


numeric_col_means = all_dummy_df.loc[:,numeric_cols].mean()
numeric_col_std = all_dummy_df.loc[:,numeric_cols].std()
all_dummy_df.loc[:,numeric_cols] = (all_dummy_df.loc[:,numeric_cols] - numeric_col_means) / numeric_col_std

# step4 建立模型
# 把数据处理之后,送回训练集和测试集
dummy_train_df = all_dummy_df.loc[train_df.index]
dummy_test_df = all_dummy_df.loc[test_df.index]
print (dummy_train_df.shape,dummy_test_df.shape)

# 将DF数据转换成Numpy Array的形式,更好地配合sklearn

X_train = dummy_train_df.values
X_test = dummy_test_df.values

# xgboost
params = [1,2,3,4,5,6]
test_scores = []
for param in params:
    clf = XGBRegressor(max_depth = param)
    test_score = np.sqrt(-cross_val_score(clf,X_train,y_train,cv = 100,scoring = 'neg_mean_squared_error'))
    test_scores.append(np.mean(test_score))
plt.plot(params,test_scores)
plt.title('max_depth vs CV Error')
plt.show()

xgb = XGBRegressor(max_depth =50)
xgb.fit(X_train, y_train)

y_final = np.expm1(xgb.predict(X_test))
# 提交结果
submission_df = pd.DataFrame(data = {'Id':test_df.index,'SalePrice':y_final})
#print (submission_df.head(10))
print (submission_df)
submission_df.to_csv('submission_xgboosting.csv',columns = ['Id','SalePrice'],index = False)

代码链接

这里写图片描述

posted @ 2022-08-19 22:59  luoganttcc  阅读(95)  评论(0编辑  收藏  举报