Search code examples
pythonpandasnumpycountvectorizer

Python Word Vect numpy array, on saving to csv gave error on dimension , Expected 1D or 2D array, got 0D array instead


Saving wordVect gave error,

from sklearn.feature_extraction.text import CountVectorizer
corpus = [
    'This is the first document.',
    'This document is the second document.',
    'And this is the third one.',
    'Is this the first document?',
]
vectorizer = CountVectorizer(ngram_range=()
X = vectorizer.fit_transform(corpus)

# Save The Vector 
np.savetxt('logvect.csv', X, delimiter=',')

Got Error

----> 1 np.savetxt('logvect.csv', X, delimiter=',')

   1375         if X.ndim == 0 or X.ndim > 2:
   1376             raise ValueError(
-> 1377                 "Expected 1D or 2D array, got %dD array instead" % X.ndim)
   1378         elif X.ndim == 1:
   1379             # Common case -- 1d array of numbers

ValueError: Expected 1D or 2D array, got 0D array instead

Solution

  • X.toarray() # Was missing

    np.savetxt('logvect.csv', X.toarray(), delimiter=',')