CountVectorizerで単語の出現数をカウントしたデータをCSVファイルに出力したところ、
途中のデータが省略されてしまいます。(printも同様)
全データを出力するにはどうすれば良いでしょうか?
```python
from sklearn.feature_extraction.text import CountVectorizer
import csv
csvc_vec = CountVectorizer()
count = c_vec.fit_transform(data)
with open('count.csv', 'wt') as f:
writer = csv.writer(f)
writer.writerows(count)
以下、count.csvファイルをエディタ等で開いた表示 " (0, 0) 29 (0, 1) 22 省略 (0, 31) 6 (0, 32) 6 : : (0, 28745) 11 (0, 28749) 3 省略 (0, 28791) 1 (0, 28793) 26" 実際のdetaは、test5.pyのサンプルにように単語がブランクで区切られた文字列の配列です。 countの型とサイズは以下の通りです。 print(type(count)): <class 'scipy.sparse.csr.csr_matrix'> print(count.shape):(1, 28799) <test5.py> ```python from sklearn.feature_extraction.text import CountVectorizer import csv data = ['Twinkle twinkle little star' 'How I wonder what you are' 'Up above the world so high' 'Like a diamond in the sky' 'Twinkle twinkle little star' 'How I wonder what you are'] csvc_vec = CountVectorizer() count = csvc_vec.fit_transform(data) with open('count.csv', 'wt') as f: writer = csv.writer(f) writer.writerows(count) print(count) print(type(count)) print(count.shape)
test5.py
(0, 1) 1
(0, 7) 1
(0, 5) 1
(0, 3) 1
(0, 4) 1
(0, 8) 1
(0, 14) 1
(0, 10) 2
(0, 0) 1
(0, 2) 1
(0, 15) 2
(0, 12) 2
(0, 13) 2
(0, 9) 2
(0, 6) 2
(0, 11) 3
<class 'scipy.sparse.csr.csr_matrix'>
(1, 16)
回答1件
あなたの回答
tips
プレビュー