-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
231 lines (181 loc) · 7.4 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import numpy as np
import pandas as pd
import joblib
import matplotlib.pyplot as plot
import matplotlib.pyplot as plt
import seaborn as sns
import streamlit as st
from PIL import Image
from sklearn.linear_model import LogisticRegression
import json
st.write("""
# White-or-red-wine
### Which one to choose at all times?
""")
#Carga del modelo guardado
model = joblib.load(open("src/modelo_wine.joblib", "rb"))
def data_preprocesador(df):
"""
función preprocesa la entrada del usuario
return type: pandas dataframe
"""
df.color = df.color.map({'white': 0, 'red': 1})
return df
def visualizacion(prediction_proba):
"""
crear un gráfico de barras de inferencia renderizado con streamlit en tiempo real
return type : matplotlib bar chart
"""
data = (prediction_proba[0]*100).round(2)
grad_percentage = pd.DataFrame(data=data, columns=['Percentage'], index=[
'Bajo', 'Mediano', 'Bueno', 'Excelente'])
ax = grad_percentage.plot(kind='barh', figsize=(
8, 6), color='#FB6942', zorder=30, width=0.5)
ax.legend().set_visible(False)
ax.set_xlim(xmin=0, xmax=100)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(True)
ax.spines['bottom'].set_visible(True)
ax.tick_params(axis="both", which="both", bottom="off", top="off",
labelbottom="on", left="off", right="off", labelleft="on")
value = ax.get_xticks()
for tick in value:
ax.axvline(x=tick, linestyle='dashed',
alpha=0.9, color='#FB6942', zorder=1)
ax.set_xlabel(" Percentage(%) Confidence Level",
labelpad=2, weight='bold', size=12)
ax.set_ylabel("Wine Quality", labelpad=10, weight='bold', size=12)
ax.set_title('Prediction Confidence Level ', fontdict=None,
loc='center', pad=None, weight='bold')
st.set_option('deprecation.showPyplotGlobalUse', False)
st.pyplot()
return
st.write("""Esta aplicación predice la ** Calidad del vino ** mediante la entrada de ** características del vino ** a través del ** panel lateral ** """)
# leer en la imagen del vino y renderizar con streamlit
image = Image.open('image/blanco-vs-tinto.png')
st.image(image, caption='Tinto o Blanco', use_column_width=True)
codigo = st.expander('¿Necesitas Ayuda? 👉')
with codigo:
st.markdown(
"Encontraras todas la informacion del dataset en [Rusgar](https://github.com/rusgar/White-or-red-wine), estamos para ayudar ")
# colección de parámetros de entrada del usuario con side_bar
st.sidebar.header('Introduzca sus cualidades')
dataset = st.selectbox('Haz tu eleccion', ('Conjunto', 'White', 'Red'))
def get_data(dataset):
data_wine = pd.read_csv('data/df_wine.csv')
data_white = pd.read_csv('data/wine_final_white_todo.csv')
data_red = pd.read_csv('data/wine_final_red_todo.csv')
if dataset == 'Conjunto':
data = data_wine
else:
data = data_red
if dataset == 'White':
data = data_white
else:
data = data_red
return data
data_heatmap = get_data(dataset)
data = get_data(dataset)
def get_dataset(dataset):
bins = (1, 6, 10)
groups = ['1', '2']
data['quality'] = pd.cut(data['quality'], bins=bins, labels=groups)
x = data.drop(columns=['quality'])
y = data['quality']
return x, y
x, y = get_dataset(data)
st.write('Conjunto de datos:', data.shape)
with st.expander('Visualizacion'):
plot = st.selectbox('Selecione el tipo PLot',
('Histogram', 'Box Plot', 'Heat Map'))
if plot == 'Heat Map':
fig1 = plt.figure(figsize=(8, 6))
heatmap = sns.heatmap(data_heatmap.corr()[['quality']].sort_values(by='quality', ascending=False), vmin=-1,
vmax=1, annot=True)
heatmap.set_title('Correlacion respecto a la calidad',
fontdict={'fontsize': 20}, pad=20)
st.pyplot(fig1)
else:
feature = st.selectbox('Selecione su caracteristica', ('fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar',
'chlorides', 'free sulfur dioxide', 'total sulfur dioxide', 'density',
'pH', 'sulphates', 'alcohol'))
if plot == 'Histogram':
fig2 = plt.figure(figsize=(7, 5))
plt.xlabel(feature)
sns.distplot(x[feature])
st.pyplot(fig2)
else:
fig3 = plt.figure(figsize=(7, 5))
plt.ylabel(feature)
plt.boxplot(x=x[feature])
st.pyplot(fig3)
def get_user_input():
"""
obtener la entrada del usuario usando sidebar slider and selectbox
return type : pandas dataframe
"""
color = st.sidebar.selectbox(
"Seleccione el tipo de Vino", ("white", "red"))
fixed_acidity = st.sidebar.slider('Fixed Acidity', 4.8, 15.9, 7.12)
volatile_acidity = st.sidebar.slider('Volatile Acidity', 0.08, 1.58, 0.32)
citric_acid = st.sidebar.slider('Citric Acid', 0.000, 1.000, 0.305)
residual_sugar = st.sidebar.slider('Residual Sugar', 0.6, 22.0, 5.4)
chlorides = st.sidebar.slider('Chlorides', 0.015, 0.611, 0.051)
free_sulfur_dioxide = st.sidebar.slider(
'Free Sulfur Dioxide', 1.00, 80.00, 30.12)
total_sulfur_dioxide = st.sidebar.slider(
'Total Sulfur Dioxide', 6.00, 285.00, 115.17)
density = st.sidebar.slider('Density', 0.967, 1.501, 0.994)
pH = st.sidebar.slider('Ph', 2.82, 4.68, 3.21)
sulphates = st.sidebar.slider('Sulphates', 0.22, 1.98, 0.51)
alcohol = st.sidebar.slider('Alcohol', 8.4, 14.9, 10.49)
nombres = {'color': color,
'fixed_acidity': fixed_acidity,
'volatile_acidity': volatile_acidity,
'citric_acid': citric_acid,
'residual_sugar': residual_sugar,
'chlorides': chlorides,
'free_sulfur_dioxide': free_sulfur_dioxide,
'total_sulfur_dioxide': total_sulfur_dioxide,
'density': density,
'pH': pH,
'sulphates': sulphates,
'alcohol': alcohol
}
data = pd.DataFrame(nombres, index=[0])
return data
input_df = get_user_input()
procesar_input = data_preprocesador(input_df)
st.write(input_df)
data = pd.read_csv("data/df_wine.csv")
X = np.array(data[['color', 'fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar',
'chlorides', 'free sulfur dioxide', 'total sulfur dioxide', 'density', 'pH', 'alcohol', 'sulphates']])
Y = np.array(data['quality'])
model = LogisticRegression()
model.fit(X, Y)
st.subheader('Etiquetas de clase y su número de índice correspondiente')
st.write(pd.DataFrame({
'wine quality': [4, 5, 6, 7]}))
predict = model.predict(input_df)
predict_probability = model.predict_proba(input_df)
st.subheader('Probabilidad que salga segun su calidad')
st.write(predict_probability)
predict = model.predict(procesar_input)
predict_probability = model.predict_proba(procesar_input)
visualizacion(predict_probability)
st.subheader('Prediccion del vino')
type_labels = {
4: 'Malo',
5: 'Mediano',
6: 'Bueno',
7: 'Excelente'
}
st.write(
type_labels.get(model.predict(input_df)[0]))
st.write(predict)
st.markdown('4:Malo 5:Mediano 6:Bueno 7:Excelente')
"""
Created on Wed Oct 27 18:05:00 2022
@author: Rusgar
"""