-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
157 lines (125 loc) · 5.44 KB
/
util.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
import json
import statistics
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from sklearn import ensemble
from sklearn.metrics import mean_squared_error
import streamlit as st
def read_map(df_mapping, provinsi):
f = open('indonesia.geojson')
indomap = json.load(f)
f.close()
provtemp = provinsi
provtemp.append('INDONESIA')
df_mapping['variabel'] = provtemp
df = pd.DataFrame(
{"Provinsi": pd.json_normalize(indomap["features"])["properties.state"]}
).assign(Columnnext=lambda d: d["Provinsi"].str.len())
mappingprovinsi = pd.read_csv('mappingprovinsi.csv')
dfnew = pd.DataFrame()
for i in mappingprovinsi['mappingtarget']:
df_mapping_temp = df_mapping.iloc[i]
dfnew = dfnew.append(df_mapping_temp)
dfnew = dfnew.reset_index(drop=True)
dfnew = pd.concat([dfnew, mappingprovinsi['provinsi']], axis=1, join="inner")
return dfnew, indomap
def plot_map(df, indomap, tahun):
fig = go.Figure(
data=go.Choropleth(
geojson=indomap,
locations=df["provinsi"],
featureidkey="properties.state",
z=df[int(tahun)],
colorscale="YlOrRd",
colorbar_title="Rentang",
)
)
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(autosize=False, width=1000, height=580)
fig.data[0].colorbar.x=-0.05
return fig
def prediction(dfprov):
namakolom = dfprov.columns[0]
X = dfprov.iloc[:, 1:12]
y = dfprov.iloc[:,0]
X_train = X[:10]
y_train = y[:10]
X_test = X[10:]
y_test = y[10:]
returns = {}
reg = ensemble.GradientBoostingRegressor(random_state=0)
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
returns['MSE'] = mse
returns['RMSE'] = rmse
returns['y_pred'] = y_pred
returns['y_test'] = y_test
feature_importance = reg.feature_importances_
importance_df = pd.DataFrame({'Fungsi Anggaran': X_train.columns,
'Keutamaan': feature_importance})
importance_df.sort_values(by='Keutamaan', ascending=False, inplace=True)
returns['importance_df'] = importance_df
importance_df2 = importance_df.drop(importance_df
[(importance_df['Fungsi Anggaran'] == 'Inflasi') | (importance_df['Fungsi Anggaran'] == 'KFD')].index)
feature=importance_df.iloc[:3,:1].T.values.tolist()
flat_list = [item for sublist in feature for item in sublist]
flat_list.append(namakolom)
feature2=importance_df2.iloc[:3,:1].T.values.tolist()
flat_list2 = [item for sublist in feature2 for item in sublist]
flat_list2.append(namakolom)
returns['dfprovawal'] = dfprov
dfprov2 = dfprov[dfprov.columns.intersection(flat_list2)]
dfprov = dfprov[dfprov.columns.intersection(flat_list)]
returns['dfprov'] = dfprov
returns['dfprovakhir'] = dfprov2
return returns
def is_feature_importance(temp):
if temp >= 0.5:
return 'background-color: lightgreen'
elif temp >= 0.2 and temp < 0.5:
return 'background-color: yellow'
elif temp >= 0.1 and temp < 0.2:
return 'background-color: orange'
else:
return 'background-color: pink'
def is_target(temp_df, col) :
for index, row in temp_df.iterrows():
deviasi = statistics.stdev(temp_df[col])*0.05
if (col == 'IPM' or col == 'LPE'):
if row['batas_bawah'] != '' and row['batas_atas']=='':
if row[col] >= row['batas_bawah']:
st.write( '🟩',int(row['tahun']), ': ',row[col])
elif row[col] < row['batas_bawah'] and row[col] >= row['batas_bawah']-deviasi:
st.write( '🟨',int(row['tahun']), ': ',row[col])
elif row[col] < row['batas_bawah']-deviasi:
st.write( '🟥', int(row['tahun']), ': ',row[col])
elif row['batas_bawah']!='' and row['batas_atas']!='':
if row[col] >= row['batas_bawah']:
st.write( '🟩',int(row['tahun']), ': ',row[col])
else:
if row[col] >= row['batas_bawah']-deviasi:
st.write( '🟨',int(row['tahun']), ': ',row[col])
elif row[col] < row['batas_bawah']-deviasi:
st.write( '🟥',int(row['tahun']), ': ',row[col])
else:
st.write( '⬜', int(row['tahun']), ': ',row[col])
else:
if row['batas_bawah'] != '' and row['batas_atas']=='':
if row[col] > row['batas_bawah'] and row[col] <= row['batas_bawah']+deviasi:
st.write( '🟨',int(row['tahun']), ': ',row[col])
elif row[col] > row['batas_bawah'] and row[col] > row['batas_bawah']+deviasi:
st.write( '🟥',int(row['tahun']), ': ',row[col])
elif row[col] <= row['batas_bawah']:
st.write( '🟩',int(row['tahun']), ': ',row[col])
elif row['batas_bawah']!='' and row['batas_atas']!='':
if row[col] <= row['batas_atas']:
st.write( '🟩',int(row['tahun']), ': ',row[col])
elif row[col] > row['batas_atas'] and row[col] <= row['batas_atas']+deviasi:
st.write( '🟨',int(row['tahun']), ': ',row[col])
elif row[col] > row['batas_atas'] and row[col] > row['batas_atas']+deviasi:
st.write( '🟥',int(row['tahun']), ': ',row[col])
else:
st.write( '⬜', int(row['tahun']), ': ',row[col])