-
Notifications
You must be signed in to change notification settings - Fork 43
/
app.py
157 lines (134 loc) · 5.36 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
# Import relevant libraries
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import plotly.express as px
import dash
from dash import dcc, html
from dash.dependencies import Input, Output, State
# Load dataset
data = pd.read_csv('data/winequality-red.csv')
# Check for missing values
data.isna().sum()
# Remove duplicate data
data.drop_duplicates(keep='first')
# Calculate the correlation matrix
corr_matrix = data.corr()
# Label quality into Good (1) and Bad (0)
data['quality'] = data['quality'].apply(lambda x: 1 if x >= 6.0 else 0)
# Drop the target variable
X = data.drop('quality', axis=1)
# Set the target variable as the label
y = data['quality']
# Split the dat a into training and testing sets (80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
# Create an instance of the logistic regression model
logreg_model = LogisticRegression()
# Fit the model to the training data
logreg_model.fit(X_train, y_train)
# Predict the labels of the test set
# y_pred = logreg_model.predict(X_test)
# Create the Dash app
app = dash.Dash(__name__)
server = app.server
# Define the layout of the dashboard
app.layout = html.Div(
children=[
html.H1('CO544-2023 Lab 3: Wine Quality Prediction'),
html.Div([
html.H3('Exploratory Data Analysis'),
html.Label('Feature 1 (X-axis)'),
dcc.Dropdown(
id='x_feature',
options=[{'label': col, 'value': col} for col in data.columns],
value=data.columns[0]
)
], style={'width': '30%', 'display': 'inline-block'}),
html.Div([
html.Label('Feature 2 (Y-axis)'),
dcc.Dropdown(
id='y_feature',
options=[{'label': col, 'value': col} for col in data.columns],
value=data.columns[1]
)
], style={'width': '30%', 'display': 'inline-block'}),
dcc.Graph(id='correlation_plot'),
# Wine quality prediction based on input feature values
html.H3("Wine Quality Prediction"),
html.Div([
html.Label("Fixed Acidity"),
dcc.Input(id='fixed_acidity', type='number', required=True),
html.Label("Volatile Acidity"),
dcc.Input(id='volatile_acidity', type='number', required=True),
html.Label("Citric Acid"),
dcc.Input(id='citric_acid', type='number', required=True),
html.Br(),
html.Label("Residual Sugar"),
dcc.Input(id='residual_sugar', type='number', required=True),
html.Label("Chlorides"),
dcc.Input(id='chlorides', type='number', required=True),
html.Label("Free Sulfur Dioxide"),
dcc.Input(id='free_sulfur_dioxide', type='number', required=True),
html.Br(),
html.Label("Total Sulfur Dioxide"),
dcc.Input(id='total_sulfur_dioxide', type='number', required=True),
html.Label("Density"),
dcc.Input(id='density', type='number', required=True),
html.Label("pH"),
dcc.Input(id='ph', type='number', required=True),
html.Br(),
html.Label("Sulphates"),
dcc.Input(id='sulphates', type='number', required=True),
html.Label("Alcohol"),
dcc.Input(id='alcohol', type='number', required=True),
html.Br(),
]),
html.Div([
html.Button('Predict', id='predict-button', n_clicks=0),
]),
html.Div([
html.H4("Predicted Quality"),
html.Div(id='prediction-output')
])
])
# Define the callback to update the correlation plot
@app.callback(
dash.dependencies.Output('correlation_plot', 'figure'),
[dash.dependencies.Input('x_feature', 'value'),
dash.dependencies.Input('y_feature', 'value')]
)
def update_correlation_plot(x_feature, y_feature):
fig = px.scatter(data, x=x_feature, y=y_feature, color='quality')
fig.update_layout(title=f"Correlation between {x_feature} and {y_feature}")
return fig
# Define the callback function to predict wine quality
@app.callback(
Output(component_id='prediction-output', component_property='children'),
[Input('predict-button', 'n_clicks')],
[State('fixed_acidity', 'value'),
State('volatile_acidity', 'value'),
State('citric_acid', 'value'),
State('residual_sugar', 'value'),
State('chlorides', 'value'),
State('free_sulfur_dioxide', 'value'),
State('total_sulfur_dioxide', 'value'),
State('density', 'value'),
State('ph', 'value'),
State('sulphates', 'value'),
State('alcohol', 'value')]
)
def predict_quality(n_clicks, fixed_acidity, volatile_acidity, citric_acid, residual_sugar,
chlorides, free_sulfur_dioxide, total_sulfur_dioxide, density, ph, sulphates, alcohol):
# Create input features array for prediction
input_features = np.array([fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides,
free_sulfur_dioxide, total_sulfur_dioxide, density, ph, sulphates, alcohol]).reshape(1, -1)
# Predict the wine quality (0 = bad, 1 = good)
prediction = logreg_model.predict(input_features)[0]
# Return the prediction
if prediction == 1:
return 'This wine is predicted to be good quality.'
else:
return 'This wine is predicted to be bad quality.'
if __name__ == '__main__':
app.run_server(debug=False)