enhancements
Browse files
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 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
|
|
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 |
|