0

トレーニング セットとテスト セット用に 2 つのスパース マトリックスがあり、それぞれにない列を削除して、両方の列を同じにする必要があります。現時点ではループを使用していますが、より効率的な方法があると確信しています。

# take out features in training set that are not in test
  i<-0
  for(feature in testmatrix@Dimnames[2][[1]]){
    i<-i+1
    if(!(feature %in% trainmatrix@Dimnames[2][[1]])){
      removerows<-c(removerows, i)
    }
  }
  testmatrix<-testmatrix[,-removerows]

# and vice versa...
4

1 に答える 1

2

私には、あなたがやりたいことは、その列testmatrixも表示されるようにしておくことだけのように見えますtrainmatrix。これを両方のマトリックスに適用したいので、簡単な方法は、各マトリックスintersectのベクトルで使用してcolnames交差を見つけcolnames、これを使用してサブセット化することです。

#  keep will be a vector of colnames that appear in BOTH train and test matrices
keep <- intersect( colnames(test) , colnames(train) )

#  Then subset on this vector
testmatrix <- testmatrix[ , keep ]
trainmatrix <- trainmatrix[ , keep ]
于 2013-06-23T10:20:44.690 に答える