eaglelandsonce commited on
Commit
028c091
·
verified ·
1 Parent(s): e297f98

Create 6_Logistic_Regression.py

Browse files
Files changed (1) hide show
  1. pages/6_Logistic_Regression.py +75 -0
pages/6_Logistic_Regression.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ from sklearn.datasets import load_iris
6
+ from sklearn.model_selection import train_test_split
7
+ 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()
14
+ X = iris.data
15
+ y = iris.target
16
+
17
+ # Only use the first two classes for binary classification
18
+ X = X[y != 2]
19
+ y = y[y != 2]
20
+
21
+ # Split the dataset into training and testing sets
22
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
23
+
24
+ # Standardize the data
25
+ 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=100, verbose=0)
36
+
37
+ # Predict and evaluate the model
38
+ y_pred_train = (model.predict(X_train) > 0.5).astype("int32")
39
+ y_pred_test = (model.predict(X_test) > 0.5).astype("int32")
40
+
41
+ train_accuracy = accuracy_score(y_train, y_pred_train)
42
+ 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}')
52
+
53
+ st.write('## Confusion Matrix')
54
+ fig, ax = plt.subplots()
55
+ ax.matshow(conf_matrix, cmap=plt.cm.Blues, alpha=0.3)
56
+ for i in range(conf_matrix.shape[0]):
57
+ for j in range(conf_matrix.shape[1]):
58
+ ax.text(x=j, y=i, s=conf_matrix[i, j], va='center', ha='center')
59
+
60
+ plt.xlabel('Predicted Label')
61
+ plt.ylabel('True Label')
62
+ st.pyplot(fig)
63
+
64
+ st.write('## Make a Prediction')
65
+ sepal_length = st.number_input('Sepal Length (cm)', min_value=0.0, max_value=10.0, value=5.0)
66
+ sepal_width = st.number_input('Sepal Width (cm)', min_value=0.0, max_value=10.0, value=3.5)
67
+ petal_length = st.number_input('Petal Length (cm)', min_value=0.0, max_value=10.0, value=1.4)
68
+ petal_width = st.number_input('Petal Width (cm)', min_value=0.0, max_value=10.0, value=0.2)
69
+
70
+ if st.button('Predict'):
71
+ input_data = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
72
+ input_data_scaled = scaler.transform(input_data)
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
+