varshitha22 commited on
Commit
f72f0ab
·
verified ·
1 Parent(s): 4518331

Update cancer.py

Browse files
Files changed (1) hide show
  1. cancer.py +8 -6
cancer.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
  import pandas as pd
3
  from sklearn.pipeline import Pipeline
4
- from sklearn.preprocessing import StandardScaler, OrdinalEncoder, OneHotEncoder
5
  from sklearn.impute import SimpleImputer
6
  from sklearn.compose import ColumnTransformer
7
  from sklearn.model_selection import train_test_split
@@ -31,7 +31,7 @@ def preprocess_data(df):
31
  ]), categorical_features)
32
  ], remainder='passthrough')
33
 
34
- x= df.drop('Cancer_Present', axis=1)
35
  y = df['Cancer_Present']
36
  return train_test_split(x, y, test_size=0.2, random_state=23), preprocess
37
 
@@ -70,9 +70,9 @@ with st.sidebar:
70
  # Train model
71
  try:
72
  model = train_model(x_train, y_train, preprocess, model_name)
73
- accuracy = model.score(x_test, y_test)
74
  st.session_state['trained_model'] = model
75
- st.session_state['x_train'] = x_train
76
  st.success(f"Model Trained Successfully! Accuracy: {accuracy:.2f}")
77
  except ValueError as e:
78
  st.error(f"Error: {e}")
@@ -104,10 +104,12 @@ input_data = [[age, tumor_size, tumor_grade, symptoms_severity, smoking_history,
104
  if st.button("Predict Cancer Presence"):
105
  if 'trained_model' in st.session_state:
106
  model = st.session_state['trained_model']
107
- x_train = st.session_state['x_train']
108
 
109
  # Prepare input data for prediction
110
- input_df = pd.DataFrame(input_data, columns=x_train.columns)
 
 
111
  input_transformed = model.named_steps['preprocessor'].transform(input_df)
112
 
113
  # Make prediction
 
1
  import streamlit as st
2
  import pandas as pd
3
  from sklearn.pipeline import Pipeline
4
+ from sklearn.preprocessing import StandardScaler, OneHotEncoder
5
  from sklearn.impute import SimpleImputer
6
  from sklearn.compose import ColumnTransformer
7
  from sklearn.model_selection import train_test_split
 
31
  ]), categorical_features)
32
  ], remainder='passthrough')
33
 
34
+ x = df.drop('Cancer_Present', axis=1)
35
  y = df['Cancer_Present']
36
  return train_test_split(x, y, test_size=0.2, random_state=23), preprocess
37
 
 
70
  # Train model
71
  try:
72
  model = train_model(x_train, y_train, preprocess, model_name)
73
+ accuracy = model.score(X_test, y_test)
74
  st.session_state['trained_model'] = model
75
+ st.session_state['x_train_columns'] = x_train.columns # Save column names for future prediction
76
  st.success(f"Model Trained Successfully! Accuracy: {accuracy:.2f}")
77
  except ValueError as e:
78
  st.error(f"Error: {e}")
 
104
  if st.button("Predict Cancer Presence"):
105
  if 'trained_model' in st.session_state:
106
  model = st.session_state['trained_model']
107
+ X_train_columns = st.session_state['x_train_columns'] # Get saved column names
108
 
109
  # Prepare input data for prediction
110
+ input_df = pd.DataFrame(input_data, columns=x_train_columns)
111
+
112
+ # Align input data with the model's expected columns
113
  input_transformed = model.named_steps['preprocessor'].transform(input_df)
114
 
115
  # Make prediction