Spaces:
Sleeping
Sleeping
fixed the problem of the decision boundary plot
Browse files
app.py
CHANGED
@@ -7,6 +7,25 @@ from sklearn.model_selection import train_test_split
|
|
7 |
from sklearn.svm import SVC
|
8 |
from sklearn.metrics import confusion_matrix, classification_report
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Load the dataset
|
11 |
st.title("SVM Kernel Performance Comparison")
|
12 |
|
@@ -39,7 +58,7 @@ if uploaded_file:
|
|
39 |
y_pred = model.predict(X_test)
|
40 |
cm = confusion_matrix(y_test, y_pred)
|
41 |
cr = classification_report(y_test, y_pred, output_dict=True)
|
42 |
-
return cm, cr
|
43 |
|
44 |
# Streamlit tabs
|
45 |
tab1, tab2, tab3 = st.tabs(["Linear Kernel", "Polynomial Kernel", "RBF Kernel"])
|
@@ -47,7 +66,7 @@ if uploaded_file:
|
|
47 |
for tab, kernel in zip([tab1, tab2, tab3], ["linear", "poly", "rbf"]):
|
48 |
with tab:
|
49 |
st.write(f"## SVM with {kernel.capitalize()} Kernel")
|
50 |
-
cm, cr = evaluate_svm(kernel)
|
51 |
|
52 |
# Confusion matrix
|
53 |
st.write("### Confusion Matrix")
|
@@ -62,6 +81,10 @@ if uploaded_file:
|
|
62 |
st.write("### Classification Report")
|
63 |
st.dataframe(pd.DataFrame(cr).transpose())
|
64 |
|
|
|
|
|
|
|
|
|
65 |
# Explanation
|
66 |
explanation = {
|
67 |
"linear": "The linear kernel performs well when the data is linearly separable.",
|
|
|
7 |
from sklearn.svm import SVC
|
8 |
from sklearn.metrics import confusion_matrix, classification_report
|
9 |
|
10 |
+
# Function to visualize decision boundary
|
11 |
+
def visualize_classifier(classifier, X, y, title=''):
|
12 |
+
min_x, max_x = X[:, 0].min() - 1.0, X[:, 0].max() + 1.0
|
13 |
+
min_y, max_y = X[:, 1].min() - 1.0, X[:, 1].max() + 1.0
|
14 |
+
mesh_step_size = 0.01
|
15 |
+
x_vals, y_vals = np.meshgrid(np.arange(min_x, max_x, mesh_step_size),
|
16 |
+
np.arange(min_y, max_y, mesh_step_size))
|
17 |
+
output = classifier.predict(np.c_[x_vals.ravel(), y_vals.ravel()])
|
18 |
+
output = output.reshape(x_vals.shape)
|
19 |
+
fig, ax = plt.subplots()
|
20 |
+
ax.set_title(title)
|
21 |
+
ax.pcolormesh(x_vals, y_vals, output, cmap=plt.cm.gray, shading='auto')
|
22 |
+
ax.scatter(X[:, 0], X[:, 1], c=y, s=75, edgecolors='black', linewidth=1, cmap=plt.cm.Paired)
|
23 |
+
ax.set_xlim(x_vals.min(), x_vals.max())
|
24 |
+
ax.set_ylim(y_vals.min(), y_vals.max())
|
25 |
+
ax.set_xticks(np.arange(int(X[:, 0].min() - 1), int(X[:, 0].max() + 1), 1.0))
|
26 |
+
ax.set_yticks(np.arange(int(X[:, 1].min() - 1), int(X[:, 1].max() + 1), 1.0))
|
27 |
+
st.pyplot(fig)
|
28 |
+
|
29 |
# Load the dataset
|
30 |
st.title("SVM Kernel Performance Comparison")
|
31 |
|
|
|
58 |
y_pred = model.predict(X_test)
|
59 |
cm = confusion_matrix(y_test, y_pred)
|
60 |
cr = classification_report(y_test, y_pred, output_dict=True)
|
61 |
+
return model, cm, cr
|
62 |
|
63 |
# Streamlit tabs
|
64 |
tab1, tab2, tab3 = st.tabs(["Linear Kernel", "Polynomial Kernel", "RBF Kernel"])
|
|
|
66 |
for tab, kernel in zip([tab1, tab2, tab3], ["linear", "poly", "rbf"]):
|
67 |
with tab:
|
68 |
st.write(f"## SVM with {kernel.capitalize()} Kernel")
|
69 |
+
model, cm, cr = evaluate_svm(kernel)
|
70 |
|
71 |
# Confusion matrix
|
72 |
st.write("### Confusion Matrix")
|
|
|
81 |
st.write("### Classification Report")
|
82 |
st.dataframe(pd.DataFrame(cr).transpose())
|
83 |
|
84 |
+
# Decision boundary
|
85 |
+
st.write("### Decision Boundary")
|
86 |
+
visualize_classifier(model, X.to_numpy(), y.to_numpy(), title=f"Decision Boundary - {kernel.capitalize()} Kernel")
|
87 |
+
|
88 |
# Explanation
|
89 |
explanation = {
|
90 |
"linear": "The linear kernel performs well when the data is linearly separable.",
|