使用sklearn之LabelEncoder將Label標(biāo)準(zhǔn)化的方法
LabelEncoder可以將標(biāo)簽分配一個(gè)0—n_classes-1之間的編碼
將各種標(biāo)簽分配一個(gè)可數(shù)的連續(xù)編號(hào):
>>> from sklearn import preprocessing >>> le = preprocessing.LabelEncoder() >>> le.fit([1, 2, 2, 6]) LabelEncoder() >>> le.classes_ array([1, 2, 6]) >>> le.transform([1, 1, 2, 6]) # Transform Categories Into Integers array([0, 0, 1, 2], dtype=int64) >>> le.inverse_transform([0, 0, 1, 2]) # Transform Integers Into Categories array([1, 1, 2, 6])
>>> le = preprocessing.LabelEncoder() >>> le.fit(["paris", "paris", "tokyo", "amsterdam"]) LabelEncoder() >>> list(le.classes_) ['amsterdam', 'paris', 'tokyo'] >>> le.transform(["tokyo", "tokyo", "paris"]) # Transform Categories Into Integers array([2, 2, 1], dtype=int64) >>> list(le.inverse_transform([2, 2, 1])) #Transform Integers Into Categories ['tokyo', 'tokyo', 'paris']
將DataFrame中的所有ID標(biāo)簽轉(zhuǎn)換成連續(xù)編號(hào):
from sklearn.preprocessing import LabelEncoder import numpy as np import pandas as pd df=pd.read_csv('testdata.csv',sep='|',header=None)
0 1 2 3 4 5 0 37 52 55 50 38 54 1 17 32 20 9 6 48 2 28 10 56 51 45 16 3 27 49 41 30 53 19 4 44 29 8 1 46 13 5 11 26 21 14 7 33 6 0 39 22 33 35 43 7 18 15 47 5 25 34 8 23 2 4 9 3 31 9 12 57 36 40 42 24
le = LabelEncoder() le.fit(np.unique(df.values)) df.apply(le.transform)
0 1 2 3 4 5 0 37 52 55 50 38 54 1 17 32 20 9 6 48 2 28 10 56 51 45 16 3 27 49 41 30 53 19 4 44 29 8 1 46 13 5 11 26 21 14 7 33 6 0 39 22 33 35 43 7 18 15 47 5 25 34 8 23 2 4 9 3 31 9 12 57 36 40 42 24
將DataFrame中的每一行ID標(biāo)簽分別轉(zhuǎn)換成連續(xù)編號(hào):
import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.pipeline import Pipeline class MultiColumnLabelEncoder: def __init__(self,columns = None): self.columns = columns # array of column names to encode def fit(self,X,y=None): return self # not relevant here def transform(self,X): ''' Transforms columns of X specified in self.columns using LabelEncoder(). If no columns specified, transforms all columns in X. ''' output = X.copy() if self.columns is not None: for col in self.columns: output[col] = LabelEncoder().fit_transform(output[col]) else: for colname,col in output.iteritems(): output[colname] = LabelEncoder().fit_transform(col) return output def fit_transform(self,X,y=None): return self.fit(X,y).transform(X)
MultiColumnLabelEncoder(columns = [0, 1, 2, 3, 4, 5]).fit_transform(df)
或者
df.apply(LabelEncoder().fit_transform)
0 1 2 3 4 5 0 8 8 8 7 5 9 1 3 5 2 2 1 8 2 7 1 9 8 7 1 3 6 7 6 4 9 2 4 9 4 1 0 8 0 5 1 3 3 3 2 5 6 0 6 4 5 4 7 7 4 2 7 1 3 6 8 5 0 0 2 0 4 9 2 9 5 6 6 3
# Create some toy data in a Pandas dataframe fruit_data = pd.DataFrame({ 'fruit': ['apple','orange','pear','orange'], 'color': ['red','orange','green','green'], 'weight': [5,6,3,4] })
color fruit weight 0 red apple 5 1 orange orange 6 2 green pear 3 3 green orange 4
MultiColumnLabelEncoder(columns = ['fruit','color']).fit_transform(fruit_data)
或者
fruit_data[['fruit','color']]=fruit_data[['fruit','color']].apply(LabelEncoder().fit_transform)
color fruit weight 0 2 0 5 1 1 1 6 2 0 2 3 3 0 1 4
以上這篇使用sklearn之LabelEncoder將Label標(biāo)準(zhǔn)化的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Appium+Python自動(dòng)化環(huán)境搭建實(shí)例教程
這篇文章主要介紹了Appium+Python自動(dòng)化環(huán)境搭建實(shí)例教程,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Flask入門教程實(shí)例:搭建一個(gè)靜態(tài)博客
這篇文章主要介紹了Flask入門教程實(shí)例:搭建一個(gè)靜態(tài)博客,本文主要介紹flask框架的環(huán)境配置以及一個(gè)靜態(tài)博客胡搭建實(shí)例,需要的朋友可以參考下2015-03-03python dataprep庫簡(jiǎn)化加速數(shù)據(jù)科學(xué)操作
這篇文章主要為大家介紹了python dataprep庫簡(jiǎn)化加速數(shù)據(jù)科學(xué)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python小游戲之300行代碼實(shí)現(xiàn)俄羅斯方塊
這篇文章主要給大家介紹了關(guān)于Python小游戲之300行代碼實(shí)現(xiàn)俄羅斯方塊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2019-01-01Python函數(shù)進(jìn)階之迭代器的原理與使用詳解
能被?next?指針調(diào)用,并不斷返回下一個(gè)值的對(duì)象,叫做迭代器。表示為Iterator,迭代器是一個(gè)對(duì)象類型數(shù)據(jù)。本文將詳細(xì)為大家講講迭代器的原理及使用,感興趣的可以學(xué)習(xí)一下2022-04-04python下載圖片實(shí)現(xiàn)方法(超簡(jiǎn)單)
下面小編就為大家?guī)硪黄猵ython下載圖片實(shí)現(xiàn)方法(超簡(jiǎn)單)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07用Python創(chuàng)建簡(jiǎn)易網(wǎng)站圖文教程
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識(shí),文章圍繞著用Python創(chuàng)建簡(jiǎn)易網(wǎng)站展開,文中有非常詳細(xì)的介紹及圖文示例,需要的朋友可以參考下2021-06-06Python用來做Web開發(fā)的優(yōu)勢(shì)有哪些
這篇文章主要介紹了Python用來做Web開發(fā)的優(yōu)勢(shì)有哪些,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下2020-08-08