使用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 相关文章推荐
JPype实现在python中调用JAVA的实例
Jul 19 Python
Python SQLite3简介
Feb 22 Python
解决Pandas to_json()中文乱码,转化为json数组的问题
May 10 Python
用PyInstaller把Python代码打包成单个独立的exe可执行文件
May 26 Python
python面向对象入门教程之从代码复用开始(一)
Dec 11 Python
python实现将多个文件分配到多个文件夹的方法
Jan 07 Python
详解python 3.6 安装json 模块(simplejson)
Apr 02 Python
详解用python写网络爬虫-爬取新浪微博评论
May 10 Python
Python搭建Spark分布式集群环境
Jul 05 Python
python线程中的同步问题及解决方法
Aug 29 Python
基于python实现操作redis及消息队列
Aug 27 Python
Pytorch 如何实现LSTM时间序列预测
May 17 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
PHILIPS D1835/D1875的电路分析与打理
2021/03/02 无线电
关于PHP5 Session生命周期介绍
2010/03/02 PHP
通过curl模拟post和get方式提交的表单类
2014/04/23 PHP
php计算程序运行时间的简单例子分享
2014/05/10 PHP
ThinkPHP中的三大自动简介
2014/08/22 PHP
php安装swoole扩展的方法
2015/03/19 PHP
开启PHP Static 关键字之旅模式
2015/11/13 PHP
PHP读取大文件的几种方法介绍
2016/10/27 PHP
PHP中抽象类,接口功能、定义方法示例
2019/02/26 PHP
js编码、解码函数介绍及其使用示例
2013/09/05 Javascript
深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制
2014/06/10 Javascript
推荐8款jQuery轻量级树形Tree插件
2014/11/12 Javascript
Javascript将数字转化成为货币格式字符串
2016/06/22 Javascript
BootStrap3使用错误记录及解决办法
2016/12/22 Javascript
原生js实现放大镜效果
2017/01/11 Javascript
javascript容错处理代码(屏蔽js错误)
2017/01/20 Javascript
使用Bootstrap + Vue.js实现添加删除数据示例
2017/02/27 Javascript
详解Angular 4 表单快速入门
2017/06/05 Javascript
微信小程序图片宽100%显示并且不变形
2017/06/21 Javascript
修改node.js默认的npm安装目录实例
2018/05/15 Javascript
Vant的安装和配合引入Vue.js项目里的方法步骤
2018/12/05 Javascript
详解Angular Karma测试的持续集成实践
2019/11/15 Javascript
python 输出一个两行字符的变量
2009/02/05 Python
Python中字典的基本知识初步介绍
2015/05/21 Python
python中私有函数调用方法解密
2016/04/29 Python
Python使用struct处理二进制的实例详解
2017/09/11 Python
浅谈Python3实现两个矩形的交并比(IoU)
2020/01/18 Python
关于CSS Tooltips(鼠标经过时显示)的效果
2013/04/10 HTML / CSS
世界上最大的巴士旅游观光公司:Big Bus Tours
2016/10/20 全球购物
卡拉威高尔夫官方网站:Callaway Golf
2020/09/16 全球购物
如何保障Web服务器安全
2014/05/05 面试题
5个HTML5的常用本地存储方式详解与介绍
2021/03/27 HTML / CSS
2014年小学数学工作总结
2014/12/12 职场文书
税务会计岗位职责
2015/04/02 职场文书
超市主管竞聘书
2015/09/15 职场文书
macos系统如何实现微信双开? mac登录两个微信以上微信的技巧
2022/07/23 数码科技