SKlear中的LabelEncoding和OneHotEncoding
来源:互联网 发布:basic和vb 编辑:程序博客网 时间:2024/06/18 04:02
# 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)
0 0
- SKlear中的LabelEncoding和OneHotEncoding
- 决策树独热onehotencoding
- c#中的'&'和'&&','|'和'||'
- linux中的&&和&,|和||
- javascript中的+和C#中的+
- Windows中的\和Java中的\
- 编程中的++和--
- 宏定义中的## 和#
- define 中的#,##和#@
- Bash中的&& 和 || 用法
- SQL Server 中的''和' '
- OGNL中的#、%和$符号
- OGNL表达式中的#、%和$
- Ibatis中的#和$区别
- Ksh中的$*和$@
- Shell中的&&、||、()和{}
- String中的“+”和StringBuilder
- 页面中的&和&;
- NavigationView+ToolBar+TabLayout+ViewPager+Fragment综合使用
- CREELINKS平台_处理器CeCcp资源使用说明(CeCcp的配置与使用)
- PAT甲级1009. Product of Polynomials (25)
- Java集合HashMap源码
- Win10 Mobile For MI4体验
- SKlear中的LabelEncoding和OneHotEncoding
- git基本命令
- Python11
- 忘记密码解决,Win7 Administrator账号密码忘记 解决办法
- 用仿真器解决Linux内核加载问题
- 黑科技 —— Type-C 接口与 USB3.1
- Linux内核分析 实验1
- Codeforces Round #401 (Div. 2) B. Game of Credit Cards
- 游戏开发中常用的设计模式