TShopping

 找回密碼
 註冊
搜索
查看: 809|回復: 0
打印 上一主題 下一主題

[教學] python 自然語言中的 CountVectorizer

[複製鏈接]
跳轉到指定樓層
1#
發表於 2021-5-4 22:07:12 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
• 預設會轉換為小寫。
• 預設會進行排序,由小而大排列。
• 拆解後的特徵名稱有以下幾個。
• vectorizer.get_feature_names( )
• 接著我們要找出現每一筆資料內每個特徵各
自出現多少次。print(X.toarray())


代碼
  1. from sklearn.feature_extraction.text import  CountVectorizer
  2. corpus = [
  3.     'This is the first document.',
  4.     'this document is the second document.',
  5.     'And this is the third one.',
  6.     'Is this the first document?',
  7. ]
  8. vectorizer=CountVectorizer()
  9. X=vectorizer.fit_transform(corpus)
  10. print(vectorizer.get_feature_names())
  11. print(X)
  12. print()
  13. print(X.toarray())
複製代碼




• idxs=np.array(sorted(score_dict.items(),key=lambda x:x[1],reverse=True))[:return_num,0]
• score_dict他是一個dict結構,內容為相似度的計算結果,我們就依照大小進行排序
• 接著轉換為numpy的陣列,就可以用數值化的概念,找出你想找的N筆資料
• ndarray[:return_num,0] 代表區塊取值,前面是rows,後面是colums的範圍,我們預設return_num為3,也就是找01 2 這三筆資料




 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-26 23:09 , Processed in 0.049126 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表