File size: 3,879 Bytes
ed82320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15ff0e4
 
 
 
 
 
 
 
 
 
004fd7d
15ff0e4
ed82320
15ff0e4
 
 
ed82320
 
15ff0e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt

# --- Function to perform SVM Regression and display results ---
def perform_svm_regression(df, problem_description, interpretation_text):
    st.write(problem_description)
    st.write("**Dataset:**")
    st.write(df)

    # Hyperparameters with unique keys
    kernel = st.selectbox("Kernel:", ("linear", "rbf", "poly"), key=f"kernel_{problem_description}")
    C = st.slider("C (Regularization parameter):", 0.1, 10.0, 1.0, 0.1, key=f"C_{problem_description}")
    epsilon = st.slider("Epsilon (Tolerance for errors):", 0.01, 1.0, 0.1, 0.01, key=f"epsilon_{problem_description}")

    # Train-test split
    X = df.iloc[:,:-1]
    y = df.iloc[:, -1]
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=42
    )

    # Model training
    model = SVR(kernel=kernel, C=C, epsilon=epsilon)
    model.fit(X_train, y_train)

    # Predictions
    y_pred = model.predict(X_test)

    # Evaluation metrics
    mse = mean_squared_error(y_test, y_pred)
    r2 = r2_score(y_test, y_pred)

    st.write(f"Mean Squared Error: {mse:.2f}")
    st.write(f"R-squared: {r2:.2f}")

    # Visualization
    fig, ax = plt.subplots()
    ax.scatter(y_test, y_pred)
    ax.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], "k--", lw=2)
    ax.set_xlabel("Actual")
    ax.set_ylabel("Predicted")
    st.pyplot(fig)

    # Interpretation
    st.write("**Interpretation:**")
    st.write(interpretation_text)


def main():
    # --- App ---
    st.title("SVM Regressor Demo")

    about = """This app demonstrates the use of Support Vector Machine (SVM) regression 
    for different problems: Business, Engineering, and Education.  Select a problem to 
    view the dataset, train an SVM model, and interpret the results.  Explore the 
    impact of hyperparameter tuning and visualize the predictions.

    Created by Louie F. Cervantes, M.Eng. (Information Engineering)"""
    with st.expander("About this App"):
        st.markdown(about) 

    # Tabs for each problem
    tab1, tab2, tab3 = st.tabs(
        ["Business Problem", "Engineering Problem", "Education Problem"]
    )

    # --- Business Problem ---
    with tab1:
        df = pd.read_csv("business_data.csv")
        perform_svm_regression(
            df,
            "**Business Problem:** Predicting customer churn based on usage patterns and demographics.",
            "The model predicts customer churn rate based on usage patterns and demographics. "
            "This information can be used to identify customers at risk of churning and take proactive steps to retain them.",
        )

    # --- Engineering Problem ---
    with tab2:
        df = pd.read_csv("engineering_data.csv")
        perform_svm_regression(
            df,
            "**Engineering Problem:** Predicting the remaining useful life of an industrial machine based on sensor data.",
            "The model predicts the remaining useful life of an industrial machine based on sensor data. "
            "This information can be used to schedule maintenance and prevent costly downtime.",
        )

    # --- Education Problem ---
    with tab3:
        df = pd.read_csv("education_data.csv")
        perform_svm_regression(
            df,
            "**Education Problem:** Predicting student performance on a standardized test based on study habits and previous grades.",
            "The model predicts student performance on a standardized test based on study habits and previous grades. "
            "This information can be used to identify students who may need extra help and provide them with appropriate support.",
        )

if __name__ == "__main__":
    main()