Spaces:
Sleeping
Sleeping
Update cancer.py
Browse files
cancer.py
CHANGED
|
@@ -10,10 +10,13 @@ from sklearn.linear_model import LogisticRegression
|
|
| 10 |
from sklearn.neighbors import KNeighborsClassifier
|
| 11 |
from sklearn.ensemble import RandomForestClassifier
|
| 12 |
from xgboost import XGBClassifier
|
|
|
|
| 13 |
|
| 14 |
# Load dataset
|
| 15 |
def load_data():
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Data Preprocessing
|
| 19 |
def preprocess_data(df):
|
|
@@ -43,11 +46,11 @@ def preprocess_data(df):
|
|
| 43 |
# Train Model
|
| 44 |
def train_model(x_train, y_train, preprocess, model_name):
|
| 45 |
models = {
|
| 46 |
-
'Decision Tree': DecisionTreeClassifier(),
|
| 47 |
-
'Logistic Regression': LogisticRegression(),
|
| 48 |
'KNN': KNeighborsClassifier(),
|
| 49 |
-
'Random Forest': RandomForestClassifier(),
|
| 50 |
-
'XGBoost': XGBClassifier()
|
| 51 |
}
|
| 52 |
pipeline = Pipeline([
|
| 53 |
('preprocessor', preprocess),
|
|
@@ -66,10 +69,15 @@ with st.sidebar:
|
|
| 66 |
df = load_data()
|
| 67 |
(x_train, x_test, y_train, y_test), preprocess = preprocess_data(df)
|
| 68 |
model = train_model(x_train, y_train, preprocess, model_name)
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
st.session_state['trained_model'] = model
|
| 71 |
st.session_state['x_train'] = x_train
|
| 72 |
st.success(f"Model Trained Successfully! Accuracy: {accuracy:.2f}")
|
|
|
|
| 73 |
|
| 74 |
st.title("🎗️ Cancer Prediction")
|
| 75 |
|
|
@@ -97,20 +105,10 @@ if st.button("Predict Cancer Presence"):
|
|
| 97 |
if 'trained_model' in st.session_state:
|
| 98 |
model = st.session_state['trained_model']
|
| 99 |
x_train = st.session_state['x_train']
|
| 100 |
-
|
| 101 |
-
# Create DataFrame for input
|
| 102 |
input_df = pd.DataFrame(input_data, columns=x_train.columns)
|
| 103 |
-
|
| 104 |
-
# Convert numeric inputs explicitly to float
|
| 105 |
-
for col in ['Age', 'Tumor_Size']:
|
| 106 |
-
input_df[col] = pd.to_numeric(input_df[col], errors='coerce')
|
| 107 |
-
|
| 108 |
-
# Apply preprocessing
|
| 109 |
input_transformed = model.named_steps['preprocessor'].transform(input_df)
|
| 110 |
-
|
| 111 |
-
# Make prediction
|
| 112 |
prediction = model.named_steps['classifier'].predict(input_transformed)
|
| 113 |
-
|
| 114 |
if prediction[0] == 1:
|
| 115 |
st.markdown("<h3 style='color: red;'>Cancer Prediction: Positive 🟥</h3>", unsafe_allow_html=True)
|
| 116 |
st.write("Unfortunately, the model predicts the presence of cancer. Please consult a doctor for further advice.")
|
|
|
|
| 10 |
from sklearn.neighbors import KNeighborsClassifier
|
| 11 |
from sklearn.ensemble import RandomForestClassifier
|
| 12 |
from xgboost import XGBClassifier
|
| 13 |
+
from sklearn.metrics import classification_report
|
| 14 |
|
| 15 |
# Load dataset
|
| 16 |
def load_data():
|
| 17 |
+
df = pd.read_csv('cancer_prediction_data (2).csv')
|
| 18 |
+
st.write("Class distribution:", df['Cancer_Present'].value_counts())
|
| 19 |
+
return df
|
| 20 |
|
| 21 |
# Data Preprocessing
|
| 22 |
def preprocess_data(df):
|
|
|
|
| 46 |
# Train Model
|
| 47 |
def train_model(x_train, y_train, preprocess, model_name):
|
| 48 |
models = {
|
| 49 |
+
'Decision Tree': DecisionTreeClassifier(class_weight='balanced'),
|
| 50 |
+
'Logistic Regression': LogisticRegression(class_weight='balanced'),
|
| 51 |
'KNN': KNeighborsClassifier(),
|
| 52 |
+
'Random Forest': RandomForestClassifier(class_weight='balanced'),
|
| 53 |
+
'XGBoost': XGBClassifier(scale_pos_weight=y_train.value_counts()[0] / y_train.value_counts()[1])
|
| 54 |
}
|
| 55 |
pipeline = Pipeline([
|
| 56 |
('preprocessor', preprocess),
|
|
|
|
| 69 |
df = load_data()
|
| 70 |
(x_train, x_test, y_train, y_test), preprocess = preprocess_data(df)
|
| 71 |
model = train_model(x_train, y_train, preprocess, model_name)
|
| 72 |
+
|
| 73 |
+
# Evaluate the model
|
| 74 |
+
y_pred = model.predict(x_test)
|
| 75 |
+
report = classification_report(y_test, y_pred, output_dict=True)
|
| 76 |
+
accuracy = report['accuracy']
|
| 77 |
st.session_state['trained_model'] = model
|
| 78 |
st.session_state['x_train'] = x_train
|
| 79 |
st.success(f"Model Trained Successfully! Accuracy: {accuracy:.2f}")
|
| 80 |
+
st.write("Classification Report:", report)
|
| 81 |
|
| 82 |
st.title("🎗️ Cancer Prediction")
|
| 83 |
|
|
|
|
| 105 |
if 'trained_model' in st.session_state:
|
| 106 |
model = st.session_state['trained_model']
|
| 107 |
x_train = st.session_state['x_train']
|
|
|
|
|
|
|
| 108 |
input_df = pd.DataFrame(input_data, columns=x_train.columns)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
input_transformed = model.named_steps['preprocessor'].transform(input_df)
|
|
|
|
|
|
|
| 110 |
prediction = model.named_steps['classifier'].predict(input_transformed)
|
| 111 |
+
|
| 112 |
if prediction[0] == 1:
|
| 113 |
st.markdown("<h3 style='color: red;'>Cancer Prediction: Positive 🟥</h3>", unsafe_allow_html=True)
|
| 114 |
st.write("Unfortunately, the model predicts the presence of cancer. Please consult a doctor for further advice.")
|