-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachine_Learning_Algorithm_Windows.py
More file actions
149 lines (126 loc) · 4.88 KB
/
Copy pathMachine_Learning_Algorithm_Windows.py
File metadata and controls
149 lines (126 loc) · 4.88 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
#!/bin/bash
import pandas as pd
import numpy as np
from sklearn.utils import *
from sklearn import ensemble
from sklearn import linear_model
from sklearn.impute import SimpleImputer
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_validate
# Choose correct file path for evaluating model.
# #
df = pd.read_csv(r"D:\GitRepositories\Simple-Weather-Predictor\WeatherAPIs\API Outputs\castle_rock_data.csv")
#df = pd.read_csv(r"D:\GitRepositories\Simple-Weather-Predictor\WeatherAPIs\API Outputs\denver_data.csv")
#df = pd.read_csv(r"D:\GitRepositories\Simple-Weather-Predictor\WeatherAPIs\API Outputs\boulder_data.csv")
df.dropna(axis=0, how='any', thresh=None, subset=None, inplace=True)
# X represents the features that will be compared to the y.
# X values will try to predict according to values in y and
# X will try produce predictions with values matching y.
#
# get_dummies will make up the 'X' and other columns. It
# converts columns into evaulables.
#
# Please chgange the 'y' and 'x' interchangably
# when making different predictions.
#
# columns=['date','pattern','temperature','pressure','wind_speed','wind_degree']
# #
features_df = pd.get_dummies(df, columns=['date','pattern','wind_degree','pressure','wind_speed'])
X = np.array(features_df)
y = np.array(df['temperature'])
# Split data into test/train set (70/30 split) and shuffle
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
#
# Above is the format from the book. random_state removed in order to get varied results.
# #
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Setting up model. Make sure to look at "ensemble" documentation
# for correct alterations.
# #
model = ensemble.GradientBoostingRegressor(
n_estimators=250,
learning_rate=0.1,
max_depth=5,
min_samples_split=4,
min_samples_leaf=6,
max_features=0.6,
loss='huber'
)
# Run model on training data
model.fit(X_train, y_train)
# Setting X values for predictions to the y
# #
x_train_tofile = model.predict(X_train)
x_test_tofile = model.predict(X_test)
# Check model accuracy (up to two decimal places)
a_mse = mean_absolute_error(y_train, model.predict(X_train))
a_accuracy = 100-a_mse
b_mse = mean_absolute_error(y_test, model.predict(X_test))
b_accuracy = 100-b_mse
print ("Training Set Mean Absolute Error Rate: %.2f" % a_mse)
print ("Training Set Mean Absolute Accuracy Rate: %.2f" % a_accuracy )
print ("Test Set Mean Absolute Error Rare: %.2f" % b_mse)
print ("Test Set Mean Absolute Accuracy Rate: %.2f" % b_accuracy )
# Validation set
#
# Make sure this set is not overfitting. If not, then
# the algorithm is successful.
# #
lasso = linear_model.Lasso()
df_vpred = cross_val_predict(lasso, X, y, cv=3)
df_vresults = cross_validate(lasso, X, y, cv=3)
val_accuracy = df_vresults['test_score'][0]*100
val_error = 100-val_accuracy
print ("Validation Error Rate: %.2f" % val_error)
print ("Validation Accuracy Rate: %.2f" % val_accuracy)
# Prediction Model
#
# Uses imputer and RandomForestRegressor to make predictions.
# Export these values for prediction for the data.
# #
imputer = SimpleImputer()
p_mdl = ensemble.RandomForestRegressor(
n_estimators=100,
random_state=0,
n_jobs=6
)
# imputer hast to fit_transform prior to transform.
# Then, it can be used for the prediction.
# #
p_mdl.fit(X_train, y_train)
imputer.fit_transform(X_train)
xval = imputer.transform(X_train)
p = p_mdl.predict(xval)
p_mdl_mape = mean_absolute_error(y_train, p)
c_mse = 100-p_mdl_mape
print ("Prediction Set Mean Absolute Error Rate: %.2f" % p_mdl_mape)
print ("Prediction Set Mean Absolute Accuracy Rate: %.2f" % c_mse)
# Predicted Results
# #
print ("Predicted 3 Day Results:")
for i in range (3):
print ( "%.2f" % p[i] )
# Extracting the model to csv file.
# That way it can be graphed via Excel or
# another program.
# #
#a_lines_to_file = ""
#b_lines_to_file = ""
# Exporting Training Set Predictions
#for i in range (len(y_train)):
# a_lines_to_file += str(y_train[i])+","+str(x_train_tofile[i])+"\n"
#with open(r"D:\GitRepositories\Simple-Weather-Predictor\WeatherAPIs\API Outputs\y_train_out.csv","a+") as file1:
# file1.write(str(a_lines_to_file))
# Exporting Test Set Preddictions
#for i in range (len(y_test)):
# b_lines_to_file += str(y_test[i])+","+str(x_test_tofile[i])+"\n"
#with open(r"D:\GitRepositories\Simple-Weather-Predictor\WeatherAPIs\API Outputs\y_test_out.csv","a+") as file2:
# file2.write(str(b_lines_to_file))
# Exporting Predictions. Change range head length.
#for i in range (3):
# a_lines_to_file += str(p[i])+","+str(y_train[i])+"\n"
#with open(r"D:\GitRepositories\Simple-Weather-Predictor\WeatherAPIs\API Outputs\predict_col.csv","a+") as file1:
# file1.write(str(a_lines_to_file))