SKlear中的LabelEncoding和OneHotEncoding
2017-02-25 23:28
295 查看
# In this exercise we'll load the titanic data (from Project 0)
# And then perform one-hot encoding on the feature names
import numpy as np
import pandas as pd
# Load the dataset
X = pd.read_csv('titanic_data.csv')
# Limit to categorical data
X = X.select_dtypes(include=[object])
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
# TODO: Create a LabelEncoder object, which will turn all labels present in
# in each feature to numbers. For example, the labels ['cat', 'dog', 'fish']
# might be transformed into [0, 1, 2]
le = LabelEncoder()
# TODO: For each feature in X, apply the LabelEncoder's fit_transform
# function, which will first learn the labels for the feature (fit)
# and then change the labels to numbers (transform).
for feature in X:
le.fit(X[feature])
le.classes_
X[feature] = le.transform(X[feature])
# TODO: Create a OneHotEncoder object, which will create a feature for each
# label present in the data. For example, for a feature 'animal' that had
# the labels ['cat','dog','fish'], the new features (instead of 'animal')
# could be ['animal_cat', 'animal_dog', 'animal_fish']
ohe = OneHotEncoder()
# TODO: Apply the OneHotEncoder's fit_transform function to all of X, which will
# first learn of all the (now numerical) labels in the data (fit), and then
# change the data to one-hot encoded entries (transform).
ohe.fit(X)
onehotlabels = ohe.transform(X)
# And then perform one-hot encoding on the feature names
import numpy as np
import pandas as pd
# Load the dataset
X = pd.read_csv('titanic_data.csv')
# Limit to categorical data
X = X.select_dtypes(include=[object])
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
# TODO: Create a LabelEncoder object, which will turn all labels present in
# in each feature to numbers. For example, the labels ['cat', 'dog', 'fish']
# might be transformed into [0, 1, 2]
le = LabelEncoder()
# TODO: For each feature in X, apply the LabelEncoder's fit_transform
# function, which will first learn the labels for the feature (fit)
# and then change the labels to numbers (transform).
for feature in X:
le.fit(X[feature])
le.classes_
X[feature] = le.transform(X[feature])
# TODO: Create a OneHotEncoder object, which will create a feature for each
# label present in the data. For example, for a feature 'animal' that had
# the labels ['cat','dog','fish'], the new features (instead of 'animal')
# could be ['animal_cat', 'animal_dog', 'animal_fish']
ohe = OneHotEncoder()
# TODO: Apply the OneHotEncoder's fit_transform function to all of X, which will
# first learn of all the (now numerical) labels in the data (fit), and then
# change the data to one-hot encoded entries (transform).
ohe.fit(X)
onehotlabels = ohe.transform(X)
相关文章推荐
- 所涉及到的几种 sklearn 的二值化编码函数:OneHotEncoder(), LabelEncoder(), LabelBinarizer(), MultiLabelBinarizer()
- Label Encoding & One-Hot Encoding
- sklearn.preprocessing.LabelEncoder和onehotencoder
- 所涉及到的几种 sklearn 的二值化编码函数:OneHotEncoder(), LabelEncoder(), LabelBinarizer(), MultiLabelBinarizer()
- Label Encoding vs One Hot Encoding
- 对python 数据处理中的LabelEncoder 和 OneHotEncoder详解
- 决策树独热onehotencoding
- labelencoding onehot
- LabelEncoderm OneHotEncoder 在python中的运用
- sklearn preprocessing 数据预处理(OneHotEncoder)
- one hot coding
- ... setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key latitudeLabel.
- 基于sklearn的序列处理 : LabelEncoder 与 OneHotEncoder
- (Andrew NG)The Importance of Encoding Versus Training with Sparse Coding and Vector Quantization
- sklearn.preprocessing.OneHotEncoder
- Spark MLlib特征处理:OneHotEncoder OneHot编码 ---原理及实战
- one hot coding -机器学习
- 【机器学习】one hot encoding 独热编码
- one hot coding -机器学习
- sklearn preprocessing 数据预处理(OneHotEncoder)