louiecerv commited on
Commit
098099d
·
1 Parent(s): 6a575cc

enhancements

Browse files
Files changed (1) hide show
  1. app.py +45 -44
app.py CHANGED
@@ -275,48 +275,49 @@ hba1c_level = st.number_input("HbA1c Level", min_value=0.0, value=6.0)
275
  blood_glucose_level = st.number_input("Blood Glucose Level", min_value=0, value=100)
276
 
277
  if st.button("Predict Diabetes"):
278
- # Create a DataFrame for the user input
279
- input_data = pd.DataFrame({
280
- 'gender': [gender],
281
- 'age': [age],
282
- 'hypertension': [int(hypertension)], # Convert categorical numerical features to int
283
- 'heart_disease': [int(heart_disease)],
284
- 'smoking_history': [smoking_history],
285
- 'bmi': [bmi],
286
- 'HbA1c_level': [hba1c_level],
287
- 'blood_glucose_level': [blood_glucose_level]
288
- })
289
-
290
- # Ensure encoding is applied correctly
291
- encoded_input = encoder.transform(input_data[['gender', 'smoking_history']])
292
- encoded_input_df = pd.DataFrame(encoded_input.toarray(), columns=encoder.get_feature_names_out())
293
-
294
- # Drop the original categorical columns and concatenate the encoded features
295
- input_data = input_data.drop(['gender', 'smoking_history'], axis=1)
296
- input_data = pd.concat([input_data, encoded_input_df], axis=1)
297
-
298
- # Ensure that the input data has the same columns as training data
299
- missing_cols = set(X_train.columns) - set(input_data.columns) # Corrected line
300
- for col in missing_cols:
301
- input_data[col] = 0 # Add missing columns with zero values
302
-
303
- # Reorder columns to match training data
304
- input_data = input_data.reindex(columns=X_train.columns, fill_value=0)
305
-
306
- # Convert all column names to strings
307
- input_data.columns = input_data.columns.astype(str)
308
-
309
- # Scale the user input (convert back to DataFrame after transformation)
310
- input_data_scaled = scaler.transform(input_data)
311
- input_data_scaled = pd.DataFrame(input_data_scaled, columns=input_data.columns) # Convert back to DataFrame
312
-
313
- # Make prediction using the selected model
314
- prediction = selected_model.predict(input_data_scaled)
315
-
316
- # Display the prediction
317
- st.write("Prediction:")
318
- if prediction[0] == 0:
319
- st.info("The model predicts that you do not have diabetes.")
320
- else:
321
- st.warning("The model predicts that you have diabetes.")
 
322
 
 
275
  blood_glucose_level = st.number_input("Blood Glucose Level", min_value=0, value=100)
276
 
277
  if st.button("Predict Diabetes"):
278
+ with st.spinner("Prrocessing inputs..."):
279
+ # Create a DataFrame for the user input
280
+ input_data = pd.DataFrame({
281
+ 'gender': [gender],
282
+ 'age': [age],
283
+ 'hypertension': [int(hypertension)], # Convert categorical numerical features to int
284
+ 'heart_disease': [int(heart_disease)],
285
+ 'smoking_history': [smoking_history],
286
+ 'bmi': [bmi],
287
+ 'HbA1c_level': [hba1c_level],
288
+ 'blood_glucose_level': [blood_glucose_level]
289
+ })
290
+
291
+ # Ensure encoding is applied correctly
292
+ encoded_input = encoder.transform(input_data[['gender', 'smoking_history']])
293
+ encoded_input_df = pd.DataFrame(encoded_input.toarray(), columns=encoder.get_feature_names_out())
294
+
295
+ # Drop the original categorical columns and concatenate the encoded features
296
+ input_data = input_data.drop(['gender', 'smoking_history'], axis=1)
297
+ input_data = pd.concat([input_data, encoded_input_df], axis=1)
298
+
299
+ # Ensure that the input data has the same columns as training data
300
+ missing_cols = set(X_train.columns) - set(input_data.columns) # ✅ Corrected line
301
+ for col in missing_cols:
302
+ input_data[col] = 0 # Add missing columns with zero values
303
+
304
+ # Reorder columns to match training data
305
+ input_data = input_data.reindex(columns=X_train.columns, fill_value=0)
306
+
307
+ # Convert all column names to strings
308
+ input_data.columns = input_data.columns.astype(str)
309
+
310
+ # Scale the user input (convert back to DataFrame after transformation)
311
+ input_data_scaled = scaler.transform(input_data)
312
+ input_data_scaled = pd.DataFrame(input_data_scaled, columns=input_data.columns) # Convert back to DataFrame
313
+
314
+ # Make prediction using the selected model
315
+ prediction = selected_model.predict(input_data_scaled)
316
+
317
+ # Display the prediction
318
+ st.write("Prediction:")
319
+ if prediction[0] == 0:
320
+ st.info("The model predicts that you do not have diabetes.")
321
+ else:
322
+ st.warning("The model predicts that you have diabetes.")
323