NLP 特征工程导论

Python 中的 NLP 特征工程

Rounak Banik

Data Scientist

数值数据

鸢尾花数据集

sepal length sepal width petal length petal width class
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 中的 NLP 特征工程

独热编码(One-hot encoding)

sex
female
male
female
male
female
...
Python 中的 NLP 特征工程

独热编码(One-hot encoding)

sex one-hot encoding
female
male
female
male
female
... ...
Python 中的 NLP 特征工程

独热编码(One-hot encoding)

sex one-hot encoding sex_female sex_male
female 1 0
male 0 1
female 1 0
male 0 1
female 1 0
... ... ... ...
Python 中的 NLP 特征工程

用 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 中的 NLP 特征工程

文本数据

电影评论数据集

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 中的 NLP 特征工程

文本预处理

  • 转为小写
    • 示例:Reductionreduction
  • 还原为词元基本形式
    • 示例:reductionreduce
Python 中的 NLP 特征工程

向量化

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 中的 NLP 特征工程

向量化

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 中的 NLP 特征工程

基础特征

  • 词数
  • 字符数
  • 平均词长
  • 推文

Silverado Records 的推文

Python 中的 NLP 特征工程

词性标注(POS)

Word POS
I Pronoun
have Verb
a Article
dog Noun
Python 中的 NLP 特征工程

命名实体识别(NER)

  • 名词指的是人、组织还是国家?

一个人、一面国旗和 TED 标志

Noun NER
Brian Person
DataCamp Organization
Python 中的 NLP 特征工程

本章要点

  • 文本预处理
  • 基础特征
  • 词特征
  • 向量化
Python 中的 NLP 特征工程

Passons à la pratique !

Python 中的 NLP 特征工程

Preparing Video For Download...