forked from Kpearson72/CADO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
175 lines (139 loc) · 6.16 KB
/
app.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
# This app serve Machine Learning to the templates
from flask import Flask, render_template, jsonify, send_from_directory, request
import json
import pandas as pd
import numpy as np
import os
from joblib import load
import pickle
# init app and class
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
# Flask routes
# Homepage
@app.route('/')
def home():
return render_template('index.html')
@app.route('/analysis')
def analysis():
return render_template('analysis.html')
@app.route('/ml')
def machine():
return render_template('machine_learning.html', predictions=0.00)
@app.route('/tables')
def tables():
return render_template('tables.html')
@app.route('/resources')
def resources():
return render_template('resources.html')
@app.route('/about')
def about():
return render_template('about.html')
"""
columns =
'total_volume', 'year', 'Albany', 'Atlanta', 'Baltimore/Washington',
'Boise', 'Boston', 'Buffalo/Rochester', 'California', 'Charlotte',
'Chicago', 'Cincinnati/Dayton', 'Columbus', 'Dallas/Ft. Worth',
'Denver', 'Detroit', 'Grand Rapids', 'Great Lakes',
'Harrisburg/Scranton', 'Hartford/Springfield', 'Houston',
'Indianapolis', 'Jacksonville', 'Las Vegas', 'Los Angeles',
'Louisville', 'Miami/Ft. Lauderdale', 'Midsouth', 'Nashville',
'New Orleans/Mobile', 'New York', 'Northeast', 'Northern New England',
'Orlando', 'Philadelphia', 'Phoenix/Tucson', 'Pittsburgh', 'Plains',
'Portland', 'Raleigh/Greensboro', 'Richmond/Norfolk', 'Roanoke',
'Sacramento', 'San Diego', 'San Francisco', 'Seattle', 'South Carolina',
'South Central', 'Southeast', 'Spokane', 'St. Louis', 'Syracuse',
'Tampa', 'Total U.S.', 'West', 'West Tex/New Mexico', '01', '02', '03',
'04', '05', '06', '07', '08', '09', '10', '11', '12'
'total_volume', 'year', 'Atlanta', 'Baltimore/Washington', 'Boise',
'Boston', 'Buffalo/Rochester', 'California', 'Charlotte', 'Chicago',
'Cincinnati/Dayton', 'Columbus', 'Dallas/Ft. Worth', 'Denver',
'Detroit', 'Grand Rapids', 'Great Lakes', 'Harrisburg/Scranton',
'Hartford/Springfield', 'Houston', 'Indianapolis', 'Jacksonville',
'Las Vegas', 'Los Angeles', 'Louisville', 'Miami/Ft. Lauderdale',
'Midsouth', 'Nashville', 'New Orleans/Mobile', 'New York', 'Northeast',
'Northern New England', 'Orlando', 'Philadelphia', 'Phoenix/Tucson',
'Pittsburgh', 'Plains', 'Portland', 'Raleigh/Greensboro',
'Richmond/Norfolk', 'Roanoke', 'Sacramento', 'San Diego',
'San Francisco', 'Seattle', 'South Carolina', 'South Central',
'Southeast', 'Spokane', 'St. Louis', 'Syracuse', 'Tampa', 'Total U.S.',
'West', 'West Tex/New Mexico', '02', '03', '04', '05', '06', '07', '08',
'09', '10', '11', '12'
"""
# Machine Learning Predictions Route
# methods = "POST" is how get the input that the user input in the UI
@app.route('/makePredictions', methods=['POST'])
def predictions():
# post data = user input. form is a tag in the html with the method post
post_data = request.form
#load the model: (using pickle)
# Previously, we need to save the model in the notebook.
filename = 'finalized_model.sav'
model = pickle.load(open(filename, 'rb'))
#### define the columns names to input the data to the model.
# We can get the columns used in the model by X_test.columns
columns = ['total_volume', 'year',
# 'Albany', # Avoid perfect multicollinearity for all dummy variables notebook cell:19
'Atlanta', 'Baltimore/Washington',
'Boise', 'Boston', 'Buffalo/Rochester', 'California', 'Charlotte',
'Chicago', 'Cincinnati/Dayton', 'Columbus', 'Dallas/Ft. Worth',
'Denver', 'Detroit', 'Grand Rapids', 'Great Lakes',
'Harrisburg/Scranton', 'Hartford/Springfield', 'Houston',
'Indianapolis', 'Jacksonville', 'Las Vegas', 'Los Angeles',
'Louisville', 'Miami/Ft. Lauderdale', 'Midsouth', 'Nashville',
'New Orleans/Mobile', 'New York', 'Northeast', 'Northern New England',
'Orlando', 'Philadelphia', 'Phoenix/Tucson', 'Pittsburgh', 'Plains',
'Portland', 'Raleigh/Greensboro', 'Richmond/Norfolk', 'Roanoke',
'Sacramento', 'San Diego', 'San Francisco', 'Seattle', 'South Carolina',
'South Central', 'Southeast', 'Spokane', 'St. Louis', 'Syracuse',
'Tampa', 'Total U.S.', 'West', 'West Tex/New Mexico',
#'01', # Avoid perfect multicollinearity for all dummy variables notebook cell:19
'02', '03',
'04', '05', '06', '07', '08', '09', '10', '11', '12']
#Set to 0 every column in the model
data = [0 for i in range(len(columns))]
# format the volume as an integer
data[0] = int(post_data['total_volume'])
#Format year as integer
data[1] = int(post_data['year'])
# Set the column requested as True
data[columns.index(post_data['region'])]=1
data[columns.index(post_data['month'])]=1
# input the the data to the model
# here we are inputting the data to the model as a numpy array.
predictions = model.predict(np.array(data).reshape(1,-1))
#round the predictions to get the needed format
out = round(predictions[0],2) # 1.24
# here we have two options 1: is to return the data to the route (this is good to work in JS)
# 2nd: ours, set predictions as out, so and render the template. predictions is previously set as a variable in html.
return render_template('machine_learning.html', predictions=out) #jsonify({"prediction": out})
@app.route('/data')
def data():
df = pd.read_csv('Tableau/avocado-updated-2020.csv')
out = []
for index, row in df.iterrows():
date = row['date']
price = row['average_price']
volume = row['total_volume']
PLU_4046 = row['4046']
PLU_4225 = row['4225']
PLU_4770 = row['4770']
total_bags = row['total_bags']
type = row['type']
year = row['year']
geography = row['geography']
out.append({
'date': date,
'price': price,
'volume': volume,
'PLU_4046': PLU_4046,
'PLU_4225': PLU_4225,
'PLU_4770': PLU_4770,
'total_bags': total_bags,
'type': type,
'year': year,
'geography':geography
})
return jsonify(out)
if __name__ == '__main__':
app.run(debug=True)