python類別數(shù)據(jù)數(shù)字化LabelEncoder?VS?OneHotEncoder區(qū)別
LabelEncoder 和 OneHotEncoder 是什么
- 在數(shù)據(jù)處理過程中,我們有時需要對不連續(xù)的數(shù)字或者文本進行數(shù)字化處理。
- 在使用 Python 進行數(shù)據(jù)處理時,用 encoder 來轉(zhuǎn)化 dummy variable(虛擬數(shù)據(jù))非常簡便,encoder 可以將數(shù)據(jù)集中的文本轉(zhuǎn)化成0或1的數(shù)值。
- LabelEncoder 和 OneHotEncoder 是 scikit-learn 包中的兩個功能,可以實現(xiàn)上述的轉(zhuǎn)化過程。
- sklearn.preprocessing.LabelEncoder
- sklearn.preprocessing.OneHotEncoder
數(shù)據(jù)集中的類別數(shù)據(jù)
在使用回歸模型和機器學習模型時,所有的考察數(shù)據(jù)都是數(shù)值更容易得到好的結(jié)果。
因為回歸和機器學習都是基于數(shù)學函數(shù)方法的,所以當我們要分析的數(shù)據(jù)集中出現(xiàn)了類別數(shù)據(jù)(categorical data),此時的數(shù)據(jù)是不理想的,因為我們不能用數(shù)學的方法處理它們。
例如,在處理男和女兩個性別數(shù)據(jù)時,我們用0和1將其代替,再進行分析。
由于這種情況的出現(xiàn),我們需要可以將文字數(shù)字化的現(xiàn)成方法。
LabelEncoder 和 OneHotEncoder 的區(qū)別

具體代碼
import pandas as pd
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.cross_validation import train_test_split
# 讀取數(shù)據(jù)
data_df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/'
'breast-cancer-wisconsin/wdbc.data', header=None)
# 前面的數(shù)據(jù)是特征,最后一列是標簽label
x, y = data_df.values[:, :-1], data_df.values[:, -1]
# 先實例化一個對象
encoder_x = LabelEncoder()
# 對標簽進行類別數(shù)據(jù)數(shù)字化
y = encoder_x.fit_transform( y )
以上就是python 數(shù)據(jù)數(shù)字化的方法LabelEncoder VS OneHotEncoder區(qū)別的詳細內(nèi)容,更多關于LabelEncoder VS OneHotEncoder的資料請關注腳本之家其它相關文章!
相關文章
手把手教你用Python打造互動式中秋節(jié)慶祝小程序
中秋節(jié)將至,本文提供了一個使用Python開發(fā)的中秋節(jié)慶祝小程序教程,通過簡單的步驟,您可以創(chuàng)建一個具有節(jié)日祝福、互動式燈謎游戲和模擬中秋明月動態(tài)背景的小程序,文章詳細介紹了程序的功能、實現(xiàn)步驟以及如何運行程序,需要的朋友可以參考下2024-09-09
python編程-將Python程序轉(zhuǎn)化為可執(zhí)行程序[整理]
python編程-將Python程序轉(zhuǎn)化為可執(zhí)行程序[整理]...2007-04-04
python中使用ctypes調(diào)用so傳參設置遇到的問題及解決方法
這篇文章主要介紹了python中使用ctypes調(diào)用so傳參設置,本文較詳細的給大家介紹了遇到問題及解決方案,需要的朋友可以參考下2019-06-06

