Question: To perform k - NN classification and answer the questions, follow these steps in RStudio: # Load required libraries library ( caret ) library (
To perform kNN classification and answer the questions, follow these steps in RStudio:
# Load required libraries
librarycaret
librarymlbench
# Load the UniversalBank dataset
dataUniversalBank
# Split the data into training and holdout sets training, holdout
set.seed
index createDataPartitionUniversalBank$PersonalLoan, p list FALSE
traindata UniversalBankindex
holdoutdata UniversalBankindex,
# Define categorical predictors as factors
categoricalcols cFamily "Education", "SecuritiesAccount", "CDAccount", "Online", "CreditCard"
traindatacategoricalcols lapplytraindatacategoricalcols asfactor
holdoutdatacategoricalcols lapplyholdoutdatacategoricalcols asfactor
# Define the new customer's data
newcustomer data.frame
Age
Experience
Income
Family
CCAvg
Education
Mortgage
SecuritiesAccount
CDAccount
Online
CreditCard
# Perform kNN classification with k
knnmodel train
PersonalLoan ~
data traindata,
method knn
preProcess ccenter "scale"
tuneGrid expand.gridk
trControl trainControlmethod cv number
# Classify the new customer using the best k
predictedclass predictknnmodel, newcustomer
predictedprob predictknnmodel, newcustomer, type "prob"
# Print the predicted class and probability
printpredictedclass
printpredictedprob
# Confusion matrix for holdout data using the best k
bestk knnmodel$bestTune$k
predictedholdout predictknnmodel, holdoutdata
confusionmatrix confusionMatrixpredictedholdout, holdoutdata$PersonalLoan
printconfusionmatrix
Explanation:
The task involves using kNN classification to predict whether customers will accept a personal loan offer based on demographic and banking information. Here are the steps:
Load the UniversalBank dataset and split it into training and holdout sets.
Define categorical predictors as factors for kNN
Define a new customer's data.
Perform kNN classification with k on the training data.
Classify the new customer using the best k obtained from fold crossvalidation.
Display the predicted class and probability for the new customer.
Calculate the confusion matrix for the holdout data using the best k
This analysis helps determine if the new customer is likely to accept a personal loan offer based on their attributes and previous campaign
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
