Created
September 22, 2015 06:01
-
-
Save MarkEdmondson1234/86ae2c685e60e400dba4 to your computer and use it in GitHub Desktop.
For Machine Learning and Web Analytics blogpost http://markedmondson.me/intro-to-machine-learning-with-web-analytics-random-forests-and-k-means
This file contains 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
## function to get plot data format | |
getCompareTable <- function (test_data, prediction) { | |
require(dplyr) | |
## plot real vs model bought Sku | |
actual_freq <- table(model_data$boughtSku) | |
predicted_freq <- table(prediction) | |
actual_freq <- actual_freq[order(actual_freq)] | |
predicted_freq <- predicted_freq[order(predicted_freq)] | |
actual_freq_s <- data.frame(sku = names(actual_freq), | |
actual = as.vector(actual_freq), | |
stringsAsFactors = F) | |
predicted_freq_s <- data.frame(sku = names(predicted_freq), | |
predict = as.vector(predicted_freq), | |
stringsAsFactors = F) | |
actual_freq_s$actual <- unname(actual_freq_s$actual) | |
predicted_freq_s$predict <- unname(predicted_freq_s$predict) | |
compare <- dplyr::left_join(actual_freq_s, predicted_freq_s, by = "sku") | |
compare | |
} | |
## use function to get plot data | |
compare <- getCompareTable(test, prediction) | |
## plot the predicted vs actual in test set | |
library(ggplot2) | |
library(reshape2) | |
compare_long <- melt(compare) | |
g <- ggplot(data = compare_long, aes(x=sku, y = value, colour = variable, group = variable)) + theme_bw() | |
g <- g + geom_bar(stat = "identity", position = "dodge", aes(fill=variable)) | |
g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment