文本在實(shí)際問題中很常見,比如用戶評(píng)論、新聞?wù)?、視頻彈幕等等。我們用的toy_data不包含文本變量,所以這里我參考了scikit-learn的文檔,一個(gè)小的corpus作為我們的訓(xùn)練數(shù)據(jù)集。
corpus = [
'This is the first document.',
'This is the second second document.',
'And the third> 'Is this the first document?',
]
corpus有四句話,可以看做是四個(gè)樣本。接下來我們先用一個(gè)簡單的方法處理文本變量——統(tǒng)計(jì)corpus中每個(gè)詞出現(xiàn)次數(shù),代碼如下: