Question: # Load the model model = joblib.load ( ' random _ forest _ model.pkl ' ) import pandas as pd import numpy as np def

# Load the model
model = joblib.load('random_forest_model.pkl')
import pandas as pd
import numpy as np
def predict_heart_disease(input_data):
# Convert input data to DataFrame
input_df = pd.DataFrame([input_data], columns=X.columns)
# Preprocess the data
preprocessed_data = preprocessor.transform(input_df)
# Predict using the model
prediction = model.predict(preprocessed_data)
# Return the prediction
return "Heart Disease" if prediction[0]==1 else "No Heart Disease"
import random
def predict_heart_disease(input_data):
# Randomly generate a prediction
return random.choice(["Heart Disease", "No Heart Disease"])
def main():
print("Heart Disease Prediction")
# Collect user inputs
try:
age = int(input("Enter age: "))
sex = int(input("Enter sex (1= male, 0= female): "))
cp = int(input("Enter chest pain type (1-4): "))
trestbps = int(input("Enter resting blood pressure (in mm Hg): "))
chol = int(input("Enter serum cholesterol (in mg/dl): "))
fbs = int(input("Enter fasting blood sugar (1= true, 0= false): "))
restecg = int(input("Enter resting electrocardiographic results (0-2): "))
thalch = int(input("Enter maximum heart rate achieved: "))
exang = int(input("Enter exercise induced angina (1= yes, 0= no): "))
oldpeak = float(input("Enter depression induced by exercise relative to rest: "))
slope = int(input("Enter slope of the peak exercise ST segment (1-3): "))
ca = int(input("Enter number of major vessels colored by fluoroscopy (0-3): "))
thal = int(input("Enter thalassemia (1= normal, 2= fixed defect, 3= reversable defect): "))
# Prepare input data
input_data ={
'age': age,
'sex': sex,
'cp': cp,
'trestbps': trestbps,
'chol': chol,
'fbs': fbs,
'restecg': restecg,
'thalch': thalch,
'exang': exang,
'oldpeak': oldpeak,
'slope': slope,
'ca': ca,
'thal': thal
}
# Predict and display result
result = predict_heart_disease(input_data)
print(f"Prediction: {result}")
except ValueError:
# Handle incorrect input without printing errors
print("Invalid input. Please enter numeric values where required.")
if __name__=="__main__":
main()
Explain the code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!