Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.datasets import load_breast_cancer
|
| 4 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.metrics import accuracy_score
|
| 7 |
+
|
| 8 |
+
# Load the breast cancer dataset
|
| 9 |
+
data = load_breast_cancer()
|
| 10 |
+
|
| 11 |
+
# Select only the 5 features for simplicity
|
| 12 |
+
X = data.data[:, [0, 1, 2, 3, 4]] # mean_radius, mean_texture, mean_perimeter, mean_area, mean_smoothness
|
| 13 |
+
y = data.target
|
| 14 |
+
|
| 15 |
+
# Split the dataset into training and testing sets
|
| 16 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 17 |
+
|
| 18 |
+
# Train a RandomForestClassifier
|
| 19 |
+
model = RandomForestClassifier(random_state=42)
|
| 20 |
+
model.fit(X_train, y_train)
|
| 21 |
+
|
| 22 |
+
# Get accuracy on test data
|
| 23 |
+
y_pred = model.predict(X_test)
|
| 24 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 25 |
+
|
| 26 |
+
# Streamlit app interface
|
| 27 |
+
st.title("Breast Cancer Detection App")
|
| 28 |
+
|
| 29 |
+
st.write("This app predicts whether the tumor is benign or malignant based on user input.")
|
| 30 |
+
|
| 31 |
+
# Function to get user input
|
| 32 |
+
def get_user_input():
|
| 33 |
+
mean_radius = st.slider('Mean Radius', float(X[:, 0].min()), float(X[:, 0].max()), float(X[:, 0].mean()))
|
| 34 |
+
mean_texture = st.slider('Mean Texture', float(X[:, 1].min()), float(X[:, 1].max()), float(X[:, 1].mean()))
|
| 35 |
+
mean_perimeter = st.slider('Mean Perimeter', float(X[:, 2].min()), float(X[:, 2].max()), float(X[:, 2].mean()))
|
| 36 |
+
mean_area = st.slider('Mean Area', float(X[:, 3].min()), float(X[:, 3].max()), float(X[:, 3].mean()))
|
| 37 |
+
mean_smoothness = st.slider('Mean Smoothness', float(X[:, 4].min()), float(X[:, 4].max()), float(X[:, 4].mean()))
|
| 38 |
+
|
| 39 |
+
# Create a dictionary for user inputs
|
| 40 |
+
user_data = {
|
| 41 |
+
'mean_radius': mean_radius,
|
| 42 |
+
'mean_texture': mean_texture,
|
| 43 |
+
'mean_perimeter': mean_perimeter,
|
| 44 |
+
'mean_area': mean_area,
|
| 45 |
+
'mean_smoothness': mean_smoothness
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
# Convert the dictionary to a dataframe
|
| 49 |
+
features = pd.DataFrame(user_data, index=[0])
|
| 50 |
+
return features
|
| 51 |
+
|
| 52 |
+
# Get the user input
|
| 53 |
+
user_input = get_user_input()
|
| 54 |
+
|
| 55 |
+
# Display the user input
|
| 56 |
+
st.subheader("User Input:")
|
| 57 |
+
st.write(user_input)
|
| 58 |
+
|
| 59 |
+
# Make prediction
|
| 60 |
+
prediction = model.predict(user_input)
|
| 61 |
+
|
| 62 |
+
# Output the result
|
| 63 |
+
if prediction[0] == 0:
|
| 64 |
+
st.write("### The prediction is: **Benign**")
|
| 65 |
+
else:
|
| 66 |
+
st.write("### The prediction is: **Malignant**")
|
| 67 |
+
|
| 68 |
+
# Show the accuracy of the model
|
| 69 |
+
st.write(f"Model Accuracy: {accuracy * 100:.2f}%")
|