我正在实现一个决策树算法,并尝试使用正交性来衡量分割的质量。我的理解是,我将正交性计算为:
1−cosθ(π,Pj)
其中i是分割前的数据分区,j是分割后的分区。Pi和Pj是每个分区中每个目标值的概率向量。你知道吗
我已经实现了以下内容,但我不确定我是否正确地解释了这一点。我有6节课,向量1在第一节课上占66%,在第二节课上占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和vector3返回0,也就是说,它们是相同的,因此是平行的。你知道吗
这让我觉得我误解了这里的一些东西。有什么想法吗?你知道吗
另外,我已经研究了其他常用的测量方法,比如基尼杂质等,它们很好,但我发现这是一种替代方法,我正在尝试测量它的有效性。你知道吗
干杯
大卫
编辑:
发现了下面的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高亮显示为相同。我需要更多地了解这个过程,但我认为这是正确的。你知道吗
抱歉耽搁了-答案确实是按照编辑使用代码
相关问题 更多 >
编程相关推荐