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


Posted in Python onJuly 11, 2018

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 相关文章推荐
Python使用Pycrypto库进行RSA加密的方法详解
Jun 06 Python
Python中的os.path路径模块中的操作方法总结
Jul 07 Python
详谈Python2.6和Python3.0中对除法操作的异同
Apr 28 Python
python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
Apr 03 Python
python图像处理入门(一)
Apr 04 Python
django框架模板中定义变量(set variable in django template)的方法分析
Jun 24 Python
Python队列RabbitMQ 使用方法实例记录
Aug 05 Python
python 默认参数相关知识详解
Sep 18 Python
Python编程快速上手——正则表达式查找功能案例分析
Feb 28 Python
Python如何生成xml文件
Jun 04 Python
Pytho爬虫中Requests设置请求头Headers的方法
Sep 22 Python
python cv2图像质量压缩的算法示例
Jun 04 Python
Python实现识别图片内容的方法分析
Jul 11 #Python
对python 数据处理中的LabelEncoder 和 OneHotEncoder详解
Jul 11 #Python
python对离散变量的one-hot编码方法
Jul 11 #Python
Python基于多线程操作数据库相关问题分析
Jul 11 #Python
pandas 按照特定顺序输出的实现代码
Jul 10 #Python
Python OpenCV处理图像之图像直方图和反向投影
Jul 10 #Python
Python中 map()函数的用法详解
Jul 10 #Python
You might like
PHP创建PowerPoint2007文档的方法
2015/12/10 PHP
PHP中时间加减函数strtotime用法分析
2017/04/26 PHP
PHP魔术方法之__call与__callStatic使用方法
2017/07/23 PHP
如何通过PHP实现Des加密算法代码实例
2020/05/09 PHP
IE事件对象(The Internet Explorer Event Object)
2012/06/27 Javascript
web开发人员学习jQuery的6大理由及jQuery的优势介绍
2013/01/03 Javascript
jquery实现在页面加载完毕后获取图片高度或宽度
2014/06/16 Javascript
使用ajaxfileupload.js实现ajax上传文件php版
2014/06/26 Javascript
详解JavaScript中常用的函数类型
2015/11/18 Javascript
JQuery中attr属性和jQuery.data()学习笔记【必看】
2016/05/18 Javascript
Bootstrap开发实战之第一次接触Bootstrap
2016/06/02 Javascript
domReady的实现案例
2016/11/23 Javascript
js实现水平滚动菜单导航
2017/07/21 Javascript
jQuery插件实现的日历功能示例【附源码下载】
2018/09/07 jQuery
vue-router 手势滑动触发返回功能
2018/09/30 Javascript
vue下的@change事件的实现
2019/10/25 Javascript
JavaScript实现英语单词题库
2019/12/24 Javascript
vue路由权限校验功能的实现代码
2020/06/07 Javascript
vue用elementui写form表单时,在label里添加空格操作
2020/08/13 Javascript
[01:15]《辉夜杯》北京网鱼队巡礼
2015/10/26 DOTA
python求crc32值的方法
2014/10/05 Python
python中readline判断文件读取结束的方法
2014/11/08 Python
python通过索引遍历列表的方法
2015/05/04 Python
基于Python List的赋值方法
2018/06/23 Python
Python使用crontab模块设置和清除定时任务操作详解
2019/04/09 Python
详解在python操作数据库中游标的使用方法
2019/11/12 Python
学习Python列表的基础知识汇总
2020/03/10 Python
Python爬虫之Selenium设置元素等待的方法
2020/12/04 Python
咖啡为什么会有酸味?你喝到的咖啡為什麼是酸的?
2021/03/17 冲泡冲煮
css3绘制天猫logo实现代码
2012/11/06 HTML / CSS
AMAVII眼镜官网:时尚和设计师太阳镜
2019/05/05 全球购物
Pedro官网:新加坡时尚品牌
2019/08/27 全球购物
给妈妈洗脚活动方案
2014/08/16 职场文书
初中家长评语和期望
2014/12/26 职场文书
婚宴祝酒词大全
2015/08/10 职场文书
学校2016年圣诞节活动总结
2016/03/31 职场文书