Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #loading data set
- data = iris
- #details of data set
- summary(data)
- #display top 5 rows
- head(data)
- #writing a function to normalise a column
- normalise = function(x){
- return ((x - min(x))/(max(x)-min(x)))
- }
- #yanking columns from the data set and normalizing them
- data$Sepal.Length = normalise(data$Sepal.Length)
- data$Sepal.Width = normalise(data$Sepal.Width)
- data$Petal.Length = normalise(data$Petal.Length)
- data$Petal.Width = normalise(data$Petal.Width)
- #randomizing data to make a data set for training
- ind = sample(1:nrow(data), size = 0.9*nrow(data), replace = FALSE )
- training_data = data[ind,]
- #creating testing data (500 IQ move)
- test_data = data[-ind,]
- #creating label for later verification (5th col of table)
- test_data_label = test_data[,5]
- #removing 5th column from test_data
- test_data = test_data[-5]
- #creating training data label
- training_data_label = training_data[,5]
- training_data = training_data[,-5]
- #load package
- library(class)
- library(caret)
- library(ggplot2)
- #executing(implementing?) KNN algorithm
- model = knn(training_data, test_data, training_data_label, k=11)
- #comparing the model and the test data labels
- model
- test_data_label
- #evaluating the performance of the model
- confusionMatrix(model, test_data_label)
- #there is another
- #resetting training data
- training_data = data[ind,]
- #another model
- model = train(Species ~., data = training_data, method = 'knn')
- #another prediction
- prediction = predict(model, test_data)
- #evaluating the performance of the new model
- confusionMatrix(prediction, test_data_label)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement