Skip to Content
Python文本分析
book

Python文本分析

by Jens Albrecht, Sidharth Ramachandran, Christian Winkler
August 2022
Intermediate to advanced
441 pages
11h 26m
Chinese
China Electric Power Press Ltd.
Content preview from Python文本分析
特征工程与句法相似性
147
另一种我们经常会遇到的计算相似性的方法是,计算两个文档向量的点积(
Dot
Product
,又称数量积)。点积的计算是对两个向量的相应元素求积,然后再对所有
积求和。可见,只有当两个元素都为
1
时,积才为
1
,因此我们可以快速计算向量
中同一位置上均为
1
的元素个数。下面,我们来试试看:
np.dot(onehot[0], onehot[1])
输出结果:
4
5.3.4
相似性矩阵
如果想找出所有文档彼此之间的相似性,那么有一种非常强大的快捷方式,只需一
个命令即可计算出所有的数字。我们可以根据上一节概括出一个公式,我们发现文
i
与文档
j
的相似性可以表示如下:
S
ij
= d
i
·d
j
使用上述文档词条矩阵,我们就可以求出点积之和:
S
ij
=
k
D
ik
D
jk
=
k
D
ik
D
T
kj
= D·D
T
ij
上述公式是文档词条矩阵与其转置矩阵的乘积。在
Python
中,这种计算非常简单(为
了方便检查相似性,结果中输出了句子本身):
3
np.dot(onehot, np.transpose(onehot))
输出结果:
array([[6, 4, 3, 3], # It was the best of times
[4, 6, 4, 4], # it was the worst of times
3 容易令人不解的是,
numpy.dot
既可用于点积(内积),也可用于矩阵乘法。如果
Numpy 检测到两个相同维度的行向量或列向量(即一维数组),它就会计算点积并
产生一个标量。否则 ...
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

精益AI

精益AI

Lomit Patel
构建知识图谱

构建知识图谱

Jesus Barrasa, Jim Webber
写给系统管理员的Python脚本编程指南

写给系统管理员的Python脚本编程指南

Posts & Telecom Press, Ganesh Sanjiv Naik

Publisher Resources

ISBN: 9787519864446