Kolpitor commited on
Commit
193855f
1 Parent(s): 2d12d89

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +105 -0
streamlit_app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system('pip install pdpbox==0.2.1')
3
+
4
+ from pdpbox.pdp import pdp_isolate, pdp_plot
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.metrics import mean_absolute_error
7
+ from sklearn.linear_model import LinearRegression
8
+ from sklearn.pipeline import make_pipeline
9
+ from sklearn.preprocessing import StandardScaler
10
+ from sklearn.feature_selection import SelectKBest
11
+ from sklearn.ensemble import RandomForestRegressor
12
+ import pandas as pd
13
+ from numpy import mean
14
+ import streamlit as st
15
+
16
+ """
17
+ # IOT
18
+ """
19
+
20
+
21
+ max_depth_input = st.slider("Max depth", 1, 100, 5)
22
+ colsample_bytree_input = st.slider("Colsample bytree", 0.0, 1.0, 0.5)
23
+ learning_rate_input = st.slider("Learning rate", 0.0, 1.0, 0.2)
24
+ alpha_input = st.slider("Alpha", 1, 100, 10)
25
+ n_estimators_input = st.slider("n estimators", 1, 100, 20)
26
+ city_input = st.selectbox(
27
+ 'Which city do you want to predict rain ?',
28
+ ("Canberra",
29
+ "Albury",
30
+ "Penrith",
31
+ "Sydney",
32
+ "MountGinini",
33
+ "Bendigo",
34
+ "Brisbane",
35
+ "Portland"), index=0)
36
+
37
+
38
+ df = pd.read_csv("city_temperature.csv")
39
+
40
+ def mergeStateToCountry():
41
+ df.loc[df['State'].notna(), 'Country'] = df['State']
42
+ df = df.loc[:, ~df.columns.str.contains('State')]
43
+
44
+ i = 0
45
+
46
+ for region in df["Region"].unique():
47
+ df["Region"] = df["Region"].replace(region, str(i))
48
+ i += 1
49
+
50
+ i = 0
51
+
52
+ for country in df["Country"].unique():
53
+ df["Country"] = df["Country"].replace(country, str(i))
54
+ i += 1
55
+
56
+ i = 0
57
+
58
+ for state in df["State"].unique():
59
+ df["State"] = df["State"].replace(state, str(i))
60
+ i += 1
61
+
62
+ i = 0
63
+
64
+ for city in df["City"].unique():
65
+ df["City"] = df["City"].replace(city, str(i))
66
+ i += 1
67
+
68
+ df = df.astype({"Region": "int"})
69
+ df = df.astype({"Country": "int"})
70
+ df = df.astype({"State": "int"})
71
+ df = df.astype({"City": "int"})
72
+
73
+ target = 'AvgTemperature'
74
+ # Here Y would be our target
75
+ Y = df[target]
76
+ # Here X would contain the other column
77
+ #X = df.loc[:, df.columns != target]
78
+ X = df[['Month', 'Day', 'Year']]
79
+
80
+ X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.25, random_state=42)
81
+
82
+ y_pred = [Y_train.mean()] * len(Y_train)
83
+
84
+ st.write('Baseline MAE: %f' % (round(mean_absolute_error(Y_train, y_pred), 5)))
85
+
86
+ lm = make_pipeline(StandardScaler(), LinearRegression(),)
87
+
88
+ lm.fit(X_train, Y_train)
89
+
90
+ st.write('Linear Regression Training MAE: %f' % (round(mean_absolute_error(Y_train, lm.predict(X_train)), 5)))
91
+ st.write('Linear Regression Test MAE: %f' % (round(mean_absolute_error(Y_val, lm.predict(X_val)), 5)))
92
+
93
+ forestModel = make_pipeline(
94
+ SelectKBest(k="all"),
95
+ StandardScaler(),
96
+ RandomForestRegressor(
97
+ n_estimators=100,
98
+ max_depth=50,
99
+ random_state=77,
100
+ n_jobs=-1))
101
+
102
+ forestModel.fit (X_train, Y_train)
103
+
104
+ st.write('Random Forest Regressor Model Training MAE: %f' % (mean_absolute_error(Y_train, forestModel.predict(X_train))))
105
+ st.write('Random Forest Regressor Model Test MAE: %f' % (mean_absolute_error(Y_val, forestModel.predict(X_val))))