-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
206 lines (144 loc) · 5.42 KB
/
Copy pathtrain.py
File metadata and controls
206 lines (144 loc) · 5.42 KB
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
import pandas as pd
import numpy as np
from sklearn import svm, tree, ensemble
import mysql.connector
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
from sklearn.model_selection import train_test_split, cross_val_score, learning_curve
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import pickle
import seaborn as sns
import json
import os
db_sql = os.getenv('DB_SQL')
db_name = os.getenv('DB_NAME')
db_port = os.getenv('DB_PORT')
# Get the data from the MySQL Azure Database:
### add your database connection here
db = mysql.connector.connect(
# user=<user>,
# password=<password>,
# host=<host>,
# port=<port>,
# database= <database>
)
cursor = db.cursor()
cursor.execute("SELECT * FROM sweden_property.housing_prices")
data = cursor.fetchall()
column_names = [desc[0] for desc in cursor.description]
cursor.close()
db.close()
df = pd.DataFrame(data, columns=column_names)
# Removing columns that will not be required for the model
del df["House_Name"]
del df["Price_Change"]
del df["Sold_date"]
del df["Charge"]
del df["Operating_cost"]
del df["Release_form"]
del df["Location"]
del df["Total_no_Floors"]
del df["Starting_price"]
# del df["S_No"]
del df["Floor"]
# Fixing the data type
df["Built_on"] = df["Built_on"].astype("category")
df = df.dropna(subset=['Municipality'])
df["Lift"] = df["Lift"].map({'No': 0, 'Yes': 1})
df["Balcony"] = df["Balcony"].map({'No': 0, 'Yes': 1})
# One-hot encoding
df = pd.get_dummies(df, columns=['Built_on'], dtype='int')
df = pd.get_dummies(df, columns=['House_type'], dtype='int')
# Numerical scaling
columns_to_normalize = ['Rooms', 'Living_area', 'Plot_area', 'Other_area']
scaler = StandardScaler()
# We will do the scaling after the train-test split to avoid data leakage
# Train-test split
X = df.drop(columns=["Final_Price"])
y = df["Final_Price"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.4, random_state=42)
X_train[columns_to_normalize] = scaler.fit_transform(
X_train[columns_to_normalize])
# To save the scaling so that it can be used in the web app for the user input
with open('scaler.pkl', 'wb') as f:
pickle.dump(scaler, f)
X_test[columns_to_normalize] = scaler.transform(
X_test[columns_to_normalize])
def target_encode(X, y):
encoded_dict = X.groupby(X).apply(
lambda x: y.loc[x.index].mean()).to_dict()
return X.map(encoded_dict), encoded_dict
encoded_train, encoding_dict = target_encode(X_train["Municipality"], y_train)
def apply_encoding(X, encoding_dict, default_value=None):
return X.map(encoding_dict).fillna(default_value)
default_value = y_train.mean()
encoded_test = apply_encoding(
X_test["Municipality"], encoding_dict, default_value)
X_train["Municipality"] = encoded_train
X_test["Municipality"] = encoded_test
with open("encoding_dict.pkl", "wb") as f:
pickle.dump(encoding_dict, f)
# CV plot
def plot_learning_curves(model, X, y):
train_sizes, train_scores, val_scores = learning_curve(
model, X, y, train_sizes=np.linspace(0.1, 1.0, 10), cv=5,
scoring='r2', shuffle=True, random_state=1
)
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std = np.std(train_scores, axis=1)
val_scores_mean = np.mean(val_scores, axis=1)
val_scores_std = np.std(val_scores, axis=1)
plt.figure(figsize=(10, 6))
plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std, alpha=0.1,
color="b")
plt.fill_between(train_sizes, val_scores_mean - val_scores_std,
val_scores_mean + val_scores_std, alpha=0.1, color="g")
plt.plot(train_sizes, train_scores_mean, 'o-', color="b",
label="Training score")
plt.plot(train_sizes, val_scores_mean, 'o-', color="g",
label="Cross-validation score")
plt.title("Learning Curves")
plt.xlabel("Training examples")
plt.ylabel("R-squared scores")
plt.legend(loc="best")
plt.grid()
# plt.show()
plt.tight_layout()
plt.savefig("Learning_curves.png", dpi=120)
plt.close()
# Model training
params = {
"n_estimators": 100,
"max_depth": 4,
"loss": "squared_error",
}
model = ensemble.GradientBoostingRegressor(**params)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
plot_learning_curves(model, X_train, y_train)
### Residual plot###
y_pred = model.predict(X_test)
res_df = pd.DataFrame(list(zip(y_test, y_pred)), columns=["true", "pred"])
ax = sns.scatterplot(x="true", y="pred", data=res_df)
ax.set_aspect('equal')
ax.set_xlabel('True value', fontsize=18)
ax.set_ylabel('Predicted value', fontsize=18)
ax.set_title('Residuals', fontsize=22)
ax.plot([0, 20], [0, 20], 'black', linewidth=1)
plt.tight_layout()
plt.savefig("residuals.png", dpi=120)
# Metrics
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
# with open("metrics.txt", 'w') as outfile:
# outfile.write(f"Mean Absolute error (Millions in Kr): {mae:.3f}\n")
# outfile.write(f"Mean Square error (Millions in Kr): {mse:.3f}\n")
# outfile.write(f"R-squared score: {r2:.3f}\n")
with open("metrics.json", "w") as outfile:
json.dump({"Mean Absolute error (Millions in Kr)": mae,
"Mean Square error (Millions in Kr)": mse, "R-squared score": r2}, outfile)
pickle.dump(model, open("model.pkl", "wb"))
# joblib.dump(model, 'model.joblib')