Question: Hyperparameter Tuning section of the code not working import pandas as pd import seaborn as sns import matplotlib.pyplot as plt # Import numpy and give
Hyperparameter Tuning section of the code not working
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Import numpy and give it the alias np
import numpy as np
# Load dataset
data pdreadcsvcontentdriveMyDriveCancerData.csv
# Display basic information
printdatainfo
printdatadescribe
# Visualize class distribution
snscountplotx'diagnosis', datadata
plttitleDistribution of Malignant and Benign Tumors'
pltshow
# Handle missing values
# Exclude nonnumeric columns from mean calculation
numericdata data.selectdtypesincludenpnumber
# Replace infinite values with NaN
numericdata.replacenpinf, npinf npnan, inplaceTrue
# Calculate mean without infinite values
# Check if there are any columns with all values as NaN after replacing inf
for col in numericdata.columns:
if numericdatacolisnullall:
# Handle columns with all NaN values here, we drop the column
numericdata.dropcol axis inplaceTrue
data.dropcol axis inplaceTrue
else:
datacol numericdatacolfillnanumericdatacolmean
# Encode categorical variables
datadiagnosis datadiagnosismapM: B: # M malignant, B benign
#Normalize numerical features to ensure they contribute equally to distance calculations in SVC and Random Forest.
from sklearn.preprocessing import StandardScaler
scaler StandardScaler
features data.dropdiagnosis axis
#Splitting the Data
from sklearn.modelselection import traintestsplit
Xtrain, Xtest, ytrain, ytest traintestsplitfeatures datadiagnosis testsize randomstate
#Fit and transform the scaler on the training data only
Xtrainscaled scaler.fittransformXtrain
#Transform the test data using the scaler fit on the training data
Xtestscaled scaler.transformXtest
#Hyperparameter Tuning
from sklearn.modelselection import GridSearchCV
# Define parameter grids
paramgridsvc C: 'kernel': linearrbf
paramgridrf nestimators': 'maxdepth': None
# Create GridSearchCV objects
gridsvc GridSearchCVSVC paramgridsvc cv
gridrf GridSearchCVRandomForestClassifier paramgridrf cv
# Fit models with grid search
gridsvcfitXtrain, ytrain
gridrffitXtrain, ytrain
#Model Evaluation
#Import the SVC class
from sklearn.svm import SVC
#Create an SVC model
svcmodel SVC
#Train the model
svcmodel.fitXtrainscaled, ytrain
# Import the RandomForestClassifier class
from sklearn.ensemble import RandomForestClassifier
# Create a RandomForestClassifier model
rfmodel RandomForestClassifierrandomstate # Add a random state for reproducibility
# Train the model
rfmodel.fitXtrainscaled, ytrain
from sklearn.metrics import accuracyscore, classificationreport
# Predictions from each model
svcpred svcmodel.predictXtestscaled # Predict on the scaled test data
rfpred rfmodel.predictXtestscaled # Predict on the scaled test data
# Evaluate models
printSVC Classification Report:
classificationreportytest, svcpred
printRandom Forest Classification Report:
classificationreportytest, rfpred
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
