-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_housing.py
154 lines (129 loc) · 6.32 KB
/
code_housing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pandas as pd
import numpy as np
from sklearn.preprocessing import Imputer
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import FeatureUnion
from sklearn.preprocessing import FunctionTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import LabelBinarizer
from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score
from sklearn_pandas import DataFrameMapper
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from scipy.stats import randint as sp_randint
import xgboost as xgb
dataset = pd.read_csv("train.csv")
scaler = StandardScaler()
submission = pd.read_csv("test.csv")
dataset['is_paved'] = np.where(dataset['Street']=="Paved",1,0)
submission['is_paved'] = np.where(submission['Street']=="Paved",1,0)
y = np.log(dataset[["SalePrice"]].values.ravel())
numeric_columns = ["LotFrontage","LotArea",
"OverallQual","OverallCond",
"YearBuilt","YearRemodAdd",
"MasVnrArea","BsmtFinSF1",
"BsmtFinSF2","BsmtUnfSF",
"TotalBsmtSF","1stFlrSF","2ndFlrSF",
"LowQualFinSF","GrLivArea","BsmtFullBath",
"BsmtHalfBath","FullBath","HalfBath",
"BedroomAbvGr","KitchenAbvGr","TotRmsAbvGrd",
"Fireplaces","GarageYrBlt","GarageCars",
"GarageArea","WoodDeckSF","OpenPorchSF",
"EnclosedPorch","3SsnPorch",
"ScreenPorch","PoolArea","MiscVal",
"YrSold","is_paved"]
categorical_columns = ["MSSubClass","MSZoning","Street","Alley","LotShape",
"LandContour","Utilities","LotConfig","LandSlope",
"Neighborhood","Condition1","Condition2",
"BldgType","HouseStyle","RoofMatl","Exterior1st",
"Exterior2nd","MasVnrType","ExterQual","ExterCond",
"Foundation","BsmtCond","BsmtExposure","BsmtFinType1",
"BsmtFinType2","Heating",
"HeatingQC","CentralAir","Electrical",
"KitchenQual","Functional","FireplaceQu",
"GarageType","GarageFinish","GarageQual",
"GarageCond","PavedDrive","PoolQC",
"Fence","MiscFeature","MoSold","SaleType",
"SaleCondition","BsmtQual","RoofStyle"]
# replace infinite values
for cat in list(dataset.columns.values):
if dataset[cat].dtypes in ['float','int']:
dataset[cat]= dataset[cat].astype("float")
n = dataset.loc[dataset[cat] != np.inf, cat].max()
dataset[cat].replace(np.inf,n+1,inplace=True)
mean = dataset[cat].mean()
dataset[cat]=dataset[cat].fillna(mean)
if cat != "SalePrice":
submission[cat] = submission[cat].astype(dataset[cat].dtypes)
n = submission.loc[submission[cat] != np.inf, cat].max()
submission[cat].replace(np.inf,n+1,inplace=True)
mean = submission[cat].mean()
submission[cat]=submission[cat].fillna(mean)
numerical_df = dataset[numeric_columns]
sub_df = submission[numeric_columns]
ids = submission[["Id"]]
dataset[categorical_columns] = dataset[categorical_columns].fillna("missing")
submission[categorical_columns] = submission[categorical_columns].fillna("missing")
dataset[categorical_columns] = dataset[categorical_columns].astype("category")
submission[categorical_columns] = submission[categorical_columns].astype("category")
dummies = pd.get_dummies(dataset[categorical_columns])
dummies_sub = pd.get_dummies(submission[categorical_columns])
#test = dataset.loc[(~np.isfinite(dataset)) & dataset.notnull()]
#dataset2 = dataset.drop(categorical_columns,axis=1)
dataset = pd.concat([numerical_df,dummies],axis=1)
submission = pd.concat([sub_df,dummies_sub],axis=1)
#dataset = dataset.drop("Id",axis=1)
X = dataset
data_dmatrix = xgb.DMatrix(data=X,label=y)
train_X,test_X,train_y,test_y = train_test_split(X,y)
get_numeric_data = FunctionTransformer(lambda x: x[numeric_columns],
validate=False)
get_categorical_data= FunctionTransformer(lambda x:x[categorical_columns],
validate=False)
numeric = Pipeline([('selector',get_numeric_data),
('imp',SimpleImputer()),
('scale',StandardScaler())])
#categoric = Pipeline([
# ('select',get_categorical_data),
# ('impute',SimpleImputer(missing_values=None,strategy="constant",fill_value="missing")),
# ('encoder',DataFrameMapper([(categorical_columns,LabelBinarizer())]))
# ])
#categoric.fit_transform(test_X,test_y)
#union = FeatureUnion(transformer_list=[('numeric',numeric),('cat',categoric)])
#pl = Pipeline([('union',union),('linreg',LinearRegression())])
#pl= Pipeline([('num',numeric),('rf',RandomForestRegressor())])
pl = xgb.XGBRegressor(objective="reg:linear")
gbm_param_grid = {
'learning_rate': np.arange(0.05,1,0.05),
'max_depth': np.arange(3, 10, 1),
'n_estimators': np.arange(20, 200, 20),
'gamma' : [0,0.01,0.1,1,10]
}
# run randomized search
n_iter_search = 70
random_search = RandomizedSearchCV(pl, param_distributions=gbm_param_grid,
n_iter=n_iter_search, cv=5)
random_search.fit(train_X,train_y)
print(random_search.score(test_X,test_y))
prediction = random_search.predict(test_X)
plt.plot(test_y,prediction,'ro')
plt.show()
for var in list(X.columns.values):
if var not in list(submission.columns.values):
submission[var] = 0
for var in list(submission.columns.values):
if var not in list(X.columns.values):
submission=submission.drop(var,axis=1)
submission = submission[X.columns]
pred_sub = np.exp(random_search.predict(submission))
res_submit = pd.concat([ids,pd.DataFrame(pred_sub,index=submission.index,columns=["SalePrice"])],
axis=1)
res_submit["Id"] = res_submit["Id"].astype("int")
res_submit.to_csv("submission.csv",index=False)