Spaces:
Sleeping
Sleeping
Update cancer.py
Browse files
cancer.py
CHANGED
|
@@ -23,18 +23,20 @@ def preprocess_data(df):
|
|
| 23 |
|
| 24 |
preprocess = ColumnTransformer([
|
| 25 |
('num', Pipeline([
|
| 26 |
-
('imputer', SimpleImputer(strategy='mean')),
|
| 27 |
-
('scaler', StandardScaler())
|
| 28 |
]), numeric),
|
|
|
|
| 29 |
('ord', Pipeline([
|
| 30 |
-
('imputer', SimpleImputer(strategy='most_frequent')),
|
| 31 |
-
('encoder', OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=-1))
|
| 32 |
]), ordinal),
|
|
|
|
| 33 |
('nom', Pipeline([
|
| 34 |
-
('imputer', SimpleImputer(strategy='most_frequent')),
|
| 35 |
-
('encoder', OneHotEncoder(sparse_output=False, handle_unknown='ignore'))
|
| 36 |
]), nominal)
|
| 37 |
-
], remainder='passthrough')
|
| 38 |
|
| 39 |
X = df.drop('Cancer_Present', axis=1)
|
| 40 |
y = df['Cancer_Present']
|
|
@@ -72,7 +74,7 @@ with st.sidebar:
|
|
| 72 |
st.session_state['X_train'] = X_train
|
| 73 |
st.success(f"Model Trained Successfully! Accuracy: {accuracy:.2f}")
|
| 74 |
|
| 75 |
-
st.title("🎗️ Cancer Prediction")
|
| 76 |
|
| 77 |
st.markdown("""<style>.big-font {font-size:20px !important;}</style>
|
| 78 |
<p class="big-font">Provide patient details below to predict cancer presence:</p>""", unsafe_allow_html=True)
|
|
@@ -88,7 +90,7 @@ with col2:
|
|
| 88 |
smoking_history = st.selectbox("Smoking History", ['Never Smoker', 'Former Smoker', 'Current Smoker'])
|
| 89 |
alcohol_consumption = st.selectbox("Alcohol Consumption", ['Low','Moderate','High'])
|
| 90 |
exercise_frequency = st.selectbox("Exercise Frequency", ['Rarely', 'Occasionally', 'Regularly','Never'])
|
| 91 |
-
gender = st.selectbox("Gender", [
|
| 92 |
family_history = st.selectbox("Family History", ["No", "Yes"])
|
| 93 |
|
| 94 |
input_data = [[age, tumor_size, tumor_grade, symptoms_severity, smoking_history,
|
|
@@ -98,8 +100,6 @@ if st.button("Predict Cancer Presence"):
|
|
| 98 |
if 'trained_model' in st.session_state:
|
| 99 |
model = st.session_state['trained_model']
|
| 100 |
X_train = st.session_state['X_train']
|
| 101 |
-
|
| 102 |
-
# Reorder input_data columns to match X_train columns
|
| 103 |
input_df = pd.DataFrame(input_data, columns=X_train.columns)
|
| 104 |
input_transformed = model.named_steps['preprocessor'].transform(input_df)
|
| 105 |
prediction = model.named_steps['classifier'].predict(input_transformed)
|
|
@@ -112,5 +112,3 @@ if st.button("Predict Cancer Presence"):
|
|
| 112 |
st.write("Good news! The model predicts that there is no cancer detected. Stay healthy!")
|
| 113 |
else:
|
| 114 |
st.error("Please train a model first!")
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 23 |
|
| 24 |
preprocess = ColumnTransformer([
|
| 25 |
('num', Pipeline([
|
| 26 |
+
('imputer', SimpleImputer(strategy='mean')), # Impute numeric columns with the mean
|
| 27 |
+
('scaler', StandardScaler()) # Standardize numeric columns
|
| 28 |
]), numeric),
|
| 29 |
+
|
| 30 |
('ord', Pipeline([
|
| 31 |
+
('imputer', SimpleImputer(strategy='most_frequent')), # Impute ordinal columns with the most frequent value
|
| 32 |
+
('encoder', OrdinalEncoder(handle_unknown="use_encoded_value", unknown_value=-1)) # Encode ordinal columns
|
| 33 |
]), ordinal),
|
| 34 |
+
|
| 35 |
('nom', Pipeline([
|
| 36 |
+
('imputer', SimpleImputer(strategy='most_frequent')), # Impute nominal columns with the most frequent value
|
| 37 |
+
('encoder', OneHotEncoder(sparse_output=False, handle_unknown='ignore')) # One hot encode nominal columns
|
| 38 |
]), nominal)
|
| 39 |
+
], remainder='passthrough') # Keep other columns unchanged
|
| 40 |
|
| 41 |
X = df.drop('Cancer_Present', axis=1)
|
| 42 |
y = df['Cancer_Present']
|
|
|
|
| 74 |
st.session_state['X_train'] = X_train
|
| 75 |
st.success(f"Model Trained Successfully! Accuracy: {accuracy:.2f}")
|
| 76 |
|
| 77 |
+
st.title("🎗️ Cancer Prediction Using Machine Learning 🎗️")
|
| 78 |
|
| 79 |
st.markdown("""<style>.big-font {font-size:20px !important;}</style>
|
| 80 |
<p class="big-font">Provide patient details below to predict cancer presence:</p>""", unsafe_allow_html=True)
|
|
|
|
| 90 |
smoking_history = st.selectbox("Smoking History", ['Never Smoker', 'Former Smoker', 'Current Smoker'])
|
| 91 |
alcohol_consumption = st.selectbox("Alcohol Consumption", ['Low','Moderate','High'])
|
| 92 |
exercise_frequency = st.selectbox("Exercise Frequency", ['Rarely', 'Occasionally', 'Regularly','Never'])
|
| 93 |
+
gender = st.selectbox("Gender", [0, 1])
|
| 94 |
family_history = st.selectbox("Family History", ["No", "Yes"])
|
| 95 |
|
| 96 |
input_data = [[age, tumor_size, tumor_grade, symptoms_severity, smoking_history,
|
|
|
|
| 100 |
if 'trained_model' in st.session_state:
|
| 101 |
model = st.session_state['trained_model']
|
| 102 |
X_train = st.session_state['X_train']
|
|
|
|
|
|
|
| 103 |
input_df = pd.DataFrame(input_data, columns=X_train.columns)
|
| 104 |
input_transformed = model.named_steps['preprocessor'].transform(input_df)
|
| 105 |
prediction = model.named_steps['classifier'].predict(input_transformed)
|
|
|
|
| 112 |
st.write("Good news! The model predicts that there is no cancer detected. Stay healthy!")
|
| 113 |
else:
|
| 114 |
st.error("Please train a model first!")
|
|
|
|
|
|