回答編集履歴
1
追記
answer
CHANGED
@@ -1,59 +1,81 @@
|
|
1
|
-
原因は、`print(count)`同様の出力結果を文字列としてそのまま書き込んでいるからです。
|
2
|
-
|
3
|
-
今一つやりたいことが分かりませんが、疎行列のまま、行列位置と値を`CSV`形式で出力したい場合は
|
4
|
-
[scipy.io.mmwrite](https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.mmwrite.html)にて[Matrix Market](http://math.nist.gov/MatrixMarket/formats.html)形式で出力したテキストを中間ファイルとして利用すれば可能です。
|
5
|
-
|
6
|
-
参考:[Dump a sparse matrix into a file](https://stackoverflow.com/questions/6087520/dump-a-sparse-matrix-into-a-file)
|
7
|
-
|
8
|
-
```Python
|
9
|
-
from sklearn.feature_extraction.text import CountVectorizer
|
10
|
-
|
11
|
-
# Scikit learnで行う文章の特徴ベクトルの抽出
|
12
|
-
# http://nonbiri-tereka.hatenablog.com/entry/2015/06/04/070933
|
13
|
-
cv = CountVectorizer()
|
14
|
-
data = [
|
15
|
-
'This is a pen.',
|
16
|
-
'That is a bot.',
|
17
|
-
'These are red document and blue document.',
|
18
|
-
]
|
19
|
-
count = cv.fit_transform(data)
|
20
|
-
print(count)
|
21
|
-
|
22
|
-
# Matrix Marketファイル(.mtx)を中間ファイルとして利用
|
23
|
-
from scipy import io
|
24
|
-
io.mmwrite( 'count', count)
|
25
|
-
with open( 'count.mtx', 'r') as t:
|
26
|
-
lines = t.read().split('\n')
|
27
|
-
lines = lines[3:] # 不要な先頭3行を飛ばす
|
28
|
-
print(lines)
|
29
|
-
import os
|
30
|
-
#os.remove('count.mtx') # 不要なので削除
|
31
|
-
|
32
|
-
import csv
|
33
|
-
with open('count.csv', 'wt',newline='') as f:
|
34
|
-
writer = csv.writer(f)
|
35
|
-
for l in lines:
|
36
|
-
# 1 7 1
|
37
|
-
dat = l.split()
|
38
|
-
if len(dat) == 3:
|
39
|
-
# 行列位置を0オリジンに
|
40
|
-
dat[0] = int(dat[0]) - 1
|
41
|
-
dat[1] = int(dat[1]) - 1
|
42
|
-
writer.writerow(dat)
|
43
|
-
```
|
44
|
-
|
45
|
-
出力結果 : count.csv
|
46
|
-
```
|
47
|
-
0,6,1
|
48
|
-
0,5,1
|
49
|
-
0,10,1
|
50
|
-
1,3,1
|
51
|
-
1,8,1
|
52
|
-
1,5,1
|
53
|
-
2,2,1
|
54
|
-
2,0,1
|
55
|
-
2,4,2
|
56
|
-
2,7,1
|
57
|
-
2,1,1
|
58
|
-
2,9,1
|
1
|
+
原因は、`print(count)`同様の出力結果を文字列としてそのまま書き込んでいるからです。
|
2
|
+
|
3
|
+
今一つやりたいことが分かりませんが、疎行列のまま、行列位置と値を`CSV`形式で出力したい場合は
|
4
|
+
[scipy.io.mmwrite](https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.mmwrite.html)にて[Matrix Market](http://math.nist.gov/MatrixMarket/formats.html)形式で出力したテキストを中間ファイルとして利用すれば可能です。
|
5
|
+
|
6
|
+
参考:[Dump a sparse matrix into a file](https://stackoverflow.com/questions/6087520/dump-a-sparse-matrix-into-a-file)
|
7
|
+
|
8
|
+
```Python
|
9
|
+
from sklearn.feature_extraction.text import CountVectorizer
|
10
|
+
|
11
|
+
# Scikit learnで行う文章の特徴ベクトルの抽出
|
12
|
+
# http://nonbiri-tereka.hatenablog.com/entry/2015/06/04/070933
|
13
|
+
cv = CountVectorizer()
|
14
|
+
data = [
|
15
|
+
'This is a pen.',
|
16
|
+
'That is a bot.',
|
17
|
+
'These are red document and blue document.',
|
18
|
+
]
|
19
|
+
count = cv.fit_transform(data)
|
20
|
+
print(count)
|
21
|
+
|
22
|
+
# Matrix Marketファイル(.mtx)を中間ファイルとして利用
|
23
|
+
from scipy import io
|
24
|
+
io.mmwrite( 'count', count)
|
25
|
+
with open( 'count.mtx', 'r') as t:
|
26
|
+
lines = t.read().split('\n')
|
27
|
+
lines = lines[3:] # 不要な先頭3行を飛ばす
|
28
|
+
print(lines)
|
29
|
+
import os
|
30
|
+
#os.remove('count.mtx') # 不要なので削除
|
31
|
+
|
32
|
+
import csv
|
33
|
+
with open('count.csv', 'wt',newline='') as f:
|
34
|
+
writer = csv.writer(f)
|
35
|
+
for l in lines:
|
36
|
+
# 1 7 1
|
37
|
+
dat = l.split()
|
38
|
+
if len(dat) == 3:
|
39
|
+
# 行列位置を0オリジンに
|
40
|
+
dat[0] = int(dat[0]) - 1
|
41
|
+
dat[1] = int(dat[1]) - 1
|
42
|
+
writer.writerow(dat)
|
43
|
+
```
|
44
|
+
|
45
|
+
出力結果 : count.csv
|
46
|
+
```
|
47
|
+
0,6,1
|
48
|
+
0,5,1
|
49
|
+
0,10,1
|
50
|
+
1,3,1
|
51
|
+
1,8,1
|
52
|
+
1,5,1
|
53
|
+
2,2,1
|
54
|
+
2,0,1
|
55
|
+
2,4,2
|
56
|
+
2,7,1
|
57
|
+
2,1,1
|
58
|
+
2,9,1
|
59
|
+
```
|
60
|
+
|
61
|
+
あるいは自力で出力することもできます。
|
62
|
+
```Python
|
63
|
+
# 略
|
64
|
+
|
65
|
+
def to_lines(m):
|
66
|
+
lines = []
|
67
|
+
ip = m.indptr
|
68
|
+
for x in range(len(ip)-1):
|
69
|
+
s,e = ip[x], ip[x+1]
|
70
|
+
if s != e:
|
71
|
+
for y, v in zip( m.indices[s:e], m.data[s:e]):
|
72
|
+
lines.append((x,y,v))
|
73
|
+
return lines
|
74
|
+
|
75
|
+
lines = to_lines(count)
|
76
|
+
lines.sort() # 分かりやすいように
|
77
|
+
|
78
|
+
with open('ret.csv', 'w') as f:
|
79
|
+
for l in lines:
|
80
|
+
f.write(f'{l[0]},{l[1]},{l[2]}\n')
|
59
81
|
```
|