行列Nがあり、N行の行列tf.m
とデータフレームdf
があります。
同じ行のn
のデータフレームの列に、行のn
行を渡したいと思います。行列の行を単一のデータフレーム列に割り当てます。
#id text tf
#1 doc1 hello world happy hello people world
# 0.0000000 0.7924813 0.0000000 0.7924813
#2 doc2 people people happy hello people world
# 0.0000000 0.0000000 0.5849625 0.0000000
#2 doc3 happy people happy hello people world
# 0.7924813 0.0000000 0.2924813 0.0000000
に基づいて、KNNを養成しようとする:私はこれを実行する場合
library("tm")
ftfidf <- function(text.d) {
txt <- VectorSource(text.d);
txt.corpus <- VCorpus(txt, readerControl = list(reader = readPlain, language = "en"));
revs <- tm_map(txt.corpus, content_transformer(tolower))
dtm <- DocumentTermMatrix(revs, control = list(weighting = function(x) weightTfIdf(x, normalize = T),stopwords = TRUE))
}
df<-data.frame(id=c("doc1", "doc2", "doc3"), text=c("hello world", "people people", "happy people"))
#id text
#1 doc1 hello world
#2 doc2 people people
#3 doc3 happy people
tf <- ftfidf(df$text) # a function that gets a DocumentTermMatrix
tf.m <- as.matrix(tf)
#Terms
#Docs happy hello people world
#1 0.0000000 0.7924813 0.0000000 0.7924813
#2 0.0000000 0.0000000 0.5849625 0.0000000
#3 0.7924813 0.0000000 0.2924813 0.0000000
、私は
df$tf<-tf.m
#id text tf.happy tf.hello tf.people tf.world
#1 doc1 hello world 0.0000000 0.7924813 0.0000000 0.7924813
#2 doc2 people people 0.0000000 0.0000000 0.5849625 0.0000000
#3 doc3 happy people 0.7924813 0.0000000 0.2924813 0.0000000
が、私はこれを持っているしたいデータフレームに4つの以上の列を取得します長期周波数df$tf
(可能であれば)
knn_model <- knn(train = df$tf[1,], cl = df$id, k=3)
を使用して、df$id
の最近傍を照会します。
私の目標は、Rでこの「LIKE」Pythonのgraphlab機能を実行することです:あなたは階層的なインデックスを持つようにしたいよう
knn_model = graphlab.nearest_neighbors.create(df,features=['tf'],label='id')
に存在する場合、自分自身が見つけるために熱心に質問を、理解ことを願っています。 – akrun
私はあなたが欲しいものを正確には分かりません。新しいデータフレームに6行がある場合は、単語とtfidfスコアが交互に表示されますか? –