Spaces:
Sleeping
Sleeping
File size: 4,058 Bytes
832d3e4 778ce34 832d3e4 778ce34 832d3e4 |
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 |
import pandas as pd
import numpy as np
import gradio as gr
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
# Loading the dataset
df = pd.read_csv('assignment-2-k2461469.csv')
# Splitting the data into features and target variable
X = df[["dirty", "wait", "lastyear", "usa"]]
y = df["good"]
# Splitting the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Creating and fitting the logistic regression model
model = LogisticRegression()
model.fit(X_train, y_train)
# Function to make predictions and display them on a graph
def predict_and_plot(dirty, wait, lastyear, usa):
# Making prediction for a single input
input_data = np.array([[dirty, wait, lastyear, usa]])
predicted_value = model.predict(input_data)[0]
# Predicting on test set for comparison
y_pred = model.predict(X_test)
# Creating subplots for each variable and showing predicted value
fig, axs = plt.subplots(2, 2, figsize=(12, 10))
# Plot dirty variable distribution with predicted value
axs[0, 0].hist(X_test[:, 0], bins=30, color='gray', alpha=0.5, label='Dirty Distribution')
axs[0, 0].axvline(dirty, color='orange', linestyle='--', label='Input Value (Dirty)')
axs[0, 0].axvline(predicted_value, color='red', linestyle='-', label='Predicted Value')
axs[0, 0].set_title('Distribution of Dirty')
axs[0, 0].set_xlabel('Dirty')
axs[0, 0].set_ylabel('Frequency')
axs[0, 0].legend()
# Plot wait variable distribution with predicted value
axs[0, 1].hist(X_test[:, 1], bins=30, color='gray', alpha=0.5, label='Wait Distribution')
axs[0, 1].axvline(wait, color='orange', linestyle='--', label='Input Value (Wait)')
axs[0, 1].axvline(predicted_value, color='red', linestyle='-', label='Predicted Value')
axs[0, 1].set_title('Distribution of Wait')
axs[0, 1].set_xlabel('Wait')
axs[0, 1].set_ylabel('Frequency')
axs[0, 1].legend()
# Plot lastyear variable distribution with predicted value
axs[1, 0].hist(X_test[:, 2], bins=30, color='gray', alpha=0.5, label='Lastyear Distribution')
axs[1, 0].axvline(lastyear, color='orange', linestyle='--', label='Input Value (Lastyear)')
axs[1, 0].axvline(predicted_value, color='red', linestyle='-', label='Predicted Value')
axs[1, 0].set_title('Distribution of Lastyear')
axs[1, 0].set_xlabel('Lastyear')
axs[1, 0].set_ylabel('Frequency')
axs[1, 0].legend()
# Plot usa variable distribution with predicted value
axs[1, 1].hist(X_test[:, 3], bins=30, color='gray', alpha=0.5, label='USA Distribution')
axs[1, 1].axvline(usa, color='orange', linestyle='--', label='Input Value (USA)')
axs[1, 1].axvline(predicted_value, color='red', linestyle='-', label='Predicted Value')
axs[1, 1].set_title('Distribution of USA')
axs[1, 1].set_xlabel('USA')
axs[1, 1].set_ylabel('Frequency')
axs[1, 1].legend()
# Adjust layout and save the plot
plt.tight_layout()
plt.savefig('output_plot.png')
plt.close()
return predicted_value, 'output_plot.png'
# Creating Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# Logistic Regression Prediction")
with gr.Row():
dirty_slider = gr.Slider(minimum=0, maximum=6, step=0.01, label="Dirty")
wait_slider = gr.Slider(minimum=0, maximum=5.3, step=0.01, label="Wait")
lastyear_slider = gr.Slider(minimum=0, maximum=70, step=0.01, label="Last Year")
usa_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="USA")
predict_button = gr.Button("Predict")
predicted_value_output = gr.Textbox(label="Predicted Value")
plot_output = gr.Image(label="Actual vs Predicted Graph")
predict_button.click(
fn=predict_and_plot,
inputs=[dirty_slider, wait_slider, lastyear_slider, usa_slider],
outputs=[predicted_value_output, plot_output]
)
demo.launch()
|