【发布时间】:2018-06-16 18:59:17
【问题描述】:
我正在实施决策树算法并尝试使用正交性来衡量拆分的质量。我的理解是我将正交性计算为:
1−cosθ(Pi,Pj)
其中i是拆分前的数据分区,j是拆分后的分区。 Pi 和 Pj 是每个分区中每个目标值的概率向量。
我已经实现了以下内容,但我不确定我是否正确解释了这一点。我有 6 个类,向量 1 在 1 类中占 66%,在 2 类中占 33%,在其余类中没有。向量 2 和 3 具有相同的分布(40%、10%、10%、20%、10%、10%)
import numpy as np
def calculate_orthogonality(vector_1, vector_2):
dot_product = np.dot(vector_1, vector_2)
orthogonality = 1 - np.cos(dot_product)
return orthogonality
vector1 = [0.6666666666666666, 0.3333333333333333, 0, 0, 0, 0]
vector2 = [0.4, 0.1, 0.1, 0.2, 0.1, 0.1]
vector3 = [0.4, 0.1, 0.1, 0.2, 0.1, 0.1]
print(calculate_orthogonality(vector1,vector2))
print(calculate_orthogonality(vector1,vector3))
print(calculate_orthogonality(vector2,vector3))
0.0446635108744
0.0446635108744
0.028662025148
我特别希望 vector2 和 3 返回 0,即它们是相同的,因此是平行的。
这让我相信我在这里误解了一些东西。有什么想法吗?
附言我查看了其他常见的措施,例如 gini 杂质等,它们很好,但我发现这是一种替代方法,我正在尝试衡量它的有效性。
干杯
大卫
编辑:
找到以下http://masongallo.github.io/machine/learning,/python/2016/07/29/cosine-similarity.html
看来我的理解还差得很远。如果我使用这个实现,我会得到以下结果
import numpy as np
def cos_sim(a, b):
"""Takes 2 vectors a, b and returns the cosine similarity according
to the definition of the dot product
"""
dot_product = np.dot(a, b)
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
return dot_product / (norm_a * norm_b)
vector1 = [0.6666666666666666, 0.3333333333333333, 0, 0, 0, 0]
vector2 = [0.4, 0.1, 0.1, 0.2, 0.1, 0.1]
vector3 = [0.4, 0.1, 0.1, 0.2, 0.1, 0.1]
print(cos_sim(vector1,vector2))
print(cos_sim(vector1,vector3))
print(cos_sim(vector2,vector3))
0.821583836258
0.821583836258
1.0
向量 2 和 3 被高亮显示为相同。我需要更多地了解这个过程,但我认为这是正确的。
【问题讨论】:
-
是的,您不需要计算
cos。 cos 将两个向量的角度 转换为点积除以两个向量的长度的乘积。但是你在这里计算点积的 cos。这在语义上没有多大意义。 -
此外,您能指出正交性是 1-cos(theta) 的位置吗?
-
谢谢@Willem Van Onsem,这是有道理的。通常情况下,在提出问题后,我想我在这里偶然发现了答案:masongallo.github.io/machine/learning,/python/2016/07/29/… - 我会更新答案。对于您的第二条评论,我再次认为这是我对计算的误解
-
您可以回答自己的问题,这表明其他 SO 用户无需寻找解决方案。
标签: python vector decision-tree orthogonal