Skip to Content
精通特征工程
book

精通特征工程

by Alice Zheng, Amanda Casari
April 2019
Intermediate to advanced
172 pages
4h 39m
Chinese
Posts & Telecom Press
Content preview from 精通特征工程
66
5
5.1
 分类变量的编码
分类变量中的类别通常不是数值型的。
1
例如,眼睛的颜色可以是“黑色”“蓝色”和“褐
色”,等等。因此,需要一种编码方法来将非数值型的类别转换为数值。我们很容易想到,
可以简单地为
k
个可能类别中的每个类别分配一个整数,如从
1
k
,但这样做的结果是
使类别彼此之间有了顺序,这在分类变量中是不允许的。所以,我们要想个别的方法。
5.1.1
 
one-hot
编码
更好的方法是使用一组比特位,每个比特位表示一种可能的类别。如果变量不能同时属于
多个类别,那么这组值中就只有一个比特位是“开”的。这就是
one-hot
编码
,它可以通
scikit-learn
中的
sklearn.preprocessing.OneHotEncoder
实现。每个比特位表示一个特
征,因此,一个可能有
k
个类别的分类变量就可以编码为一个长度为
k
的特征向量。表
5-1
给出了一个例子。
5-1:表示3个城市的分类变量的one-hot编码
e
1
e
2
e
3
San Francisco 1 0 0
New York 0 1 0
Seattle 0 0 1
one-hot
编码很容易理解,但它使用的比特位要比实际需要的多一位。如果
k
-
1
位都是
0
那么最后一位肯定是
1
,因为变量必须取
k
个值中的一个。在数学上,可以将这个限制条
件表述为“所有位的和必须等于
1
”:
e
1
+
e
2
+
+
e
k
= 1
于是就得到了一个线性相关关系。正如我们在第
4
章中发现的那样,线性相关的特征有一
点讨厌,因为它们会使训练出的模型不唯一。特征的不同线性组合可以做出同样的预测 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

精通機器學習

精通機器學習

Aurélien Géron

Publisher Resources

ISBN: 9787115509680