使用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进程通信之匿名管道实例讲解
Apr 11 Python
python虚拟环境virtualenv的安装与使用
Sep 21 Python
Django开发中复选框用法示例
Mar 20 Python
Flask框架Flask-Login用法分析
Jul 23 Python
对python打乱数据集中X,y标签对的方法详解
Dec 14 Python
使用Template格式化Python字符串的方法
Jan 22 Python
TensorFlow实现简单的CNN的方法
Jul 18 Python
简单了解python元组tuple相关原理
Dec 02 Python
Python3 ID3决策树判断申请贷款是否成功的实现代码
May 21 Python
Pytorch转onnx、torchscript方式
May 25 Python
Python importlib模块重载使用方法详解
Oct 13 Python
Python超详细分步解析随机漫步
Mar 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
php中将数组转成字符串并保存到数据库中的函数代码
2013/09/29 PHP
[原创]php实现子字符串位置相互对调互换的方法
2016/06/02 PHP
PHP会员找回密码功能的简单实现
2016/09/05 PHP
PHP连接MySQL数据库操作代码实例解析
2020/07/11 PHP
js仿淘宝和百度文库的评分功能
2016/05/15 Javascript
Javascript前端经典的面试题及答案
2017/03/14 Javascript
Node.js设置CORS跨域请求中多域名白名单的方法
2017/03/28 Javascript
JS同步、异步、延迟加载的方法
2018/05/05 Javascript
javascript之分片上传,断点续传的实际项目实现详解
2019/09/05 Javascript
利用PHP实现递归删除链表元素的方法示例
2020/10/23 Javascript
[02:11]2016国际邀请赛中国区预选赛全程回顾
2016/07/01 DOTA
深入源码解析Python中的对象与类型
2015/12/11 Python
Python读写txt文本文件的操作方法全解析
2016/06/26 Python
Django视图之ORM数据库查询操作API的实例
2017/10/27 Python
Python实现PS滤镜功能之波浪特效示例
2018/01/26 Python
python tornado微信开发入门代码
2018/08/24 Python
python实现多张图片拼接成大图
2019/01/15 Python
对python使用telnet实现弱密码登录的方法详解
2019/01/26 Python
python实现贪吃蛇游戏
2020/03/21 Python
Python实现定时执行任务的三种方式简单示例
2019/03/30 Python
200行python代码实现2048游戏
2019/07/17 Python
Numpy之将矩阵拉成向量的实例
2019/11/30 Python
聊聊python中的循环遍历
2020/09/07 Python
python利用google翻译方法实例(翻译字幕文件)
2020/09/21 Python
基于css3 animate制作绚丽的动画效果
2015/11/24 HTML / CSS
HTML5手指下滑弹出负一屏阻止移动端浏览器内置下拉刷新功能的实现代码
2020/04/10 HTML / CSS
介绍一下linux的文件系统
2015/10/06 面试题
电子银行营销方案
2014/02/22 职场文书
应聘文员自荐信范文
2014/03/11 职场文书
《小鹰学飞》教学反思
2014/04/23 职场文书
食品安全汇报材料
2014/08/18 职场文书
国际贸易实务实训报告
2014/11/05 职场文书
2014年村计划生育工作总结
2014/11/14 职场文书
金砖之国观后感
2015/06/11 职场文书
2019新员工试用期转正工作总结范文
2019/08/21 职场文书
在前女友婚礼上,用Python破解了现场的WIFI还把名称改成了
2021/05/28 Python