Question: # # Preprocessing Pipeline # Define preprocessing for numerical and categorical features numeric _ features = [ ' age ' , 'trestbps', 'chol', 'thalch', 'oldpeak'
# # Preprocessing Pipeline
# Define preprocessing for numerical and categorical features
numericfeatures age 'trestbps', 'chol', 'thalch', 'oldpeak'
categoricalfeatures sexcpfbs 'restecg', 'slope', ca 'thal'
# Numerical pipeline
numericpipeline Pipeline
imputer SimpleImputerstrategy'median'
scaler StandardScaler
# Categorical pipeline
categoricalpipeline Pipeline
imputer SimpleImputerstrategy'mostfrequent'
onehot OneHotEncoderdrop'first'
# Combine pipelines into a full preprocessing pipeline
preprocessor ColumnTransformer
num numericpipeline, numericfeatures
cat categoricalpipeline, categoricalfeatures
# # Split the Data
# Split data into training and test sets
Xtrain, Xtest, ytrain, ytest traintestsplitX y testsize randomstate
# Create and train model
logreg Pipeline
preprocessor preprocessor
classifier LogisticRegressionmaxiter
logreg.fitXtrain, ytrain
ypred logreg.predictXtest
# Evaluation
printLogistic Regression"
printAccuracy: accuracyscoreytest, ypred
printClassification Report:
classificationreportytest, ypred
printConfusion Matrix:
confusionmatrixytest, ypred
# Create and train model
decisiontree Pipeline
preprocessor preprocessor
classifier DecisionTreeClassifier
decisiontree.fitXtrain, ytrain
ypred decisiontree.predictXtest
# Evaluation
printDecision Tree"
printAccuracy: accuracyscoreytest, ypred
printClassification Report:
classificationreportytest, ypred
printConfusion Matrix:
confusionmatrixytest, ypred
# # Random Forest
#
# In:
# Create and train model
randomforest Pipeline
preprocessor preprocessor
classifier RandomForestClassifier
randomforest.fitXtrain, ytrain
ypred randomforest.predictXtest
# Evaluation
printRandom Forest"
printAccuracy: accuracyscoreytest, ypred
printClassification Report:
classificationreportytest, ypred
printConfusion Matrix:
confusionmatrixytest, ypred
# # Support Vector Machine SVM
# In:
# Create and train model
svm Pipeline
preprocessor preprocessor
classifier SVCprobabilityTrue
svmfitXtrain, ytrain
ypred svmpredictXtest
# Evaluation
printSupport Vector Machine"
printAccuracy: accuracyscoreytest, ypred
printClassification Report:
classificationreportytest, ypred
printConfusion Matrix:
confusionmatrixytest, ypred
# # KNearest Neighbors KNN
#
# In:
# Create and train model
knn Pipeline
preprocessor preprocessor
classifier KNeighborsClassifier
knnfitXtrain, ytrain
ypred knnpredictXtest
# Evaluation
printKNearest Neighbors"
printAccuracy: accuracyscoreytest, ypred
printClassification Report:
classificationreportytest, ypred
printConfusion Matrix:
confusionmatrixytest, ypred
# # Hyperparameter Tuning for Each Model
# # Logistic Regression
#
# In:
# Define hyperparameters for Logistic Regression
paramgridlogreg
'classifierC:
'classifiersolver': liblinear 'saga'
# Set up GridSearchCV
gridlogreg GridSearchCVPipeline
preprocessor preprocessor
classifier LogisticRegressionmaxiter
paramgridlogreg, cv scoring'accuracy'
# Fit GridSearchCV
gridlogreg.fitXtrain, ytrain
# Best parameters and score
printBest Parameters for Logistic Regression:", gridlogreg.bestparams
printBest Score for Logistic Regression:", gridlogreg.bestscore
# Evaluate on the test set
ypred gridlogreg.predictXtest
printLogistic Regression Test Accuracy:", accuracyscoreytest, ypred
printClassification Report:
classificationreportytest, ypred
printConfusion Matrix:
confusionmatrixytest, ypred
# # Decision Tree
# In:
# Define hyperparameters for Decision Tree
paramgriddectree
'classifiermaxdepth': None
'classifierminsamplessplit':
'classifiercriterion': gini 'entropy'
# Set up GridSearchCV
griddectree GridSearchCVPipeline
preprocessor preprocessor
classifier DecisionTreeClassifier
paramgriddectree, cv scoring'accuracy'
# Fit GridSearchCV
griddectree.fitXtrain, ytrain
# Best parameters and score
printBest Parameters for Decision Tree:", griddectree.bestparams
printBest Score for Decision Tree:", griddectree.bestscore
Explain this code
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
