使用sklearn之LabelEncoder将Label标准化的方法

 更新时间:2018年07月11日 09:46:51   作者:趙大宝  
今天小编就为大家分享一篇使用sklearn之LabelEncoder将Label标准化的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

LabelEncoder可以将标签分配一个0—n_classes-1之间的编码

将各种标签分配一个可数的连续编号:

>>> 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标签转换成连续编号:

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标签分别转换成连续编号:

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标准化的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法

    python报错TypeError: ‘NoneType‘ object is not subscriptable的解决

    这篇文章主要给大家介绍了关于python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 利用Python学习RabbitMQ消息队列

    利用Python学习RabbitMQ消息队列

    RabbitMQ和邮局的主要区别就是RabbitMQ接收、存储和发送的是二进制数据----消息,本篇文章给大家介绍利用Python学习RabbitMQ消息队列,对python消息队列相关知识感兴趣的朋友参考下
    2015-11-11
  • Python实现打印螺旋矩阵功能的方法

    Python实现打印螺旋矩阵功能的方法

    这篇文章主要介绍了Python实现打印螺旋矩阵功能的方法,简单描述了螺旋矩阵的概念、原理及Python实现方法,需要的朋友可以参考下
    2017-11-11
  • python字符串常用方法

    python字符串常用方法

    这篇文章主要介绍了python字符串常用方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-06-06
  • 详解python函数的闭包问题(内部函数与外部函数详述)

    详解python函数的闭包问题(内部函数与外部函数详述)

    这篇文章主要介绍了python函数的闭包问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Python自动化开发学习之三级菜单制作

    Python自动化开发学习之三级菜单制作

    这篇文章主要为大家详细介绍了Python自动化开发学习之三级菜单的制作方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Pandas的MultiIndex多层索引使用说明

    Pandas的MultiIndex多层索引使用说明

    这篇文章主要介绍了Pandas的MultiIndex多层索引使用说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • pandas dataframe保留重复行的方法

    pandas dataframe保留重复行的方法

    如何保留Pandas DataFrame中的重复行,可以使用duplicated()方法和布尔索引,具有一定的参考价值,感兴趣的可以了解一下
    2023-11-11
  • tensorflow 大于某个值为1,小于为0的实例

    tensorflow 大于某个值为1,小于为0的实例

    这篇文章主要介绍了tensorflow 大于某个值为1,小于为0的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • python程序如何进行保存

    python程序如何进行保存

    在本篇文章里小编给大家整理了关于python程序保存的方法,有需要的朋友们可以学习下。
    2020-07-07

最新评论