python类别数据数字化LabelEncoder VS OneHotEncoder区别 目录 LabelEncoder 和 OneHotEncoder 是什么 数据集中的类别数据 LabelEncoder 和 OneHotEncoder 的区别 具体代码 LabelEncoder 和 OneHotEncoder 是什么 - 在数据处理过程中,我们有时需要对不连续的数字或者文本进行数
目录
- LabelEncoder 和 OneHotEncoder 是什么
- 数据集中的类别数据
- LabelEncoder 和 OneHotEncoder 的区别
- 具体代码
LabelEncoder 和 OneHotEncoder 是什么
- 在数据处理过程中,我们有时需要对不连续的数字或者文本进行数字化处理。
- 在使用 Python 进行数据处理时,用 encoder 来转化 dummy variable(虚拟数据)非常简便,encoder 可以将数据集中的文本转化成0或1的数值。
- LabelEncoder 和 OneHotEncoder 是 scikit-learn 包中的两个功能,可以实现上述的转化过程。
- sklearn.preprocessing.LabelEncoder
- sklearn.preprocessing.OneHotEncoder
数据集中的类别数据
在使用回归模型和机器学习模型时,所有的考察数据都是数值更容易得到好的结果。
因为回归和机器学习都是基于数学函数方法的,所以当我们要分析的数据集中出现了类别数据(categorical data),此时的数据是不理想的,因为我们不能用数学的方法处理它们。
例如,在处理男和女两个性别数据时,我们用0和1将其代替,再进行分析。
由于这种情况的出现,我们需要可以将文字数字化的现成方法。
LabelEncoder 和 OneHotEncoder 的区别
具体代码
import pandas as pd
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.cross_validation import train_test_split
# 读取数据
data_df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/'
'breast-cancer-wisconsin/wdbc.data', header=None)
# 前面的数据是特征,最后一列是标签label
x, y = data_df.values[:, :-1], data_df.values[:, -1]
# 先实例化一个对象
encoder_x = LabelEncoder()
# 对标签进行类别数据数字化
y = encoder_x.fit_transform( y )
以上就是python 数据数字化的方法LabelEncoder VS OneHotEncoder区别的详细内容,更多关于LabelEncoder VS OneHotEncoder的资料请关注我们其它相关文章!
本文标题为:python类别数据数字化LabelEncoder VS OneHotEncoder区别


- python中列表添加元素的几种方式(+、append()、ext 2022-09-02
- CentOS7 安装 Python3.6 2023-09-04
- python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor 2023-09-04
- 在centos6.4下安装python3.5 2023-09-04
- Python实现将DNA序列存储为tfr文件并读取流程介绍 2022-10-20
- Python 保存数据的方法(4种方法) 2023-09-04
- Python Pandas如何获取和修改任意位置的值(at,iat,loc,iloc) 2023-08-04
- python中defaultdict用法实例详解 2022-10-20
- Python之路-Python中的线程与进程 2023-09-04
- windows安装python2.7.12和pycharm2018教程 2023-09-03