Created
December 6, 2016 08:10
-
-
Save alpo-p/cac6a4c4cba3e4a376a406827c47c4ee to your computer and use it in GitHub Desktop.
R: K nearest neighbours test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| setwd("~/R/harjottelut/prostate cancer") | |
| library(class) | |
| library(gmodels) | |
| prc <- read.csv("Prostate_Cancer.csv", stringsAsFactors = FALSE) | |
| #str(prc) | |
| prc <- prc[-1] | |
| #View(prc) | |
| #table(prc$diagnosis_result) | |
| prc$diagnosis_result <- factor(prc$diagnosis_result, levels = c("B", "M"), labels = c("Benign", "Malignant")) | |
| #round(prop.table(table(prc$diagnosis)) * 100, digits = 1) | |
| normalize <- function(x) { | |
| return ((x - min(x)) / (max(x) - min(x))) | |
| } | |
| prc_n <- as.data.frame(lapply(prc[2:9], normalize)) | |
| #View(prc_n) | |
| #summary(prc_n$radius) | |
| prc_train <- prc_n[1:65,] | |
| prc_test <- prc_n[66:100,] | |
| prc_train_labels <- prc[1:65, 1] | |
| prc_test_labels <- prc[66:100, 1] | |
| prc_test_pred <- knn(train = prc_train, test = prc_test, cl = prc_train_labels, k=12) #decided to go with k=12 | |
| CrossTable(x = prc_test_labels, y = prc_test_pred, prop.chisq = FALSE) #shows the results. not perfect, but decent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment