Spaces:
Sleeping
Sleeping
File size: 4,503 Bytes
8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8f50eea 8ce0474 1ed6078 8869984 8f50eea 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 8869984 1ed6078 | 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | import streamlit as st
import pandas as pd
import warnings
# Filter out warnings
warnings.filterwarnings("ignore")
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
# --- Define your preprocessing pipeline ---
def create_preprocessing_pipeline():
# Define numerical and categorical feature names based on your dataset
numerical_features = ['Age', 'Tumor_Size']
categorical_features = ['Gender', 'Tumor_Grade', 'Symptoms_Severity',
'Family_History', 'Smoking_History',
'Alcohol_Consumption', 'Exercise_Frequency']
# Pipeline for numerical features: impute missing values and scale
numerical_pipeline = Pipeline(steps=[
('imputer', SimpleImputer(strategy='mean')),
('scaler', StandardScaler())
])
# Pipeline for categorical features: impute missing values and encode
categorical_pipeline = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('encoder', OneHotEncoder(handle_unknown='ignore'))
])
preprocessor = ColumnTransformer(
transformers=[
('num', numerical_pipeline, numerical_features),
('cat', categorical_pipeline, categorical_features)
]
)
return preprocessor
# --- Load dataset using st.cache_data ---
@st.cache_data
def load_data():
data = pd.read_csv('cancer_prediction_data (2).csv')
return data
data = load_data()
st.title("Cancer Prediction App")
#st.write("This app uses an SVM model to predict the presence of cancer based on patient data.")
#if st.checkbox("Show Data Preview"):
#st.write(data.head())
# Assume the target column is 'Cancer_Present'
target_col = 'Cancer_Present'
if target_col not in data.columns:
st.error(f"Target column '{target_col}' not found in data!")
st.stop()
# Split features and target
X = data.drop(columns=[target_col])
y = data[target_col]
# Create preprocessing pipeline
preprocess = create_preprocessing_pipeline()
# Split into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# --- Create and train the SVM model pipeline ---
svm_pipeline = Pipeline([
('preprocessing', preprocess),
('svm', SVC())
])
svm_pipeline.fit(X_train, y_train)
svm_accuracy = svm_pipeline.score(X_test, y_test)
#st.write(f"**Model Training Complete!** SVM Accuracy on Test Data: {svm_accuracy * 100:.2f}%")
# --- Sidebar for user input ---
st.sidebar.header("Enter Patient Data")
def user_input_features():
age = st.sidebar.number_input("Age", min_value=0, max_value=120, value=50)
tumor_size = st.sidebar.number_input("Tumor Size", min_value=0.0, max_value=100.0, value=5.0)
gender = st.sidebar.selectbox("Gender", options=["Male", "Female"])
tumor_grade = st.sidebar.selectbox("Tumor Grade", options=["Low", "Medium", "High"])
symptoms_severity = st.sidebar.selectbox("Symptoms Severity", options=["Mild", "Moderate", "Severe"])
family_history = st.sidebar.selectbox("Family History", options=["Yes", "No"])
smoking_history = st.sidebar.selectbox("Smoking History", options=["Current Smoker", "Non-Smoker"])
alcohol_consumption = st.sidebar.selectbox("Alcohol Consumption", options=["Low", "Moderate", "High"])
exercise_frequency = st.sidebar.selectbox("Exercise Frequency", options=["Never", "Rarely", "Occasionally", "Often"])
# Create a dictionary of features. Keys must match dataset columns.
data_dict = {
'Age': age,
'Tumor_Size': tumor_size,
'Gender': gender,
'Tumor_Grade': tumor_grade,
'Symptoms_Severity': symptoms_severity,
'Family_History': family_history,
'Smoking_History': smoking_history,
'Alcohol_Consumption': alcohol_consumption,
'Exercise_Frequency': exercise_frequency
}
return pd.DataFrame(data_dict, index=[0])
st.sidebar.markdown("### Patient Data Input")
input_df = user_input_features()
st.subheader("User Input Data")
st.write(input_df)
# --- Make Prediction ---
if st.button("Predict Cancer Presence"):
prediction = svm_pipeline.predict(input_df)
result = "Cancer Detected" if prediction[0] == 1 else "No Cancer Detected"
st.write(f"### Prediction: {result}")
|