knn.pred = knn(train.X, test.X, train.colors5 ,k=5) table(knn.pred , train.colors5) ## train.colors5 ## knn.pred blue cyan green orange purple ## blue 348 360 115 205 270 ## cyan 362 397 114 183 307 ## green 118 119 45 50 115 ## orange 184 182 71 121 161 ## purple 305 334 98 153 283 mean(knn.pred != train.colors5) ## [1] 0.7612 standardized.X = (df5[ , 1:2]) var(df5[ , 1]) ## [1] 33.34 var(df5[ , 2]) ## [1] 856.5 var(standardized.X[ , 1]) ## [1] 1 var( standardized.X[ , 2]) ## [1] 1 train.X = standardized.X[train, ] test.X= standardized.X[-train, ] train.Y = df5[train, 3] test.Y = df5[-train, 3] set.seed (2014) knn.pred = knn(train.X, test.X, train.Y, k = 1) mean(test.Y != knn.pred) # 0.0156 This is much better! ## [1] 0.0156 set.seed (2014) knn.pred = knn(train.X, test.X, train.Y, k = 3) mean(test.Y != knn.pred) ## [1] 0.0162
knn.pred = knn(train.X, test.X, train.colors5 ,k=5) table(knn.pred , train.colors5) ## train.colors5 ## knn.pred blue cyan green orange purple ## blue 348 360 115 205 270 ## cyan 362 397 114 183 307 ## green 118 119 45 50 115 ## orange 184 182 71 121 161 ## purple 305 334 98 153 283 mean(knn.pred != train.colors5) ## [1] 0.7612 standardized.X = (df5[ , 1:2]) var(df5[ , 1]) ## [1] 33.34 var(df5[ , 2]) ## [1] 856.5 var(standardized.X[ , 1]) ## [1] 1 var( standardized.X[ , 2]) ## [1] 1 train.X = standardized.X[train, ] test.X= standardized.X[-train, ] train.Y = df5[train, 3] test.Y = df5[-train, 3] set.seed (2014) knn.pred = knn(train.X, test.X, train.Y, k = 1) mean(test.Y != knn.pred) # 0.0156 This is much better! ## [1] 0.0156 set.seed (2014) knn.pred = knn(train.X, test.X, train.Y, k = 3) mean(test.Y != knn.pred) ## [1] 0.0162