Suchinthana commited on
Commit
4f9f9c2
·
1 Parent(s): 26f2246

Neural network approach

Browse files
Files changed (1) hide show
  1. app.py +88 -33
app.py CHANGED
@@ -3,7 +3,8 @@ import numpy as np
3
  import gradio as gr
4
  from sklearn.model_selection import train_test_split
5
  from sklearn.linear_model import LogisticRegression
6
- from sklearn.metrics import accuracy_score
 
7
  import matplotlib.pyplot as plt
8
 
9
  # Loading the dataset
@@ -17,24 +18,24 @@ y = df["good"]
17
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
18
 
19
  # Creating and fitting the logistic regression model
20
- model = LogisticRegression()
21
- model.fit(X_train, y_train)
22
 
23
- # Function to make predictions and display them on a graph
24
- def predict_and_plot(dirty, wait, lastyear, usa):
25
- # Making prediction for a single input
 
 
 
26
  input_data = np.array([[dirty, wait, lastyear, usa]])
27
- predicted_value = model.predict(input_data)[0]
28
 
29
- # Predicting on test set for comparison
30
- y_pred = model.predict(X_test)
31
 
32
- # Plotting Wait vs Good
33
  plt.figure(figsize=(12, 6))
34
-
35
- # Scatter plot for Wait vs Good
36
  plt.subplot(1, 2, 1)
37
- plt.scatter(X_test.iloc[:, 1], y_test, color='blue', alpha=0.6, label='Actual Good Values') # X_test[:, 1] for wait
38
  plt.scatter(wait, predicted_value, color='red', label=f'Input: Wait={wait}, Predicted Good={predicted_value:.2f}', zorder=5)
39
  plt.title('Wait vs Good')
40
  plt.xlabel('Wait')
@@ -42,9 +43,8 @@ def predict_and_plot(dirty, wait, lastyear, usa):
42
  plt.legend()
43
  plt.grid(True)
44
 
45
- # Plotting USA vs Good
46
  plt.subplot(1, 2, 2)
47
- plt.scatter(X_test.iloc[:, 3], y_test, color='blue', alpha=0.6, label='Actual Good Values') # X_test[:, 3] for usa
48
  plt.scatter(usa, predicted_value, color='red', label=f'Input: USA={usa}, Predicted Good={predicted_value:.2f}', zorder=5)
49
  plt.title('USA vs Good')
50
  plt.xlabel('USA')
@@ -52,32 +52,87 @@ def predict_and_plot(dirty, wait, lastyear, usa):
52
  plt.legend()
53
  plt.grid(True)
54
 
55
- # Adjust layout and save the plot
56
  plt.tight_layout()
57
- plt.savefig('output_plot.png')
58
  plt.close()
59
 
60
- return predicted_value, 'output_plot.png'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- # Creating Gradio UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  with gr.Blocks() as demo:
64
- gr.Markdown("# Logistic Regression Prediction")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- with gr.Row():
67
- dirty_slider = gr.Slider(minimum=0, maximum=6, step=0.01, label="Dirty")
68
- wait_slider = gr.Slider(minimum=0, maximum=5.3, step=0.01, label="Wait")
69
- lastyear_slider = gr.Slider(minimum=0, maximum=70, step=0.01, label="Last Year")
70
- usa_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="USA")
71
 
72
- predict_button = gr.Button("Predict")
73
 
74
- predicted_value_output = gr.Textbox(label="Predicted Value")
75
- plot_output = gr.Image(label="Actual vs Predicted Graph")
76
 
77
- predict_button.click(
78
- fn=predict_and_plot,
79
- inputs=[dirty_slider, wait_slider, lastyear_slider, usa_slider],
80
- outputs=[predicted_value_output, plot_output]
81
- )
82
 
83
  demo.launch()
 
3
  import gradio as gr
4
  from sklearn.model_selection import train_test_split
5
  from sklearn.linear_model import LogisticRegression
6
+ from sklearn.neural_network import MLPClassifier
7
+ from sklearn.metrics import accuracy_score, classification_report
8
  import matplotlib.pyplot as plt
9
 
10
  # Loading the dataset
 
18
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
19
 
20
  # Creating and fitting the logistic regression model
21
+ logreg_model = LogisticRegression()
22
+ logreg_model.fit(X_train, y_train)
23
 
24
+ # Creating and fitting the neural network model
25
+ nn_model = MLPClassifier(random_state=42)
26
+ nn_model.fit(X_train, y_train)
27
+
28
+ # Function to make predictions and display them on a graph (Logistic Regression)
29
+ def predict_and_plot_logreg(dirty, wait, lastyear, usa):
30
  input_data = np.array([[dirty, wait, lastyear, usa]])
31
+ predicted_value = logreg_model.predict(input_data)[0]
32
 
33
+ y_pred = logreg_model.predict(X_test)
 
34
 
 
35
  plt.figure(figsize=(12, 6))
36
+
 
37
  plt.subplot(1, 2, 1)
38
+ plt.scatter(X_test.iloc[:, 1], y_test, color='blue', alpha=0.6, label='Actual Good Values')
39
  plt.scatter(wait, predicted_value, color='red', label=f'Input: Wait={wait}, Predicted Good={predicted_value:.2f}', zorder=5)
40
  plt.title('Wait vs Good')
41
  plt.xlabel('Wait')
 
43
  plt.legend()
44
  plt.grid(True)
45
 
 
46
  plt.subplot(1, 2, 2)
47
+ plt.scatter(X_test.iloc[:, 3], y_test, color='blue', alpha=0.6, label='Actual Good Values')
48
  plt.scatter(usa, predicted_value, color='red', label=f'Input: USA={usa}, Predicted Good={predicted_value:.2f}', zorder=5)
49
  plt.title('USA vs Good')
50
  plt.xlabel('USA')
 
52
  plt.legend()
53
  plt.grid(True)
54
 
 
55
  plt.tight_layout()
56
+ plt.savefig('output_plot_logreg.png')
57
  plt.close()
58
 
59
+ return predicted_value, 'output_plot_logreg.png'
60
+
61
+ # Function to make predictions and display them on a graph (Neural Network)
62
+ def predict_and_plot_nn(dirty, wait, lastyear, usa):
63
+ input_data = np.array([[dirty, wait, lastyear, usa]])
64
+ predicted_value = nn_model.predict(input_data)[0]
65
+
66
+ y_pred = nn_model.predict(X_test)
67
+
68
+ plt.figure(figsize=(12, 6))
69
+
70
+ plt.subplot(1, 2, 1)
71
+ plt.scatter(X_test.iloc[:, 1], y_test, color='blue', alpha=0.6, label='Actual Good Values')
72
+ plt.scatter(wait, predicted_value, color='red', label=f'Input: Wait={wait}, Predicted Good={predicted_value:.2f}', zorder=5)
73
+ plt.title('Wait vs Good (NN)')
74
+ plt.xlabel('Wait')
75
+ plt.ylabel('Good')
76
+ plt.legend()
77
+ plt.grid(True)
78
 
79
+ plt.subplot(1, 2, 2)
80
+ plt.scatter(X_test.iloc[:, 3], y_test, color='blue', alpha=0.6, label='Actual Good Values')
81
+ plt.scatter(usa, predicted_value, color='red', label=f'Input: USA={usa}, Predicted Good={predicted_value:.2f}', zorder=5)
82
+ plt.title('USA vs Good (NN)')
83
+ plt.xlabel('USA')
84
+ plt.ylabel('Good')
85
+ plt.legend()
86
+ plt.grid(True)
87
+
88
+ plt.tight_layout()
89
+ plt.savefig('output_plot_nn.png')
90
+ plt.close()
91
+
92
+ return predicted_value, 'output_plot_nn.png'
93
+
94
+ # Creating Gradio UI with two tabs for Logistic Regression and Neural Network
95
  with gr.Blocks() as demo:
96
+ gr.Markdown("# Prediction Models")
97
+
98
+ with gr.Tab("Logistic Regression"):
99
+ gr.Markdown("### Logistic Regression Prediction")
100
+
101
+ with gr.Row():
102
+ dirty_slider = gr.Slider(minimum=0, maximum=6, step=0.01, label="Dirty")
103
+ wait_slider = gr.Slider(minimum=0, maximum=5.3, step=0.01, label="Wait")
104
+ lastyear_slider = gr.Slider(minimum=0, maximum=70, step=0.01, label="Last Year")
105
+ usa_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="USA")
106
+
107
+ predict_button_logreg = gr.Button("Predict")
108
+
109
+ predicted_value_output_logreg = gr.Textbox(label="Predicted Value (Logistic Regression)")
110
+ plot_output_logreg = gr.Image(label="Actual vs Predicted Graph (Logistic Regression)")
111
+
112
+ predict_button_logreg.click(
113
+ fn=predict_and_plot_logreg,
114
+ inputs=[dirty_slider, wait_slider, lastyear_slider, usa_slider],
115
+ outputs=[predicted_value_output_logreg, plot_output_logreg]
116
+ )
117
+
118
+ with gr.Tab("Neural Network"):
119
+ gr.Markdown("### Neural Network Prediction")
120
 
121
+ with gr.Row():
122
+ dirty_slider_nn = gr.Slider(minimum=0, maximum=6, step=0.01, label="Dirty")
123
+ wait_slider_nn = gr.Slider(minimum=0, maximum=5.3, step=0.01, label="Wait")
124
+ lastyear_slider_nn = gr.Slider(minimum=0, maximum=70, step=0.01, label="Last Year")
125
+ usa_slider_nn = gr.Slider(minimum=0, maximum=1, step=0.01, label="USA")
126
 
127
+ predict_button_nn = gr.Button("Predict")
128
 
129
+ predicted_value_output_nn = gr.Textbox(label="Predicted Value (Neural Network)")
130
+ plot_output_nn = gr.Image(label="Actual vs Predicted Graph (Neural Network)")
131
 
132
+ predict_button_nn.click(
133
+ fn=predict_and_plot_nn,
134
+ inputs=[dirty_slider_nn, wait_slider_nn, lastyear_slider_nn, usa_slider_nn],
135
+ outputs=[predicted_value_output_nn, plot_output_nn]
136
+ )
137
 
138
  demo.launch()