Spaces:
Sleeping
Sleeping
Update pages/6_Logistic_Regression.py
Browse files
pages/6_Logistic_Regression.py
CHANGED
@@ -8,6 +8,9 @@ from sklearn.preprocessing import StandardScaler
|
|
8 |
from sklearn.metrics import accuracy_score, confusion_matrix
|
9 |
from tensorflow.keras.models import Sequential
|
10 |
from tensorflow.keras.layers import Dense
|
|
|
|
|
|
|
11 |
|
12 |
# Load Iris dataset
|
13 |
iris = load_iris()
|
@@ -26,13 +29,61 @@ scaler = StandardScaler()
|
|
26 |
X_train = scaler.fit_transform(X_train)
|
27 |
X_test = scaler.transform(X_test)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Build the logistic regression model using Keras
|
30 |
model = Sequential()
|
31 |
model.add(Dense(1, input_dim=4, activation='sigmoid'))
|
32 |
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# Train the model
|
35 |
-
model.fit(X_train, y_train, epochs=
|
36 |
|
37 |
# Predict and evaluate the model
|
38 |
y_pred_train = (model.predict(X_train) > 0.5).astype("int32")
|
@@ -43,9 +94,6 @@ test_accuracy = accuracy_score(y_test, y_pred_test)
|
|
43 |
|
44 |
conf_matrix = confusion_matrix(y_test, y_pred_test)
|
45 |
|
46 |
-
# Streamlit interface
|
47 |
-
st.title('Logistic Regression with Keras on Iris Dataset')
|
48 |
-
|
49 |
st.write('## Model Performance')
|
50 |
st.write(f'Training Accuracy: {train_accuracy:.2f}')
|
51 |
st.write(f'Testing Accuracy: {test_accuracy:.2f}')
|
@@ -73,3 +121,18 @@ if st.button('Predict'):
|
|
73 |
prediction = (model.predict(input_data_scaled) > 0.5).astype("int32")
|
74 |
st.write(f'Prediction: {"Setosa" if prediction[0][0] == 0 else "Versicolor"}')
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from sklearn.metrics import accuracy_score, confusion_matrix
|
9 |
from tensorflow.keras.models import Sequential
|
10 |
from tensorflow.keras.layers import Dense
|
11 |
+
from tensorflow.keras.utils import plot_model
|
12 |
+
import io
|
13 |
+
from PIL import Image
|
14 |
|
15 |
# Load Iris dataset
|
16 |
iris = load_iris()
|
|
|
29 |
X_train = scaler.fit_transform(X_train)
|
30 |
X_test = scaler.transform(X_test)
|
31 |
|
32 |
+
# Streamlit interface
|
33 |
+
st.title('Logistic Regression with Keras on Iris Dataset')
|
34 |
+
st.write("""
|
35 |
+
## Introduction
|
36 |
+
Logistic Regression is a statistical model used for binary classification tasks.
|
37 |
+
In this tutorial, we will use the Iris dataset to classify whether a flower is
|
38 |
+
**Setosa** or **Versicolor** based on its features.
|
39 |
+
""")
|
40 |
+
|
41 |
+
# Display Iris dataset information
|
42 |
+
st.write("### Iris Dataset")
|
43 |
+
st.write("""
|
44 |
+
The Iris dataset contains 150 samples of iris flowers, each described by four features:
|
45 |
+
sepal length, sepal width, petal length, and petal width. There are three classes: Setosa, Versicolor, and Virginica.
|
46 |
+
For this example, we'll only use the Setosa and Versicolor classes.
|
47 |
+
""")
|
48 |
+
|
49 |
+
# Display flower images
|
50 |
+
st.write("### Flower Images")
|
51 |
+
col1, col2 = st.columns(2)
|
52 |
+
with col1:
|
53 |
+
st.image("https://upload.wikimedia.org/wikipedia/commons/5/56/Iris_setosa_2.jpg", caption="Iris Setosa", use_column_width=True)
|
54 |
+
with col2:
|
55 |
+
st.image("https://upload.wikimedia.org/wikipedia/commons/4/41/Iris_versicolor_3.jpg", caption="Iris Versicolor", use_column_width=True)
|
56 |
+
|
57 |
+
# Plotting sample data
|
58 |
+
st.write("### Sample Data Distribution")
|
59 |
+
fig, ax = plt.subplots()
|
60 |
+
for i, color in zip([0, 1], ['blue', 'orange']):
|
61 |
+
idx = np.where(y == i)
|
62 |
+
ax.scatter(X[idx, 0], X[idx, 1], c=color, label=iris.target_names[i], edgecolor='k')
|
63 |
+
ax.set_xlabel(iris.feature_names[0])
|
64 |
+
ax.set_ylabel(iris.feature_names[1])
|
65 |
+
ax.legend()
|
66 |
+
st.pyplot(fig)
|
67 |
+
|
68 |
+
# User input for number of epochs
|
69 |
+
epochs = st.slider('Select number of epochs for training:', min_value=10, max_value=200, value=100, step=10)
|
70 |
+
|
71 |
# Build the logistic regression model using Keras
|
72 |
model = Sequential()
|
73 |
model.add(Dense(1, input_dim=4, activation='sigmoid'))
|
74 |
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
75 |
|
76 |
+
# Display the model architecture
|
77 |
+
st.write("### Model Architecture")
|
78 |
+
st.write(model.summary())
|
79 |
+
fig, ax = plt.subplots()
|
80 |
+
buf = io.BytesIO()
|
81 |
+
plot_model(model, to_file=buf, show_shapes=True, show_layer_names=True)
|
82 |
+
buf.seek(0)
|
83 |
+
st.image(buf, caption='Logistic Regression Model Architecture', use_column_width=True)
|
84 |
+
|
85 |
# Train the model
|
86 |
+
model.fit(X_train, y_train, epochs=epochs, verbose=0)
|
87 |
|
88 |
# Predict and evaluate the model
|
89 |
y_pred_train = (model.predict(X_train) > 0.5).astype("int32")
|
|
|
94 |
|
95 |
conf_matrix = confusion_matrix(y_test, y_pred_test)
|
96 |
|
|
|
|
|
|
|
97 |
st.write('## Model Performance')
|
98 |
st.write(f'Training Accuracy: {train_accuracy:.2f}')
|
99 |
st.write(f'Testing Accuracy: {test_accuracy:.2f}')
|
|
|
121 |
prediction = (model.predict(input_data_scaled) > 0.5).astype("int32")
|
122 |
st.write(f'Prediction: {"Setosa" if prediction[0][0] == 0 else "Versicolor"}')
|
123 |
|
124 |
+
# Examples of different parameters for each flower type
|
125 |
+
st.write('## Examples of Parameters')
|
126 |
+
st.write("""
|
127 |
+
### Iris Setosa:
|
128 |
+
- Sepal Length: 5.1 cm
|
129 |
+
- Sepal Width: 3.5 cm
|
130 |
+
- Petal Length: 1.4 cm
|
131 |
+
- Petal Width: 0.2 cm
|
132 |
+
|
133 |
+
### Iris Versicolor:
|
134 |
+
- Sepal Length: 7.0 cm
|
135 |
+
- Sepal Width: 3.2 cm
|
136 |
+
- Petal Length: 4.7 cm
|
137 |
+
- Petal Width: 1.4 cm
|
138 |
+
""")
|