為什麼需要特征工程(Feature Engineering)
機器學習領域的大神Andrew Ng(吳恩達)老師說“Coming up with features is difficult, time-consuming, requires expert knowledge. “Applied machine learning” is basically feature engineering. ”
注:業界廣泛流傳:資料和特征決定了機器學習的上限,而模型和算法隻是逼近這個上限而已。
什麼是特征工程
定義不唯一
特征工程是使用專業背景知識和技巧處理資料,使得特征能在機器學習算法上發揮更好的作用的過程。
意義:會直接影響機器學習的效果
特征工程 & 資料處理

- sklearn 專門用來做特征工程
- pandas 資料清洗、資料處理
特征工程介紹
特征工程包含内容:
- 特征抽取
- 特征預處理
- 特征降維
特征抽取/特征提取
特征提取
将任意資料(如文本或圖像)轉換為可用于機器學習的數字特征
特征值化
特征值化是為了讓計算機能夠更好地了解資料
- 字典特征提取(特征離散化)
- 文本特征提取
- 圖像特征提取(深度學習将介紹)
如何在sklearn中實作特征提取?
sklearn.feature_extraction
字典特征提取
作用:對字典資料進行特征值化
vector 數學:向量 實體:矢量
既有大小、又有方向
那麼我們應該如何在計算機中使用vector呢?
矩陣 matrix 多元數組
矩陣是由向量構成的
向量 vector 一維數組
![]()
08-字典特征抽取 ![]()
08-字典特征抽取 ![]()
08-字典特征抽取
應用
我們對以下資料進行特征提取
[{'city': '北京','temperature':100}
{'city': '上海','temperature':60}
{'city': '深圳','temperature':30}]
# -*- coding: utf-8 -*-
"""
@Time : 2021/3/7 14:50
@Author : yuhui
@Email : [email protected]
@FileName: 08_字典特征抽取.py
@Software: PyCharm
"""
from sklearn.feature_extraction import DictVectorizer
def dict_feature_extraction():
"""
字典特征抽取
:return:
"""
data=[{'city': '北京','temperature':100},
{'city': '上海','temperature':60},
{'city': '深圳','temperature':30}]
# 執行個體化一個轉化器類
# transfer=DictVectorizer(sparse=True)
transfer=DictVectorizer(sparse=False)
# sparse 稀疏
# fit_transform()
data_new=transfer.fit_transform(data)
print(data_new)
if __name__ == '__main__':
dict_feature_extraction()
# -*- coding: utf-8 -*-
"""
@Time : 2021/3/7 14:50
@Author : yuhui
@Email : [email protected]
@FileName: 08_字典特征抽取.py
@Software: PyCharm
"""
from sklearn.feature_extraction import DictVectorizer
def dict_feature_extraction():
"""
字典特征抽取
:return:
"""
data=[{'city': '北京','temperature':100},
{'city': '上海','temperature':60},
{'city': '深圳','temperature':30}]
# 執行個體化一個轉化器類
# transfer=DictVectorizer(sparse=True)
transfer=DictVectorizer(sparse=False)
# sparse 稀疏
# fit_transform()
data_new=transfer.fit_transform(data)
# print(data_new)
# 檢視屬性
# 檢視特征名字
# print(transfer.feature_names_) # 方法一
# print(transfer.get_feature_names()) # 方法二
if __name__ == '__main__':
dict_feature_extraction()
"one-hot"編碼
應用場景
凡是可以化為以下形式的資料,我們都可以使用字典特征提取的方法來對資料進行特征提取。
小結
字典特征提取
- 導入庫
from sklearn.feature_extraction import DictVectorizer
- 執行個體化對象
- 調用方法
- 檢視屬性
# 檢視特征名字
print(transfer.feature_names_) # 方法一
print(transfer.get_feature_names()) # 方法二
注意
- sparse=True
- sparse=False
第一次複習
# -*- coding: utf-8 -*-
"""
@Time : 2021/4/8 13:40
@Author : yuhui
@Email : [email protected]
@FileName: 08_字典特征抽取_2.py
@Software: PyCharm
"""
from sklearn import feature_extraction
def dictionary_feature_extraction():
"""字典特征提取"""
data=[{'city': '北京','temperature':100},
{'city': '上海','temperature':60},
{'city': '深圳','temperature':30}]
transfer=feature_extraction.DictVectorizer(sparse=True)
data_new=transfer.fit_transform(data)
print(data_new)
print(data)
# 檢視屬性
# 檢視特征名字
print(transfer.feature_names_)
print(transfer.get_feature_names())
# 檢視
print(transfer.inverse_transform(data_new))
if __name__ == '__main__':
dictionary_feature_extraction()
D:\Anaconda3\Installation\envs\math\python.exe D:/Machine_Learning/Machine_Learning_1/code/08_字典特征抽取_2.py
(0, 1) 1.0
(0, 3) 100.0
(1, 0) 1.0
(1, 3) 60.0
(2, 2) 1.0
(2, 3) 30.0
[{'city': '北京', 'temperature': 100}, {'city': '上海', 'temperature': 60}, {'city': '深圳', 'temperature': 30}]
['city=上海', 'city=北京', 'city=深圳', 'temperature']
['city=上海', 'city=北京', 'city=深圳', 'temperature']
[{'city=北京': 1.0, 'temperature': 100.0}, {'city=上海': 1.0, 'temperature': 60.0}, {'city=深圳': 1.0, 'temperature': 30.0}]
Process finished with exit code 0