Added brian.md to 04-ML_KNN

Ah!!!!


On Monday, August 4, 2014 4:33 PM, Aaron Schumacher <notifications@github.com> wrote:
 


In 04-ML_KNN/brian.R:
> @@ -0,0 +1,23 @@
> +assign.score <- function(test.row, labels, training.data, K = 1) {
> +  training.data$score <- apply(training.data[,1:4], 1, function(x) { sqrt((x[1] - test.row[1])^2 + (x[2] - test.row[2])^2 + (x[3] - test.row[3])^2 + (x[4] - test.row[4])^2)})
> +  sorted <- training.data[with(training.data, order(score)), ]
> +  return(sample(sorted[K, as.character(labels)], 1))
> +  # This does not implement the voting functionality.
> +}
> +
> +iris.nn <- function(training_data, labels, test_data, K = 1) {
> +  test_data[labels] <- NA
> +  predicto <- apply(test_data, 1, function(x){ assign.score(x, labels, training_data) })
> +  test_data[labels] <- predicto
> +  return(test_data)
> +}
> +
> +outcome = "Species" 
Ah - now I see that this is passed as labels when you do your function call. This is an alternative design to what the hw called for, and it explains why you're writing out the individual numeric components in your distance calculation. It might simplify the implementation if you passed a vector of labels rather than the column name of the labels.
—
Reply to this email directly or view it on GitHub.