나는 내 컴퓨터 단어의 점수에 tfidf
점수를 생성했으며 어떤 단어인지 식별하고 싶습니다. 내가 발견하고 싶은 무엇tfidf 색인 및 점수를 기반으로 가장 대표적인 단어를 찾는 방법
from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer(stop_words = 'english')
X_counts = count_vect.fit_transform(X)
X_counts.shape
Out[4]: (26, 3777)
from sklearn.feature_extraction.text import TfidfTransformer
tfidf_transformer = TfidfTransformer()
X_tfidf = tfidf_transformer.fit_transform(X_counts)
X_tfidf.shape
Out[73]: (26, 3777)
print(X_tfidf)
(0, 3378) 0.0349567750954
(0, 3018) 0.0349567750954
(0, 3317) 0.0349567750954
(0, 2873) 0.0349567750954
(0, 1678) 0.0310225609857
(0, 2005) 0.0282311916523
(0, 1554) 0.0349567750954
(0, 1855) 0.0349567750954
(0, 709) 0.0260660373875
(0, 3101) 0.0282311916523
(0, 2889) 0.0699135501907
(0, 3483) 0.0193404539445
(0, 3388) 0.0349567750954
(0, 2418) 0.0349567750954
(0, 2962) 0.0310225609857
(0, 1465) 0.0349567750954
(0, 406) 0.0310225609857
(0, 3063) 0.0349567750954
(0, 1070) 0.0260660373875
(0, 1890) 0.0349567750954
(0, 163) 0.0349567750954
(0, 820) 0.0310225609857
(0, 1705) 0.0349567750954
(0, 1985) 0.0215056082093
(0, 760) 0.0349567750954
: :
(25, 711) 0.102364672113
(25, 1512) 0.102364672113
(25, 1674) 0.0701273701419
(25, 2863) 0.102364672113
(25, 765) 0.112486016266
(25, 756) 0.0945139476693
(25, 3537) 0.283541843008
(25, 949) 0.0945139476693
(25, 850) 0.0826760487146
(25, 1289) 0.0945139476693
(25, 3475) 0.127425722423
(25, 186) 0.0738342053646
(25, 3485) 0.0738342053646
(25, 532) 0.0945139476693
(25, 2293) 0.088099438739
(25, 164) 0.0494476278373
(25, 3003) 0.0475454135311
(25, 2994) 0.200322389399
(25, 2993) 0.133548259599
(25, 3559) 0.369171026823
(25, 1474) 0.0738342053646
(25, 3728) 0.102364672113
(25, 923) 0.0826760487146
(25, 1291) 0.0701273701419
(25, 2285) 0.233934283758
이 기사 당 가장 유익한 단어, 기사 당 최고 열 단어 이는이 내 코드와 결과입니다.
(0, 760) 0.0349567750954
(25, 3559) 0.369171026823
(25, 2285) 0.233934283758
편집 :
I tested the code but I get following error. I also tested it on the X_tfidf vectors and its the same error.
top_n = 10
for i in range(len(X_counts)):
print X_tfidf.getrow(i).todense().A1.argsort()[top_n:][::-1]
Traceback (most recent call last):
File "<ipython-input-13-2a181d63441b>", line 2, in <module>
for i in range(len(X_counts)):
File "/home/fledrmaus/anaconda2/lib/python2.7/site-packages/scipy/sparse/base.py", line 199, in __len__
raise TypeError("sparse matrix length is ambiguous; use getnnz()"
TypeError: sparse matrix length is ambiguous; use getnnz() or shape[0]
EDITED II : 예를 들어, 다음과 같은 점수를 첫 번째와 마지막 문서의 단어 어떤 있습니다
좋아, 내가이 일을 변경 그리고 지금 작동합니다. 그러나 벡터가 생성되지만 가장 높은 점수를 가진 단어는 생성되지 않습니다.
top_n = 10
for i in range(26):
print tfidf.getrow(i).todense().A1.argsort()[top_n:][::-1]
[ 681 2501 3693 ..., 2451 2450 2449]
[ 552 1532 1566 ..., 2452 2451 2450]
[2285 3602 742 ..., 2455 2466 2465]
[1266 1074 1662 ..., 2481 2493 2491]
[ 397 2545 2815 ..., 2418 2417 2416]
[3559 1746 482 ..., 2456 2455 2454]
[ 562 2104 1854 ..., 2466 2477 2476]
[1158 3668 983 ..., 2470 2482 2481]
[2070 704 3418 ..., 2452 2451 2450]
[3350 515 376 ..., 2487 2500 2499]
[2266 734 735 ..., 2461 2474 2472]
[ 756 1499 60 ..., 2479 2490 2489]
[3559 3537 550 ..., 2509 2508 2507]
[3559 2882 1720 ..., 2455 2466 2465]
[3404 3199 1617 ..., 2477 2488 2487]
[1415 63 65 ..., 2474 2485 2484]
[2373 3017 441 ..., 2499 2498 2497]
[ 733 2994 516 ..., 2508 2507 2506]
[3615 2200 2387 ..., 2511 2510 2509]
[3559 2558 1289 ..., 2455 2466 2465]
[ 239 1685 2993 ..., 2485 2496 2495]
[1897 2227 357 ..., 2503 2502 2501]
[ 491 1512 3008 ..., 2506 2505 2504]
[2994 675 3125 ..., 2480 2491 2490]
[ 612 1466 2926 ..., 2424 2423 2422]
[2059 3329 3051 ..., 2479 2490 2489]
EDITED III
마지막 줄이 오류를 제공합니다 :
Traceback (most recent call last):
File "<ipython-input-12-813e5387f3b7>", line 9, in <module>
print X_counts.get_feature_names()[wordindexes]
File "/home/fledrmaus/anaconda2/lib/python2.7/site-packages/scipy/sparse/base.py", line 525, in __getattr__
raise AttributeError(attr + " not found")
AttributeError: get_feature_names not found
나는 TfidfVectorizer이 방법을 시도하고 오늘 아침에 같은 오류가 발생했습니다.
EDITED IV
print(X_counts)
(0, 2175) 2
(0, 481) 1
(0, 2511) 1
(0, 1167) 1
(0, 3711) 9
(0, 2501) 10
(0, 3298) 1
(0, 2263) 1
(0, 2313) 1
(0, 2939) 1
(0, 1382) 8
(0, 2040) 3
(0, 3542) 1
(0, 715) 1
(0, 2374) 1
(0, 2375) 1
(0, 1643) 3
(0, 1303) 2
(0, 3599) 8
(0, 708) 6
(0, 709) 1
(0, 1128) 1
(0, 559) 1
(0, 1901) 1
(0, 2310) 1
: :
(25, 2755) 1
(25, 1380) 1
(25, 680) 1
(25, 1079) 1
(25, 890) 1
(25, 658) 1
(25, 1363) 1
(25, 337) 1
(25, 3661) 1
(25, 1035) 1
(25, 2952) 1
(25, 94) 1
(25, 1906) 1
(25, 2133) 1
(25, 374) 1
(25, 2099) 1
(25, 2736) 1
(25, 2089) 1
(25, 3163) 1
(25, 3680) 1
(25, 3040) 1
(25, 3157) 1
(25, 1080) 1
(25, 555) 1
(25, 2016) 1
는 다시 코드를 테스트하고 나는 다시 벡터를 얻을 수 있지만 단어 :
[ 681 2501 3693 3694 1382 3711 2141 3599 3598 1741]
[ 552 1532 1566 690 1898 3503 2730 2993 1189 1420]
[2285 3602 742 3708 3264 3668 1511 2211 3579 1291]
[1266 1074 1662 2827 3524 3069 3070 3218 1365 805]
[ 397 2545 2815 1962 213 432 2241 653 426 2117]
EDITED V는 :
그것은 또 다른 오류가 발생하지 :
이없는 그들 중 다섯 이상, 하나 개의 벡터/기사 작동처럼 91,363,210[ 681 2501 3693 3711 1382 3694 3599 2141 3598 1741]
[1532 552 1566 690 1898 3503 2730 2993 1189 1420]
[2285 3602 742 3708 3264 3668 2211 1511 1292 3579]
[1266 1074 1662 2827 3070 3524 3069 3218 1365 805]
[ 397 2545 2815 1962 213 432 2241 653 426 2117]
print count_vect.get_feature_names()[wordindexes]
Traceback (most recent call last):
File "<ipython-input-16-95b994e8246b>", line 1, in <module>
print count_vect.get_feature_names()[wordindexes]
TypeError: only integer arrays with one element can be converted to an index
EDITED VI
는 것 같습니다.
wordfeatures = count_vect.get_feature_names()
for i in wordindexes:
print wordfeatures[i]
chemical
phosphorus
weapon
white
falluja
weapons
used
marines
use
illegal
X_ 카운트가 무엇인지 공유 할 수 있습니까? –
최신 코드를 실행 한 후 마지막으로 수정하십시오. –