Spaces:
Sleeping
Sleeping
Suchinthana
commited on
Commit
·
832d3e4
1
Parent(s):
0d03136
UI code added
Browse files- app.py +69 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
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
|
10 |
+
df = pd.read_csv('assignment-2-k2461469.csv')
|
11 |
+
|
12 |
+
# Splitting the data into features and target variable
|
13 |
+
X = df[["dirty", "wait", "lastyear", "usa"]]
|
14 |
+
y = df["good"]
|
15 |
+
|
16 |
+
# Splitting the dataset into training and test sets
|
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 actual vs predicted values
|
33 |
+
plt.figure(figsize=(8, 6))
|
34 |
+
plt.scatter(range(len(y_test)), y_test, color='blue', label='Actual Values', alpha=0.6)
|
35 |
+
plt.scatter(range(len(y_pred)), y_pred, color='red', label='Predicted Values', alpha=0.6)
|
36 |
+
plt.title('Actual vs Predicted Values')
|
37 |
+
plt.xlabel('Sample Index')
|
38 |
+
plt.ylabel('Value')
|
39 |
+
plt.legend()
|
40 |
+
plt.grid(True)
|
41 |
+
|
42 |
+
# Save plot to a file and display
|
43 |
+
plt.savefig('output_plot.png')
|
44 |
+
plt.close()
|
45 |
+
|
46 |
+
return predicted_value, 'output_plot.png'
|
47 |
+
|
48 |
+
# Creating Gradio UI
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown("# Logistic Regression Prediction")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
dirty_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Dirty")
|
54 |
+
wait_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Wait")
|
55 |
+
lastyear_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Last Year")
|
56 |
+
usa_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="USA")
|
57 |
+
|
58 |
+
predict_button = gr.Button("Predict")
|
59 |
+
|
60 |
+
predicted_value_output = gr.Textbox(label="Predicted Value")
|
61 |
+
plot_output = gr.Image(label="Actual vs Predicted Graph")
|
62 |
+
|
63 |
+
predict_button.click(
|
64 |
+
fn=predict_and_plot,
|
65 |
+
inputs=[dirty_slider, wait_slider, lastyear_slider, usa_slider],
|
66 |
+
outputs=[predicted_value_output, plot_output]
|
67 |
+
)
|
68 |
+
|
69 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
numpy
|
3 |
+
gradio
|
4 |
+
scikit-learn
|
5 |
+
matplotlib
|