2017-03-22 6 views
0

私は前処理されたデータを作成しました。今、それをベクトル化してテキストファイルに書きたいと思います。ベクトル化オブジェクトを配列に変換する際に、このエラーが発生します。可能な解決策は何でしょうか?ベクター化オブジェクトtoArray()、配列が大きすぎるエラー

from sklearn.feature_extraction.text import CountVectorizer 
    import numpy as np 
    vectorizer = CountVectorizer(analyzer = "word", \ 
           tokenizer = None, \ 
           preprocessor = None, \ 
           stop_words = None, \ 
           max_features = 1000) 
    newTestFile = open("testfile.txt", 'r', encoding='latin-1') 
    featureVector=vectorizer.fit_transform(newTestFile) 
    train_data_features = featureVector.toarray() 
    np.savetxt('plotFeatureVector.txt', train_data_features, fmt="%10s %10.3f") 

The error: 

    Traceback (most recent call last): 
     File "C:/Users/NuMA/Desktop/Lecture Stuff/EE 485/Project/Deneme/bagOfWords.py", line 12, in <module> 
     train_data_features = featureVector.toarray() 
     File "C:\Users\NuMA\AppData\Local\Programs\Python\Python35-32\lib\site-packages\scipy\sparse\compressed.py", line 964, in toarray 
     return self.tocoo(copy=False).toarray(order=order, out=out) 
     File "C:\Users\NuMA\AppData\Local\Programs\Python\Python35-32\lib\site-packages\scipy\sparse\coo.py", line 252, in toarray 
     B = self._process_toarray_args(order, out) 
     File "C:\Users\NuMA\AppData\Local\Programs\Python\Python35-32\lib\site-packages\scipy\sparse\base.py", line 1039, in _process_toarray_args 
     return np.zeros(self.shape, dtype=self.dtype, order=order) 
    ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size. 
+0

あなたはベクトル化オブジェクトを変換していません。「featureVector」は疎行列です。 –

+0

[ポータブルデータ形式でscipy sparse csr \ _matrixを保存/ロードする]の可能な複製(http://stackoverflow.com/questions/8955448/save-load-scipy-sparse-csr-matrix-in-portable-data-format ) –

+0

特に、dupe-targetの[this](http://stackoverflow.com/a/42101691/5014455)答えで** np.savez/np.load **のアプローチを使用する必要があります。 –

答えて

0

vectorizerが大きい疎行列、featureVectorを作成しました。

featureVector.toarray()(私は通常featureVector.Aを使用しています)は、そこから密な(普通のnumpy)配列を作成することになっています。明らかに必要なサイズが大きすぎます。

repr(featureVector)を印刷できますか?これは、この行列の形状、dtype、および非ゼロ項の数を示すはずです。私はそれが何百万行と数千の列を持っていると思います。

それでもうまくいきましたが、そのような大きな配列のsavetxtfmt="%10s %10.3f" would work. Or thatのcsv`ファイルを使用できるのかどうかは疑問です。

vectorizerが何を生産しているかを必ず確認してください。結果から密な配列を作成して保存するというこの作業を再考してください。

+0

nnz = 12110452、shape =(290988,1000)、dtype = int64、format = csr。私はそれが大きな行列であることを知っていますが、それを配列に書くのは難しいはずはありません。 –

+0

'np.zeros(...)'をそれほど大きくしようとしましたか? – hpaulj

関連する問題