Skip to content

Instantly share code, notes, and snippets.

@iwata-n
Created March 26, 2016 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwata-n/b2111405229b52b75320 to your computer and use it in GitHub Desktop.
Save iwata-n/b2111405229b52b75320 to your computer and use it in GitHub Desktop.
世界のビールと音楽の組み合わせをデータ分析で最適化!で作ったソース
# イベントページ
# http://connpass.com/event/27829/
# パッケージのインストール
# install.packages("arules")
# install.packages("arulesViz")
library(arules)
library(arulesViz)
data <- read.csv("music.csv", fileEncoding = "UTF-8")
# head(data) # コンソールで表示
# View(data) # エクセルっぽく表で表示
# data[1, 1] # 特定の要素だけ表示
# data[1,] # 行を表示
# data[,2] # 列を表示
# データフレーム型だとアソシエーション分析に入れれないので変える
data <- data[, -1] # 参加者情報は不要なので除く
data[, 1] <- as.factor(data[, 1]) # 型の変換(factor型へ変換)
data[, 2] <- as.factor(data[, 2])
data <- as(data, "transactions")
# par(family = "Osaka") # Macの場合フォントファミリーを指定する
itemFrequencyPlot(data, type = "relative")
# アソシエーション分析する
rules <- apriori(data, parameter = list(support = 0.01, confidence = 0.01, minlen = 2))
# rules <- apriori(data, parameter = list(support = 0.01, confidence = 0.01))
# しきい値以上でフィルター
rules <- subset(rules, subset = (lift > 1.0))
# サポート血が大きい順に並べ替え
rules <- sort(rules, by = "support")
# コンソールで表示
inspect(head(rules, n = 10))
# csvで書き出す
write.csv(as(rules, "data.frame"), file = "結果.csv", row.names = FALSE, fileEncoding = "UTF-8")
# 関係図で表示
plot(
rules,
method = "graph",
control = list(
type = "items",
cex = 1.0
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment