-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKNN_Mar1'18.R
76 lines (49 loc) · 1.32 KB
/
KNN_Mar1'18.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
library()
installed.packages()
library(class)
?knn()
data()
data("iris")
View(iris)
data(iris)
?sample()
range_1_100<-1:100
sample(range_1_100,80)
smpl80<-sort(sample(range_1_100,80))
smpl80
?sort
nrow(iris)*.65
idx<-sort(sample(nrow(iris),as.integer(.65*nrow(iris))))
idx
training<-iris[idx,]
test<-iris[-idx,]
test
training
library(class)
?knn
training2<-training[,-5]
test2<-test[,-5]
outcome<-training[,5]
knn(training[,-5],test[,-5],training[,5])
predict<-knn(training[,-5],test[,-5],training[,5],k=3)
table(Prediction=predict, Actual=test[,5])
mmnorm<-function(x,minx,maxx){z<-((x-minx)/(maxx-minx))
return(z)
}
mmnorm(5,0,10)
iris_normalized <- as.data.frame(
cbind(Sepal.Length=mmnorm(iris[,1],min(iris[,1]),max(iris[,1])),
Sepal.Width=mmnorm(iris[,2],min(iris[,2]),max(iris[,2])),
Petal.Length=mmnorm(iris[,3],min(iris[,3]),max(iris[,3])),
Petal.Width=mmnorm(iris[,4],min(iris[,4]),max(iris[,4])),
Species=iris[,5]
)
)
idx <- sort(sample(nrow(iris_normalized),as.integer(.65*nrow(iris_normalized))))
training <- iris_normalized[idx,]
test <- iris_normalized[-idx,]
View(training[,-5])
View(test[,-5])
?knn()
predict <- knn(training[,-5],test[,-5],training[,5],k=3)
table(Prediction=predict,Actual=test[,5])