Python之sklearn:LabelEncoder函数简介(编码与编码还原)、使用方法、具体案例之详细攻略
Python之sklearn:LabelEncoder函数简介(编码与编码还原)、使用方法、具体案例之详细攻略
目录
LabelEncoder函数的简介(编码与编码还原)
Methods
LabelEncoder函数的使用方法
LabelEncoder函数的具体案例
1、基础案例
2、在数据缺失和test数据内存在新值(train数据未出现过)环境下的数据LabelEncoder化
LabelEncoder函数的简介(编码与编码还原)
class LabelEncoder Found at: sklearn.preprocessing._labelclass LabelEncoder(TransformerMixin, BaseEstimator): |
""对目标标签进行编码,值在0到n_class -1之间。 这个转换器应该用于编码目标值,*即' y ',而不是输入' X '。 更多内容见:ref: ' User Guide '。 |
.. versionadded:: 0.12 Attributes ---------- classes_ : array of shape (n_class,) Holds the label for each class. Examples -------- `LabelEncoder` can be used to normalize labels. >>> 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]) array([0, 0, 1, 2]...) >>> le.inverse_transform([0, 0, 1, 2]) array([1, 1, 2, 6]) It can also be used to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels. >>> le = preprocessing.LabelEncoder() >>> le.fit(["paris", "paris", "tokyo", "amsterdam"]) LabelEncoder() >>> list(le.classes_) ['amsterdam', 'paris', 'tokyo'] >>> le.transform(["tokyo", "tokyo", "paris"]) array([2, 2, 1]...) >>> list(le.inverse_transform([2, 2, 1])) ['tokyo', 'tokyo', 'paris'] See also -------- sklearn.preprocessing.OrdinalEncoder : Encode categorical features using an ordinal encoding scheme. sklearn.preprocessing.OneHotEncoder : Encode categorical features as a one-hot numeric array. |
. .versionadded:: 0.12 属性 例子 >>> from sklearn import preprocessing 它还可以用于将非数字标签(只要它们是可hashable和可比的)转换为数字标签。 >>> le = preprocessing.LabelEncoder() 另请参阅 |
""" Parameters Returns Parameters Returns Parameters Returns Parameters Returns |
Methods
|
Fit label encoder |
|
Fit label encoder and return encoded labels |
|
Get parameters for this estimator. |
|
Transform labels back to original encoding. |
|
Set the parameters of this estimator. |
|
Transform labels to normalized encoding. |
LabelEncoder函数的使用方法
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from DataScienceNYY.DataAnalysis import dataframe_fillAnyNull,Dataframe2LabelEncoder#构造数据
train_data_dict={'Name':['张三','李四','王五','赵六','张七','李八','王十','un'],'Age':[22,23,24,25,22,22,22,None],'District':['北京','上海','广东','深圳','山东','河南','浙江',' '],'Job':['CEO','CTO','CFO','COO','CEO','CTO','CEO','']}
test_data_dict={'Name':['张三','李四','王十一',None],'Age':[22,23,22,'un'],'District':['北京','上海','广东',''],'Job':['CEO','CTO','UFO',' ']}
train_data_df = pd.DataFrame(train_data_dict)
test_data_df = pd.DataFrame(test_data_dict)
print(train_data_df,'\n',test_data_df)#缺失数据填充
for col in train_data_df.columns:train_data_df[col]=dataframe_fillAnyNull(train_data_df,col)test_data_df[col]=dataframe_fillAnyNull(test_data_df,col)
print(train_data_df,'\n',test_data_df)#数据LabelEncoder化
train_data,test_data=Dataframe2LabelEncoder(train_data_df,test_data_df)
print(train_data,'\n',test_data)
LabelEncoder函数的具体案例
1、基础案例
LabelEncoder can be used to normalize labels.>>>
>>> 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])
array([0, 0, 1, 2]...)
>>> le.inverse_transform([0, 0, 1, 2])
array([1, 1, 2, 6])
It can also be used to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels.>>>
>>> le = preprocessing.LabelEncoder()
>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])
LabelEncoder()
>>> list(le.classes_)
['amsterdam', 'paris', 'tokyo']
>>> le.transform(["tokyo", "tokyo", "paris"])
array([2, 2, 1]...)
>>> list(le.inverse_transform([2, 2, 1]))
['tokyo', 'tokyo', 'paris']
2、在数据缺失和test数据内存在新值(train数据未出现过)环境下的数据LabelEncoder化
参考文章:Python之sklearn:LabelEncoder函数的使用方法之使用LabelEncoder之前的必要操作
import numpy as np
from sklearn.preprocessing import LabelEncoder#训练train数据
LE= LabelEncoder()
LE.fit(train_df[col])#test数据中的新值添加到LE.classes_
test_df[col] =test_df[col].map(lambda s:'Unknown' if s not in LE.classes_ else s)
LE.classes_ = np.append(LE.classes_, 'Unknown') #分别转化train、test数据
train_df[col] = LE.transform(train_df[col])
test_df[col] = LE.transform(test_df[col])
Python之sklearn:LabelEncoder函数简介(编码与编码还原)、使用方法、具体案例之详细攻略相关推荐
- ML之sklearn:sklearn.metrics中常用的函数参数(比如confusion_matrix等 )解释及其用法说明之详细攻略
ML之sklearn:sklearn.metrics中常用的函数参数(比如confusion_matrix等 )解释及其用法说明之详细攻略 目录 sklearn.metrics中常用的函数参数 con ...
- BigData:大数据开发的简介、核心知识(linux基础+Java/Python编程语言+Hadoop{HDFS、HBase、Hive}+Docker)、经典场景应用之详细攻略
BigData:大数据开发的简介.核心知识(linux基础+Java/Python编程语言+Hadoop{HDFS.HBase.Hive}+Docker).经典场景应用之详细攻略 BigData:大数 ...
- Python语言学习之字母R开头函数使用集锦:random/range/replace/reshape用法之详细攻略
Python语言学习之字母R开头函数使用集锦:random/range/replace/reshape用法之详细攻略 random/range/replace/reshape用法 1.random用法 ...
- Py之Pyinstaller:Python库之Pyinstaller简介、使用方法、安装详细攻略
Py之Pyinstaller:Python库之Pyinstaller简介.使用方法.安装详细攻略 目录 Pyinstaller简介 Pyinstaller安装 Pyinstaller使用方法 1.使用 ...
- Python编程语言学习:python语言中快速查询python自带模块函数的用法及其属性方法、如何查询某个函数关键词的用法、输出一个类或者实例化对象的所有属性和方法名之详细攻略
Python编程语言学习:python语言中快速查询python自带模块&函数的用法及其属性方法.如何查询某个函数&关键词的用法.输出一个类或者实例化对象的所有属性和方法名之详细攻略 ...
- DL之DNN优化技术:神经网络算法简介之GD/SGD算法的简介、代码实现、代码调参之详细攻略
DL之DNN优化技术:神经网络算法简介之GD/SGD算法的简介.代码实现.代码调参之详细攻略 目录 GD算法的简介 GD/SGD算法的代码实现 1.Matlab编程实现 GD算法的改进算法 GD算法中 ...
- DL之AF:机器学习/深度学习中常用的激活函数(sigmoid、softmax等)简介、应用、计算图实现、代码实现详细攻略
DL之AF:机器学习/深度学习中常用的激活函数(sigmoid.softmax等)简介.应用.计算图实现.代码实现详细攻略 目录 激活函数(Activation functions)相关配图 各个激活 ...
- Cloud Computing:云网端融合的简介、层次、典型代表、未来趋势之详细攻略
Cloud Computing:云网端融合的简介.层次.典型代表.未来趋势之详细攻略 导读:云网端融合,站在行业的角度,就是IT企业和通信企业互相掐架.抢饭碗. 目录 云网端融合的简介.层次.未来趋势 ...
- CV之FD之HOG:图像检测之基于HOG算法、简介、代码实现(计算图像相似度)之详细攻略
CV之FD之HOG:图像检测之基于HOG算法.简介.代码实现(计算图像相似度)之详细攻略 图像检测之基于HOG算法.简介.代码实现(计算图像相似度)之详细攻略 相关文章:CV之FD之HOG:图像检测之 ...
最新文章
- 机器学习PAI产品架构
- 潮州市云计算数据中心挂牌 粤东地区规模最大
- python+selenium配置Edge浏览器
- Spring Boot之基于Redis实现MyBatis查询缓存解决方案
- 系统相机裁剪比例_从照相到摄影你只差这5个技巧!人像裁剪这4大原则你一定要了解...
- 数据结构实验 6.宗教信仰
- VB如何实现Undo(撤消)功能
- python中的特殊函数__call__
- matlab最后ceq,求解 minimax 约束问题
- 一款基于java开发的开源监控平台
- Hadoop(八)网站流量分析
- 关于数学计算机手抄报简单的,数学手抄报简单又漂亮图片
- python学习使用easyocr识别图片文字
- Euresys eVision 加载和保存图像
- 子网掩码是什么,IP段的24是什么写法(CIDR写法,斜杠记法斜线记法)
- Yii2-Admin-Theme 基于layui的通用后台模板
- winform 中listBox控件绑定list类型数据源,并设置DisplayMember
- 处理el获取session值:严重: Servlet.service() for servlet [LoginServlet] in context with path [/LDMS]...
- springboot+druid+dynamic-datasource+mysql数据库密码加密
- ecshop2.72文件结构说明