看板 Python 關於我們 聯絡資訊
from sklearn.feature_extraction.text import CountVectorizer text = [ "我|愛你", "他|愛狗", "貓|愛鼠" ] vectorizer = CountVectorizer() vectorizer.fit(text) vector = vectorizer.transform(text) print (vectorizer.vocabulary_) # 維度 print (vector.shape) print (vector.toarray()) ▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ 輸出: {'愛你': 0, '愛狗': 1, '愛鼠': 2} (3, 3) [[1 0 0] [0 1 0] [0 0 1]] ▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ 想請問版上的各位大大 若是我希望單字(我、他、貓)也要放在bag內 要如何改寫這段程式 或要用什麼關鍵字去google查詢呢 謝謝打家 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.124.158.195 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1529981292.A.316.html ※ 編輯: ctr1 (59.124.158.195), 06/26/2018 10:50:29