sklearn.preprocessing.LabelEncoder的使用
在训练模型之前,我们通常都要对训练数据进行一定的处理。将类别编号就是一种常用的处理方法,比如把类别“男”,“女”编号为0和1。可以使用sklearn.preprocessing中的LabelEncoder处理这个问题。
作用
将n个类别编码为0~n-1之间的整数(包含0和n-1)。
例子
假设我们要对性别数据进行编码,则数据可以分为两种情况:无NaN,有NaN。
首先导入要使用的包
import numpy as np
import pandas as pd
from sklearn import preprocessing
无NaN
数据如下
sex = pd.Series(["male", "female", "female", "male"])
使用LabelEncoder进行处理,过程如下
le = preprocessing.LabelEncoder() #获取一个LabelEncoder
le = le.fit(["male", "female"]) #训练LabelEncoder, 把male编码为0,female编码为1
sex = le.transform(sex) #使用训练好的LabelEncoder对原数据进行编码
print(sex)
输出:
[1 0 0 1]
可以看到LabelEncoder将源数据中用字符串表示的类别编码成int型的数字,便于训练。
根据编码后的类别还可以获取编码前的类别:
le.inverse_transform([1,0,0,1])
输出:
array(['male', 'female', 'female', 'male'], dtype='<U6')
有NaN
假如数据中包含NaN,如下
sex = pd.Series(["male", "female", "female", np.nan])
这时执行
le = preprocessing.LabelEncoder() #获取一个LabelEncoder
le = le.fit(["male", "female"]) #训练LabelEncoder, 把male编码为0,female编码为1
sex = le.transform(sex) #使用训练好的LabelEncoder对原数据进行编码
print(sex)
就会出错
ValueError: y contains previously unseen labels: nan
解决方法也很简单,只要把NaN替换掉就行了
sex.fillna("unknown", inplace=True)
le = preprocessing.LabelEncoder() #获取一个LabelEncoder
le = le.fit(["male", "female", "unknown"]) #训练LabelEncoder, 把male编码为0,female编码为1, unknown为2
sex = le.transform(sex) #使用训练好的LabelEncoder对原数据进行编码
print(sex)
输出:
[1 0 0 2]
这里将NaN替换为unkown,将unknown加入le.fit中,这样unknown就会被编码为2了。
总结
sklearn.preprocessing.LabelEncoder可以简单方便地将数据中的类别编码。
sklearn.preprocessing.LabelEncoder的使用的更多相关文章
- 11.sklearn.preprocessing.LabelEncoder的作用
In [5]: from sklearn import preprocessing ...: le =preprocessing.LabelEncoder() ...: le.fit(["p ...
- 利用sklearn的LabelEncoder对标签进行数字化编码
from sklearn.preprocessing import LabelEncoder def gen_label_encoder(): labels = ['BB', 'CC'] le = L ...
- pandas 下的 one hot encoder 及 pd.get_dummies() 与 sklearn.preprocessing 下的 OneHotEncoder 的区别
sklearn.preprocessing 下除了提供 OneHotEncoder 还提供 LabelEncoder(简单地将 categorical labels 转换为不同的数字): 1. 简单区 ...
- 数据规范化——sklearn.preprocessing
sklearn实现---归类为5大类 sklearn.preprocessing.scale()(最常用,易受异常值影响) sklearn.preprocessing.StandardScaler() ...
- 【sklearn】数据预处理 sklearn.preprocessing
数据预处理 标准化 (Standardization) 规范化(Normalization) 二值化 分类特征编码 推定缺失数据 生成多项式特征 定制转换器 1. 标准化Standardization ...
- sklearn.preprocessing.LabelBinarizer
sklearn.preprocessing.LabelBinarizer
- sklearn preprocessing (预处理)
预处理的几种方法:标准化.数据最大最小缩放处理.正则化.特征二值化和数据缺失值处理. 知识回顾: p-范数:先算绝对值的p次方,再求和,再开p次方. 数据标准化:尽量将数据转化为均值为0,方差为1的数 ...
- sklearn学习笔记(一)——数据预处理 sklearn.preprocessing
https://blog.csdn.net/zhangyang10d/article/details/53418227 数据预处理 sklearn.preprocessing 标准化 (Standar ...
- sklearn.preprocessing.StandardScaler 离线使用 不使用pickle如何做
Having said that, you can query sklearn.preprocessing.StandardScaler for the fit parameters: scale_ ...
随机推荐
- C# redis简单的使用
1.项目一:用于在Redis中添加数据 using System; using System.Collections.Generic; using System.Linq; using System. ...
- UE4的AI学习(1)——基本概念
AI学习当中,不学习行为树基本概念就不能明白具体实例中的操作意义,但是没有经过具体实例实验,又觉得基本概念抽象难以理解.建议先泛读(1)(2)后再对具体的细节进行死磕,能较深的理解行为树的具体概念.第 ...
- TCP3次握手和4次挥手及其为什么
TCP 3次握手 客户端向服务器发送一个SYN(包含了SYN,SEQ). 当服务器接收到客户端发过来的SYN时,会向客户端发送一个SYN+ACK的数据包,其实ACK的ack等于上一次发送SYN数据包的 ...
- TypeError: view must be a callable or a list/tuple in the case of include()
原文连接: http://www.imooc.com/qadetail/98920 我是这么写的就好了 from django.conf.urls import url from django.con ...
- geeksforgeeks-Array-Rotation and deletion
As usual Babul is again back with his problem and now with numbers. He thought of an array of numb ...
- ROS Kinetic Install on Debian 9
Not Succesed! 1. 配置源$ sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release - ...
- C语言中malloc函数返回值是否需要类型强制转换问题
1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...
- 集群下Dubbo负载均衡配置
在集群负载均衡时,Dubbo提供了4种均衡策略,默认为Random(随机调用) 负载均衡策略: 1).Random LoadBalance(随机,按照权重的设置随机概率) 2).RoundRobin ...
- Cleaning up old NVIDIA driver files
原文地址:https://www.gameplayinside.com/optimize/cleaning-up-old-nvidia-driver-files-to-save-disk-space/ ...
- IIS8.5 运行WCF
背景 这是一个项目给其它项目提供接口,其实现在哪有用WCF的了,都是restful.不过.net在这方面还是不错,比java强些,java竟然很多采用自己解析xml方式来做Web服务.难以理解. 但是 ...