-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom_forest_model.py
More file actions
40 lines (32 loc) · 1.37 KB
/
random_forest_model.py
File metadata and controls
40 lines (32 loc) · 1.37 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
import data
import config
import metrics
import numpy as np
import matplotlib
import sklearn.svm as svm
import matplotlib.pyplot as plt
from sklearn import linear_model
import sklearn.ensemble as ensemble
from sklearn.model_selection import GridSearchCV
def rf_model(city='', savefile="models.rf_model"):
"""Train a linear regression model without regularization and obtain predictions on test-set"""
if not city:
X_train, y_train, X_val, y_val, X_test, y_test = data.load_dataset()
else:
X_train, y_train, X_val, y_val, X_test, y_test = data.load_dataset(config.datasets[city])
clf = ensemble.RandomForestRegressor(n_jobs=-1)
parameter_grid = {'n_estimators': [7, 10, 15, 20, 30], 'max_features' : ['auto', 'sqrt', 'log2']}
regr_lm = GridSearchCV(clf, parameter_grid)
if city:
print('\nFitting model for ' + city)
else:
print("\nFitting global model")
regr_lm.fit( np.vstack((X_train, X_val)), np.vstack((y_train, y_val)) )
print("Predicting values")
pred_val = regr_lm.predict(X_val)
pred_test = regr_lm.predict(X_test)
results_heading = 'Random Forest Regressor'
results_heading += "\nBest params: " + str(regr_lm.get_params())
return metrics.generate_results(city, y_val, y_test, pred_val, pred_test, savefile, results_heading)
# if __name__ == "__main__":
# metrics.get_model_results(rf_model)