NLP 特徵工程導論

Python 中文本特徵工程

Rounak Banik

Data Scientist

數值資料

Iris 資料集

萼片長度 萼片寬度 花瓣長度 花瓣寬度 類別
6.3 2.9 5.6 1.8 Iris-virginica
4.9 3.0 1.4 0.2 Iris-setosa
5.6 2.9 3.6 1.3 Iris-versicolor
6.0 2.7 5.1 1.6 Iris-versicolor
7.2 3.6 6.1 2.5 Iris-virginica
Python 中文本特徵工程

獨熱編碼(One-hot)

sex
female
male
female
male
female
...
Python 中文本特徵工程

獨熱編碼(One-hot)

sex one-hot encoding
female
male
female
male
female
... ...
Python 中文本特徵工程

獨熱編碼(One-hot)

sex one-hot encoding sex_female sex_male
female 1 0
male 0 1
female 1 0
male 0 1
female 1 0
... ... ... ...
Python 中文本特徵工程

使用 pandas 進行獨熱編碼

# Import the pandas library
import pandas as pd

# Perform one-hot encoding on the 'sex' feature of df df = pd.get_dummies(df, columns=['sex'])
Python 中文本特徵工程

文字資料

電影評論資料集

review class
This movie is for dog lovers. A very poignant... positive
The movie is forgettable. The plot lacked... negative
A truly amazing movie about dogs. A gripping... positive
Python 中文本特徵工程

文字前處理

  • 轉小寫
    • 範例: Reduction 變成 reduction
  • 還原詞形(詞元還原)
    • 範例: reduction 變成 reduce
Python 中文本特徵工程

向量化

review class
This movie is for dog lovers. A very poignant... positive
The movie is forgettable. The plot lacked... negative
A truly amazing movie about dogs. A gripping... positive
Python 中文本特徵工程

向量化

0 1 2 ... n class
0.03 0.71 0.00 ... 0.22 positive
0.45 0.00 0.03 ... 0.19 negative
0.14 0.18 0.00 ... 0.45 positive
Python 中文本特徵工程

基礎特徵

  • 單字數量
  • 字元數量
  • 平均字長
  • 推文

Silverado Records 推文

Python 中文本特徵工程

詞性標註(POS)

Word 詞性(POS)
I 代名詞
have 動詞
a 冠詞
dog 名詞
Python 中文本特徵工程

命名實體辨識

  • 名詞是否指人、組織或國家?

人物、國旗與 TED 標誌

名詞 命名實體(NER)
Brian Person
DataCamp Organization
Python 中文本特徵工程

本章重點

  • 文字前處理
  • 基礎特徵
  • 詞彙特徵
  • 向量化
Python 中文本特徵工程

一起來練習吧!

Python 中文本特徵工程

Preparing Video For Download...