iris_s <- as.matrix(scale(iris[1:4]))
class_ids <- as.integer(iris$Species)
num_classes <- max(class_ids)
# create output dataset to be used for training, Here we encode class as -1s and 1s
iris_data_out <- matrix( data = -1, nrow = nrow(iris_s), ncol = num_classes)
# now for each case, assign a 1 to the column corresponding to its class
for(r in 1:nrow(iris_data_out)) iris_data_out[r,class_ids[r]]=1
# Finally apply MAM:
# Encode train pairs in MAM and then get output dataset by recalling the test data.
mam <- new("MAM")
mam$encode(iris_s,iris_data_out)
# test the encoding by recalling the original input data...
mam_data_out <- mam$recall(iris_s)
# find which MAM output has the largest value and use this as the final cluster tag.
mam_recalled_cluster_ids = apply(mam_data_out,1,which.max)
plot(iris_s, pch=mam_recalled_cluster_ids, main="MAM recalled Iris data classes")
cat("MAM recalled these IDs:\n")
print(mam_recalled_cluster_ids)
Run the code above in your browser using DataLab