Skip to Content
Python机器学习基础教程
book

Python机器学习基础教程

by Andreas C. Müller, Sarah Guido
January 2018
Intermediate to advanced
301 pages
8h 54m
Chinese
Posts & Telecom Press
Content preview from Python机器学习基础教程
254
7
我们要解决的任务如下:给定一条评论,我们希望根据该评论的文本内容对其分配一个
“正面的”或“负面的”标签。这是一项标准的二分类任务。但是,文本数据并不是机器
学习模型可以处理的格式。我们需要将文本的字符串表示转换为数值表示,从而可以对其
应用机器学习算法。
7.3
 将文本数据表示为词袋
用于机器学习的文本表示有一种最简单的方法,也是最有效且最常用的方法,就是使用
bag-of-words
)表示。使用这种表示方式时,我们舍弃了输入文本中的大部分结构,如
章节、段落、句子和格式,只计算语料库中
每个单词在每个文本中的出现频次
。舍弃结构
并仅计算单词出现次数,这会让脑海中出现将文本表示为“袋”的画面。
对于文档语料库,计算词袋表示包括以下三个步骤。
(1)
分词
tokenization
)。将每个文档划分为出现在其中的单词
[
称为词例(
token
]
,比如
按空格和标点划分。
(2)
构建词表(
vocabulary building
)。收集一个词表,里面包含出现在任意文档中的所有词,
并对它们进行编号(比如按字母顺序排序)。
(3)
编码
encoding
)。对于每个文档,计算词表中每个单词在该文档中的出现频次。
在步骤
1
和步骤
2
中涉及一些细微之处,我们将在本章后面进一步深入讨论。目前,我们
来看一下如何利用 scikit-learn 来应用词袋处理过程。图
7-1
展示了对字符串 "This is
how you get ants." 的处理过程。其输出是包含每个文档中单词计数的一个向量。对于词 ...
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

数据驱动力:企业数据分析实战

数据驱动力:企业数据分析实战

Carl Anderson
Python应用开发指南

Python应用开发指南

Posts & Telecom Press, Ninad Sathaye
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey

Publisher Resources

ISBN: 9787115475619