SHOW:
|
|
- or go back to the newest paste.
1 | #loading data set | |
2 | data = iris | |
3 | #details of data set | |
4 | summary(data) | |
5 | #display top 5 rows | |
6 | head(data) | |
7 | ||
8 | #writing a function to normalise a column | |
9 | normalise = function(x){ | |
10 | return ((x - min(x))/(max(x)-min(x))) | |
11 | } | |
12 | ||
13 | #yanking columns from the data set and normalizing them | |
14 | data$Sepal.Length = normalise(data$Sepal.Length) | |
15 | data$Sepal.Width = normalise(data$Sepal.Width) | |
16 | data$Petal.Length = normalise(data$Petal.Length) | |
17 | data$Petal.Width = normalise(data$Petal.Width) | |
18 | ||
19 | #randomizing data to make a data set for training | |
20 | ind = sample(1:nrow(data), size = 0.9*nrow(data), replace = FALSE ) | |
21 | training_data = data[ind,] | |
22 | ||
23 | #creating testing data (500 IQ move) | |
24 | test_data = data[-ind,] | |
25 | ||
26 | #creating label for later verification (5th col of table) | |
27 | test_data_label = test_data[,5] | |
28 | ||
29 | #removing 5th column from test_data | |
30 | test_data = test_data[-5] | |
31 | ||
32 | #creating training data label | |
33 | training_data_label = training_data[,5] | |
34 | training_data = training_data[,-5] | |
35 | ||
36 | #load package | |
37 | library(class) | |
38 | library(caret) | |
39 | library(ggplot2) | |
40 | ||
41 | #executing(implementing?) KNN algorithm | |
42 | model = knn(training_data, test_data, training_data_label, k=11) | |
43 | ||
44 | #comparing the model and the test data labels | |
45 | model | |
46 | test_data_label | |
47 | ||
48 | #evaluating the performance of the model | |
49 | confusionMatrix(model, test_data_label) | |
50 | ||
51 | #there is another | |
52 | ||
53 | #resetting training data | |
54 | training_data = data[ind,] | |
55 | ||
56 | #another model | |
57 | model = train(Species ~., data = training_data, method = 'knn') | |
58 | ||
59 | #another prediction | |
60 | prediction = predict(model, test_data) | |
61 | ||
62 | #evaluating the performance of the new model | |
63 | confusionMatrix(prediction, test_data_label) | |
64 |