-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhouse-pricing-lgbm-regression.py
183 lines (100 loc) · 3.42 KB
/
house-pricing-lgbm-regression.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from tensorflow import keras
from tensorflow.keras import layers
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import lightgbm as lgb
from lightgbm import LGBMRegressor
from sklearn.metrics import mean_squared_error
df = pd.read_csv(
'/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
y = pd.read_csv(
'/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
df.head(5)
# In[2]:
# Sort by nulls in columns
pd.set_option('display.max_rows', df.shape[0])
pd.DataFrame(df.isnull().sum().sort_values(ascending = False))
# In[3]:
# Delete columns with a lot of nulls (over50%)
df.drop(columns=['Id', 'Alley', 'PoolQC', 'Fence', 'MiscFeature', 'Utilities', 'FireplaceQu'],
inplace = True)
a = df.columns[df.isnull().any()]
# In another columns replace nulls - mode
for i in a:
df[i] = df[i].fillna(df[i].mode()[0])
df.head(10)
# In[4]:
# Extract SalePrice feature as target array
y = df['SalePrice']
del df['SalePrice']
# In[5]:
# Transfom objects in coloumns to int64
a = df.select_dtypes(include = object)
for i in a:
label_encoder = preprocessing.LabelEncoder()
df[i] = label_encoder.fit_transform(df[i])
df.drop(columns = [], inplace = True)
df.head(10)
# In[6]:
# Train/test splitting
x_train, x_test, y_train, y_test = train_test_split(
df, y, test_size = 0.2, random_state = 1337)
# In[7]:
#Building Model! LGBM Regressor is my favorite regressor for high-dim
lgbm = LGBMRegressor(objective = 'regression',
num_leaves = 13,
learning_rate = 0.034428,
n_estimators = 4235,
random_state = 1337)
# Fit'n'show rmse
lgbm.fit(x_train, y_train)
lgbm_train_predict = lgbm.predict(x_train)
rmse = np.sqrt(mean_squared_error(y_train, lgbm_train_predict))
print(rmse)
# In[8]:
# Prediction on specific example
pred_0 = lgbm.predict(df)
a = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
y_0 = a['SalePrice']
abs(pred_0[1337] / y_0[1337])
# In[9]:
# Preparing test data
test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
pd.set_option('display.max_rows', test.shape[0])
pd.DataFrame(test.isnull().sum().sort_values(ascending = False))
# In[10]:
# Convet test data as train
test.drop(columns=[
'Id', 'Alley', 'PoolQC', 'Fence', 'MiscFeature', 'Utilities', 'FireplaceQu'],
inplace = True)
a = test.columns[test.isnull().any()]
for i in a:
test[i] = test[i].fillna(test[i].mode()[0])
label_encoder = preprocessing.LabelEncoder()
a = test.select_dtypes(include = object)
for i in a:
label_encoder = preprocessing.LabelEncoder()
test[i] = label_encoder.fit_transform(test[i])
test.drop(columns = [], inplace = True)
test.head(10)
# In[11]:
# Make some predictions...!
sub = lgbm.predict(test)
sub = pd.DataFrame(sub)
# In[12]:
#Write to csv
submission = pd.read_csv(
'/kaggle/input/house-prices-advanced-regression-techniques/test.csv')
submission = submission['Id']
submission = pd.DataFrame(submission)
submission['SalePrice'] = sub
submission.to_csv('/kaggle/working/submission.csv', index = False)
# In[13]:
submission = pd.read_csv('/kaggle/working/submission.csv')
submission.head()