To implement knn algorithm for wine dataset using R.
y – dependent variable
p – Percentage of data for training
method – resampling method
number – Either the number of folds or number of resampling iterations repeats
repeats – For repeated k-fold cross-validation only: the number of complete sets of folds to compute.
Data – data frame
model – fitted model
data – New data
#Loading Required packages
library(“caret”)
#Import Data
my_input<-read.csv(“wine.csv”)
View(my_input)
str(my_input)
summary(my_input)
#Data preparation
sum(is.na(my_input))
#Splitting into train and tset data set
training<-createDataPartition( my_input$class,p=0.7,list=F)
train<-my_input[training,]
test<-my_input[-training,]
dim(train)
dim(test)
#Converting the class variables as factor in train data set
train$class<-as.factor(train$class)
test$class<-as.factor(test$class)
str(my_input)
#Training the knn model
tr<-trainControl(method = “repeatedcv”,number = 10,repeats = 3)
knn_model<-train(class~., data=train,method=”knn”, trControl=tr,
preProcess=c(“center”,”scale”),
tuneLength=10)
knn_model
#Plotting knn Model
plot(knn_model)
#Prediction
predict_knn<-predict(knn_model, test)
predict_knn
#Confusion Matrix
levels(train$class)
levels(test$class)
confusion_knn<-confusionMatrix(predict_knn,test$class)
confusion_knn