2016-11-27 12 views
3

私の目標は、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

私は言葉や簡単な信頼のためにその相関ルールを疑います文書のトピックを予測するのに理想的です。

つまり、is.subset機能を試してみてください。 .csvファイルなしでサンプルを再現することはできませんが、次のコードは、最高信頼度に基づいてtrans.train[3]の予測トピックを提供します。

# sort rules by conf (you already did that but for the sake of completeness) 
rules<-sort(rules, decreasing=TRUE, by="confidence") 

# find all rules whose lhs matches the training example 
rulesMatch <- is.subset([email protected],trans.train[3]) 

# subset all applicable rules 
applicable <- rules[rulesMatch==TRUE] 

# the first rule has the highest confidence since they are sorted 
prediction <- applicable[1] 
inspect([email protected]) 
+1

これはうまくいきました。あなたの第1のポイントとして、相関ルールを用いた分類が、予測変数間のリンク/依存性を考慮に入れることができるので、より最適化された分類子(決定木など)と比較する方法を検討しています。期待された精度(このアプリケーションの場合)は悪化しましたが、実行時間は非常に短かったです! – Magic8ball

1

今後のリリースでは、RパッケージarulesCBAはこのタイプの機能をサポートしています。

現在の開発バージョンでは、arulesCBAには、ソートされたルールセットを受け入れてCBAクラスファイヤーオブジェクトを返すCBA_rulesetというfunctonがあります。

関連する問題