简体   繁体   中英

Sci-Kit Learn: getting value from matrix generated by CountVectorizer.fit_transofrm() (PYTHON)

My code is this one:

from sklearn.feature_extraction.text import CountVectorizer

count_vect = CountVectorizer()
new_text = ["with with hello hello hello house"]
X_new_counts = count_vect.fit_transform(new_text)


i = count_vect.vocabulary_.get('hello')
print(X_new_counts.shape)
c = X_new_counts.getcol(0)
print(c)

The matrix generated by X_new_counts = count_vect.fit_transform(new_text) has got this shape: (1, 3)

with i = count_vect.vocabulary_.get('hello') , i get the index in the vocabulary of hello .

My goal is getting the value from this matrix of the index relative count. How i can? If I type:

value = X_new_counts.getcol(i)

it returns:

(0, 0) 3

where "3" is the correct value, but i don't want (0,0). So, how can i get only this value from the matrix?

X_new_counts是一个(稀疏)矩阵,因此您可以使用以下方法在i,j中获取值:

X_new_counts[i, j]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM