File size: 2,372 Bytes
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
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)

    # Plotting actual vs predicted values
    plt.figure(figsize=(8, 6))
    plt.scatter(range(len(y_test)), y_test, color='blue', label='Actual Values', alpha=0.6)
    plt.scatter(range(len(y_pred)), y_pred, color='red', label='Predicted Values', alpha=0.6)
    plt.title('Actual vs Predicted Values')
    plt.xlabel('Sample Index')
    plt.ylabel('Value')
    plt.legend()
    plt.grid(True)

    # Save plot to a file and display
    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=1, step=0.01, label="Dirty")
        wait_slider = gr.Slider(minimum=0, maximum=1, step=0.01, label="Wait")
        lastyear_slider = gr.Slider(minimum=0, maximum=1, 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()