-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial_regression.py
282 lines (231 loc) · 10 KB
/
polynomial_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
Created on 6/16/2017 by Ben
Polynomial Regression
"""
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
from sklearn.model_selection import train_test_split
from math import sqrt
def polynomial_df(feature, degree):
""" Takes in a pandas Series and returns a Dataframe of feature powers
up to a specified degree. """
poly_df = pd.DataFrame()
poly_df['power_1'] = feature
if degree > 1:
for power in range(2, degree+1):
name = 'power_' + str(power)
poly_df[name] = feature.apply(lambda x: x ** power)
return poly_df
def rmsle(num_observations, predictions, actual_value):
""" Return the Root Mean Squared Log Error.
This penalizes lower predictions more than higher ones. """
epsilon = sqrt((sum((np.log(predictions + 1) -
np.log(actual_value + 1)) ** 2)) / num_observations)
return epsilon
df = pd.read_csv("../data/train.csv")
# Suppress SettingWithCopyWarning
# default='warn'
pd.options.mode.chained_assignment = None
# Remove outliers
ulimit = np.percentile(df.price_doc.values, 99.5)
llimit = np.percentile(df.price_doc.values, 0.5)
df['price_doc'].loc[df['price_doc']>ulimit] = ulimit
df['price_doc'].loc[df['price_doc']<llimit] = llimit
col = "full_sq"
ulimit = np.percentile(df[col].values, 99.5)
llimit = np.percentile(df[col].values, 0.5)
df[col].loc[df[col]>ulimit] = ulimit
df[col].loc[df[col]<llimit] = llimit
# For plotting purposes, sort by 'full_sq'
df.sort_values(['full_sq', 'price_doc'], inplace=True)
# Degree one polynomial for now (straight line)
poly1_data = polynomial_df(df['full_sq'], 1)
# Add target 'price' to the new DataFrame
poly1_data['price'] = df['price_doc']
# Convert pandas Series to numpy array and reshape
xs = poly1_data.power_1.values.reshape(len(poly1_data['power_1']), 1)
ys = poly1_data.price.values
# Create linear regression object and train the model
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
print('Coefficients: \n', regr.coef_)
print('Intercept: \n', regr.intercept_)
print("Mean squared error: %.2f" % np.mean((regr.predict(xs) - ys) ** 2))
print('Variance score: %.2f' % regr.score(xs, ys))
plt.scatter(xs, ys, color='black')
plt.plot(xs, regr.predict(xs), color='blue', linewidth=3)
plt.ylabel('Price', fontsize=14)
plt.xlabel('Full square meters', fontsize=12)
plt.show()
print(rmsle(num_observations=len(xs),
predictions=regr.predict(xs),
actual_value=ys))
""" Now let's try it with higher order polynomials. """
# Degree two polynomial
poly2_data = polynomial_df(df['full_sq'], 2)
# Add target 'price' to the new DataFrame
poly2_data['price'] = df['price_doc']
# Convert pandas Series to numpy array
xs = poly2_data[['power_1', 'power_2']].values
ys = poly2_data.price.values
# Create linear regression object and train the model
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
print('Coefficients: \n', regr.coef_)
print('Intercept: \n', regr.intercept_)
print("Mean squared error: %.2f" % np.mean((regr.predict(xs) - ys) ** 2))
print('Variance score: %.2f' % regr.score(xs, ys))
plt.scatter(poly2_data['power_1'], ys, color='black')
plt.plot(poly2_data['power_1'], regr.predict(xs), color='blue', linewidth=3)
plt.ylabel('Price', fontsize=14)
plt.xlabel('Full square meters', fontsize=12)
plt.axis(xmin=20)
plt.show()
print(rmsle(num_observations=len(xs),
predictions=regr.predict(xs),
actual_value=ys))
# Now degree three polynomial
poly3_data = polynomial_df(df['full_sq'], 3)
# Add target 'price' to the new DataFrame
poly3_data['price'] = df['price_doc']
# Convert pandas Series to numpy array
xs = poly3_data[['power_1', 'power_2', 'power_3']].values
ys = poly3_data.price.values
# Create linear regression object and train the model
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
print('Coefficients: \n', regr.coef_)
print('Intercept: \n', regr.intercept_)
print("Mean squared error: %.2f" % np.mean((regr.predict(xs) - ys) ** 2))
print('Variance score: %.2f' % regr.score(xs, ys))
plt.scatter(poly3_data['power_1'], ys, color='black')
plt.plot(poly3_data['power_1'], regr.predict(xs), color='blue', linewidth=3)
plt.ylabel('Price', fontsize=14)
plt.xlabel('Full square meters', fontsize=12)
plt.axis(xmin=20)
plt.show()
print(rmsle(num_observations=len(xs),
predictions=regr.predict(xs),
actual_value=ys))
# Now degree seven polynomial, on all data
new_poly7_data = polynomial_df(df['full_sq'], 7)
my_features = new_poly7_data.columns.values
# Add target 'price' to the new DataFrame
new_poly7_data['price'] = df['price_doc']
# Convert pandas Series to numpy array
xs = new_poly7_data[my_features].values
ys = new_poly7_data.price.values
# Create linear regression object and train the model
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
print('Coefficients: \n', regr.coef_)
print('Intercept: \n', regr.intercept_)
print("Mean squared error: %.2f" % np.mean((regr.predict(xs) - ys) ** 2))
print('Variance score: %.2f' % regr.score(xs, ys))
plt.scatter(new_poly7_data['power_1'], ys, color='black')
plt.plot(new_poly7_data['power_1'], regr.predict(xs), color='blue', linewidth=3)
plt.ylabel('Price', fontsize=14)
plt.xlabel('Full square meters', fontsize=12)
plt.axis(xmin=20)
plt.show()
print(rmsle(num_observations=len(xs),
predictions=regr.predict(xs),
actual_value=ys))
""" We need to choose the best degree polynomial - validation. """
# Split data into training, validation, and testing data
x_train_and_valid, x_test, y_train_and_valid, y_test = train_test_split(
df['full_sq'].values, df['price_doc'].values,
test_size=0.1, random_state=12)
x_train, x_valid, y_train, y_valid = train_test_split(
x_train_and_valid[:], y_train_and_valid[:],
test_size=0.5, random_state=12)
x_test = pd.Series(x_test)
y_test = pd.Series(y_test)
x_train = pd.Series(x_train)
x_valid = pd.Series(x_valid)
y_train = pd.Series(y_train)
y_valid = pd.Series(y_valid)
def rss_validate(x_train, y_train, x_valid, y_valid, highest_degree):
"""Takes in training data, validation data and the highest acceptable
degree polynomial fit and returns a dict of RSS. """
from operator import sub
rss_list = {}
for degree in range(1, highest_degree+1):
if degree == 1:
poly_data = polynomial_df(x_train, degree)
poly_data['price'] = y_train
xs = poly_data.power_1.values.reshape(len(poly_data['power_1']), 1)
ys = poly_data.price.values
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
val_results = regr.predict(x_valid.values.reshape(len(x_valid), 1))
else:
poly_data = polynomial_df(x_train, degree)
my_features = poly_data.columns.values
poly_data['price'] = y_train
xs = poly_data[my_features].values
ys = y_train.values
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
val_data = polynomial_df(x_valid, degree)
val_features = val_data.columns.values
xs_val = val_data[val_features].values
val_results = regr.predict(xs_val)
diff = map(sub, val_results, y_valid)
RSS = sum(map(lambda x: x ** 2, diff))
rss_list[degree] = RSS
return rss_list
rss_valid_results = rss_validate(x_train, y_train, x_valid, y_valid, 15)
print(min(rss_valid_results, key=rss_valid_results.get))
def rmsle_validate(x_train, y_train, x_valid, y_valid, highest_degree):
"""Takes in training data, validation data and the highest acceptable
degree polynomial fit and returns a dict of RSMLE. """
rmsle_list = {}
for degree in range(1, highest_degree + 1):
if degree == 1:
poly_data = polynomial_df(x_train, degree)
poly_data['price'] = y_train
xs = poly_data.power_1.values.reshape(len(poly_data['power_1']), 1)
ys = poly_data.price.values
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
val_results = regr.predict(x_valid.values.reshape(len(x_valid), 1))
else:
poly_data = polynomial_df(x_train, degree)
my_features = poly_data.columns.values
poly_data['price'] = y_train
xs = poly_data[my_features].values
ys = y_train.values
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
val_data = polynomial_df(x_valid, degree)
val_features = val_data.columns.values
xs_val = val_data[val_features].values
val_results = regr.predict(xs_val)
rmsle_list[degree] = rmsle(num_observations=len(val_results),
predictions=val_results,
actual_value=y_valid)
return rmsle_list
rmsle_valid_results = rmsle_validate(x_train, y_train, x_valid, y_valid, 15)
print(rmsle_valid_results)
print(min(rmsle_valid_results, key=rmsle_valid_results.get))
poly7_data = polynomial_df(x_test, 7)
my_features = poly7_data.columns.values
poly7_data['price'] = y_test
poly7_data.sort_values(['power_1', 'price'], inplace=True)
xs = poly7_data[my_features].values
ys = poly7_data['price'].values
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(xs, ys)
plt.scatter(poly7_data['power_1'], ys, color='black')
plt.plot(poly7_data['power_1'], regr.predict(xs), color='blue', linewidth=3)
plt.ylabel('Price', fontsize=14)
plt.xlabel('Full square meters', fontsize=12)
plt.axis(xmin=20)
plt.show()
print(rmsle(num_observations=len(xs),
predictions=regr.predict(xs),
actual_value=ys))