-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.R
36 lines (28 loc) · 1.12 KB
/
knn.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# ************************************************
# knnModel() :
#
#
# INPUT : data frame - train - training dataset
# data frame - test - testing dataset
# list - config - list of configurations
#
# OUTPUT :
# Data Frame - measures - performance metrics
# ************************************************
knnModel<-function(train,test,config,plot){
myTitle<-(paste("KNN. K =",config$K_VALUE))
print(myTitle)
positionClassOutput<-which(names(train)==config$OUTPUT_FIELD)
# train data: dataframe with the input fields
train_inputs<-train[-positionClassOutput]
# train data: vector with the expected output
train_expected<-train[,positionClassOutput]
knnClassifier <- caret::knn3(train_inputs, factor(train_expected), k=config$K_VALUE)
# Use the created KNN classifier with the test dataset
measures<-testModel(myModel=knnClassifier,
testDataset=test,
title=myTitle,
config=config,
plot=plot)
return (measures)
}