-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
130 lines (91 loc) · 3.59 KB
/
train.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
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, StandardScaler
from sklearn.svm import SVR
from sklearn.linear_model import RandomizedLasso
from sklearn.ensemble import RandomForestRegressor
from sklearn.feature_selection import RFECV
from sklearn.model_selection import KFold, GridSearchCV
les = dict()
ohes = dict()
def preprocess_train(df):
drop_cols = ['Id']
df = df.drop(drop_cols, 1)
cat_columns = []
for col in df.columns:
if df[col].dtype.name == 'object':
cat_columns.append(col)
for col in cat_columns:
les[col] = LabelEncoder()
df[col] = les[col].fit_transform(df[col])
for col in cat_columns:
df[col].fillna(np.nanmean(df[col].values), inplace = True)
encoded = []
for col in cat_columns:
ohes[col] = OneHotEncoder()
encoded_data = ohes[col].fit_transform(df[col].values.reshape(-1, 1)).toarray()
n_columns = encoded_data.shape[1]
columns = [col + '_%d' % i for i in range(n_columns)]
encoded.append(pd.DataFrame(data = encoded_data, columns = columns))
df_encoded = pd.concat(encoded, axis = 1)
df_encoded.index = df.index
df.drop(cat_columns, 1, inplace = True)
df = pd.concat([df, df_encoded], axis = 1)
return df
def preprocess(df):
drop_cols = ['Id']
df = df.drop(drop_cols, 1)
cat_columns = []
for col in df.columns:
if df[col].dtype.name == 'object':
cat_columns.append(col)
for col in cat_columns:
df[col] = les[col].transform(df[col])
for col in df.columns:
df[col].fillna(np.nanmean(df[col].values), inplace = True)
encoded = []
for col in cat_columns:
encoded_data = ohes[col].transform(df[col].values.reshape(-1, 1)).toarray()
n_columns = encoded_data.shape[1]
columns = [col + '_%d' % i for i in range(n_columns)]
encoded.append(pd.DataFrame(data = encoded_data, columns = columns))
df_encoded = pd.concat(encoded, axis = 1)
df_encoded.index = df.index
df.drop(cat_columns, 1, inplace = True)
df = pd.concat([df, df_encoded], axis = 1)
return df
def SVR_model():
model = SVR()
param_grid = dict(
C = np.logspace(-3, 3, 10),
epsilon = np.logspace(-3, 3, 10)
)
kf = KFold(n_splits = 5, shuffle = True, random_state = np.random.randint(1000))
search = GridSearchCV(model, param_grid = param_grid, cv = kf, scoring = 'r2')
return search
def forest_model():
return RandomForestRegressor(n_estimators = 300)
if __name__ == '__main__':
np.random.seed(0)
train_data = pd.read_csv('data/train.csv')
test_data = pd.read_csv('data/test.csv')
train_test_data = pd.concat([train_data, test_data])
preprocess_train(train_test_data)
print('Training...')
train = preprocess(train_data)
test = preprocess(test_data)
X = train.drop('SalePrice', 1).values
y = train['SalePrice'].values
scaler = StandardScaler()
X = scaler.fit_transform(X)
model = forest_model()
model.fit(X, y)
print('Testing...')
X = test.values
X = scaler.transform(X)
y = model.predict(X)
predictions = pd.DataFrame(dict({
'SalePrice': y,
'Id': test_data['Id']
}))
predictions.to_csv('data/prediction.csv', index = False)