2016-03-23 12 views
2

私はpythonを使ってword2vecモデルを計算し、私のコーパスにはgensimを計算しました。NearestNeighborsとword2vecを使用して文の類似性を検出する

次に、各文の平均word2vecベクトルを計算し(文中のすべての単語のすべてのベクトルを平均して)、それをパンダデータフレームに格納しました。パンダのデータフレームdfの 列は以下のとおりです。

  • ブックのタイトル(センテンスが来るブック)
  • 平均ベクトル(文中のword2vecベクトルの平均 - サイズ100)

は、私はおそらく代わりにdoc2vec使用することができます(文の類似性を検出するためのscikit-learnNearestNeighborsを使用しようとしていますが、目的の1つが)doc2vecに対してこの方法を比較することです。

これは私のコードです:

X = df['mean_vector'].values 
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X) 

私は次のエラーを取得する:

ValueError: setting an array element with a sequence. 

私は何とか私はrow == sentence基づいて最も近い隣人を計算できるようにするには、ベクトルを反復処理すべきだと思いますこれは私の現在の(限定された)パイソンスキルを超えているようです。

これは、df['mean_vector'][0]の最初のセルのデータです。これは、文章のベクトルにわたって平均化されたフルベクトルサイズ100である。

for vec in df['mean_vector']: 
X = vec 
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X) 

しかし、私は次の警告を得る:

array([ -2.14208905e-02, 2.42093615e-02, -5.78106642e-02, 
    1.32915592e-02, -2.43393257e-02, -1.41872400e-02, 
    2.83471867e-02, -2.02910602e-02, -5.49359620e-02, 
    -6.70913085e-02, -5.56188896e-02, -2.95186806e-02, 
    4.97652516e-02, 7.16793686e-02, 1.81338750e-02, 
    -1.50108105e-02, 1.79438610e-02, -2.41483524e-02, 
    4.97504435e-02, 2.91026086e-02, -6.87966943e-02, 
    3.27585079e-02, 5.10644279e-02, 1.97029337e-02, 
    7.73109496e-02, 3.23865712e-02, -2.81659551e-02, 
    -9.69715789e-03, 5.23059331e-02, 3.81100960e-02, 
    -3.62489261e-02, -3.40068117e-02, -4.90736961e-02, 
    8.72346922e-04, 2.27111522e-02, 1.06063476e-02, 
    -3.93234752e-02, -1.10617064e-01, 8.05142429e-03, 
    4.56497036e-02, -1.73281748e-02, 2.35153548e-02, 
    5.13465842e-03, 1.88336968e-02, 2.40451116e-02, 
    3.79024050e-03, -4.83284928e-02, 2.10295208e-02, 
    -4.92134318e-03, 1.01532964e-02, 8.02216958e-03, 
    -6.74675079e-03, -1.39653292e-02, -2.07276996e-02, 
    9.73508134e-03, -7.37899616e-02, -2.58320477e-02, 
    -1.10700730e-05, -4.53227758e-02, 2.31859135e-03, 
    1.40053956e-02, 1.61973312e-02, 3.01702786e-02, 
    -6.96818605e-02, -3.47468331e-02, 4.79541793e-02, 
    -1.78820305e-02, 5.99209731e-03, -5.92620336e-02, 
    7.34678581e-02, -5.23381204e-05, -5.07357903e-02, 
    -2.55154949e-02, 5.06089740e-02, -3.70467864e-02, 
    -2.04878468e-02, -7.62404222e-03, -5.38200373e-03, 
    7.68705690e-03, -3.27000804e-02, -2.18365286e-02, 
    2.34392099e-03, -3.02998684e-02, 9.42565035e-03, 
    3.24523374e-02, -1.10793915e-02, 3.06244520e-03, 
    -1.82240941e-02, -5.70741761e-03, 3.13486941e-02, 
    -1.15621388e-02, 1.10221673e-02, -3.55655849e-02, 
    -4.56304513e-02, 5.54837054e-03, 4.38252240e-02, 
    1.57828294e-02, 2.65670624e-02, 8.08797963e-03, 
    4.55569401e-02], dtype=float32) 

は私もやることを試みた例が同様にword2vecを使用してgithubのとNearestNeighborsに存在する場合

DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. 

をシナリオ私はそれを見たいと思う。

+1

どのようにXが見えますか?サンプルXで質問を更新できますか? –

+0

MWEのデータが必要です。私は 'NearestNeighbors'を' gensim'のデータを使ってテストしました。 – ncfirth

+0

@Abhishek Thakur私は例ベクトルを追加しました(コーパスの各文に1つありますので、かなりの数です)。 – Enzo

答えて

2

sklearnは2次元入力を想定しており、それぞれの例が新しい行にあるため、編集がエラーを投げる理由があります。 X.reshape(1, -1)または[X]のいずれかを使用できます。最初の方が良い方法です。生データや適切なMWEがなければ、正確に間違っているとは言い難いですが、私の推測によれば、データをデータフレームの内外に入れて何かが間違っているということです。 X.shapeがあなたに合っていることを確認してください。以下は

は、私はすべてをチェックするために使用された例は、私の仕事です:

from sklearn.neighbors import NearestNeighbors 
from gensim.models import Word2Vec 
import numpy as np 

a = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore 
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea 
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est 
laborum.""" 
a = [x.split(' ') for x in a.split('\n') if len(x)] 
model = Word2Vec(a, min_count=1) 

# Get the average of all of the words to get data for a sentence 
b = np.array([np.mean([model[xx] for xx in x], axis=0) for x in a]) 
# Check it's the correct shape 
print b.shape 

nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(b) 
+0

私の問題は、私が平均ベクトルを格納するためにパンダのデータフレームを使用していると思います。 'print(df ['mean_vector']。fit(X): ''(67680、) ''(67680、) ''が返されますが、X = df ['mean_vector' 'まだ配列に配列を設定しています。 ' – Enzo

+0

データフレームに67680の文がいくつありますか?もしそうなら、 'X'のサイズが'(67680,100) 'を返すと期待します。 – ncfirth

+0

あなたの例を見ると(非常に便利です!)、問題は私のデータフレームの形になっていると思っています。それを修正する方法がわからない... – Enzo

関連する問題