私の目標は、Rパッケージarules
によって生成されたルールを使用して、各トランザクションのtopic
を予測することです(各トランザクションは1つのトピックを持ちます)。資料。私はトレーニングセットtrans.train
(ルールの作成に使用)とテストセットtrans.test
(私は "トピック"を予測したい)を持っています。私はまた、これらの予測(ルールの右側が正しいトピックである割合)をテストできるようにしたいと考えています。R内の小包から新しいトランザクションに生成されたルールを適用する
各ルールの右側がトピック(topic = earnなど)で、左側がドキュメント内の他の単語であることを保証することができます。だから私のすべてのルールは、形式があります:私はルールをソートし、最も高い信頼を持つルールは右側を予測するようにすることをtrans.test
にそれらを適用したいき
{word1,...,wordN} -> {topic=topic1}
を、私はどのように把握することはできませんドキュメンテーションに基づいてこれを行う。
これを実装する方法はありますか。私はarulesCBA
パッケージを見ましたが、より複雑なアルゴリズムを実装していますが、私はtopic
の予測子として最も信頼度の高いルールを使いたいだけです。
トランザクションを生成するコード:
library(arules)
#load data into R
filename = "C:/Users/sterl_000/Desktop/lab2file.csv"
data = read.csv(filename,header=TRUE,sep="\t")
#Get the number of columns in the matrix
col = dim(data)[2]
#Turn into logical matrix
data[,2:col]=(data[,2:col]>0)
#define % of training and test set
train_pct = 0.8
bound <- floor((nrow(data)*train_pct))
#randomly permute rows
data <- data[sample(nrow(data)), ]
#get training data
data.train <- data[1:bound, ]
#get test data
data.test <- data[(bound+1):nrow(data),]
#Turn into transaction format
trans.train = as(data.train,"transactions")
trans.test = as(data.test,"transactions")
#Create list of unique topics in 'topic=earn' format
#Allows us to specify only the topic label as the right hand side
uni_topics = paste0('topic=',unique(data[,1]))
#Get assocation rules
rules = apriori(trans.train,
parameter=list(support = 0.02,target= "rules", confidence = 0.5),
appearance = list(rhs = uni_topics,default='lhs'))
#Sort association rules by confidence
rules = sort(rules,by="confidence")
#Predict the right hand side, topic= in trans.train based on the sorted rules
例トランザクション:
> inspect(trans.train[3])
items transactionID
[1] {topic=coffee,
current,
meet,
group,
statement,
quota,
organ,
brazil,
import,
around,
five,
intern,
produc,
coffe,
institut,
reduc,
intent,
consid} 8760
ルール例:
> inspect(rules[1])
lhs rhs support confidence lift
[1] {qtli} => {topic=earn} 0.03761135 1 2.871171
これはうまくいきました。あなたの第1のポイントとして、相関ルールを用いた分類が、予測変数間のリンク/依存性を考慮に入れることができるので、より最適化された分類子(決定木など)と比較する方法を検討しています。期待された精度(このアプリケーションの場合)は悪化しましたが、実行時間は非常に短かったです! – Magic8ball