1

Rでk-Meansの実装に取り​​組んでいます。

個々のファイルから特徴ベクトルを計算し、それらをすべて、この方法で「ホルダー」と呼ばれるバッグに入れます。

holder[[filename]] <- c(featureVector)

次のように書くと、特定の特徴ベクトルを回復できます。

holder[["file3453.txt"]]またはholder[[number]]

重心やその他の計算に特徴ベクトルを使用するので、特徴ベクトル V があると仮定すると、ホルダーからファイルの名前を取得するにはどうすればよいですか?

この質問は、次のように解釈することもできます。

値(特徴ベクトル)が与えられた場合、キー(ファイル名)をどのように決定できますか?

4

3 に答える 3

2

nograpes のソリューションを拡張する。リバース マップを作成する場合は、次の操作を実行できます。

#this function converts a feature vector
#there may be other, better ways to do that, but this one is simple.
#it works best when your feature vectors are short and contain integers
#it may not work at all due to precision issues for real numbers
my.to.string = function(x) paste(x,collapse="_")  

ホルダーベクターを構築するときは、次のようにします。

holder[[filename]] = featureVector   #updates the holder
reverseMap[[my.to.string(featureVector)]] = filename   # updates the reverse map

今 - あなたの仕事を終わらせるために

my.file = reverseMap[[my.to.string(my.feature)]]

これは簡単で、単純なケースで機能します。まだ見たことがない、または R を必要としていた実際のハッシュコード ベースのデータ構造を置き換えることはできません。

于 2013-10-09T20:28:51.977 に答える
1

リストは R のハッシュテーブルで実装されていないことを知っておく必要があります。また、必要なことを行う効率的な方法はありません。逆引きリストを維持するか、一致するインデックスをスキャンする必要があります。例えば、

# Test data.
holder<-list(`file1`=c(1,0,1,0),`file2`=c(1,1,1,1),`file3`=c(1,0,1,0))
# Find this feature.
feature<-c(1,0,1,0)
# Find all indices that have this feature vector.
names(holder)[sapply(holder,function(x)all(x==feature))]
于 2013-10-09T20:17:22.563 に答える