我有350个文档分数,当我将它们绘制出来时,形状如下:
docScores = [(0, 68.62998962), (1, 60.21374512), (2, 54.72480392),
(3, 50.71389389), (4, 49.39723969), ...,
(345, 28.3756237), (346, 28.37126923),
(347, 28.36397934), (348, 28.35762787), (349, 28.34219933)]
我在pastebin
上发布了完整的数组,链接在这里(它对应于以下代码中的dataPoints
列表)。
现在,我最初需要找到这个L-shape
曲线的拐点
,感谢这篇文章,我找到了。
在下面的图中,红色向量p
表示弯曲点。 我想要找到向量b
上对应于p
正交投影的点x=(?,?)
(即黄色星形)。
图中的红点是我得到的点(显然是错误的)。 我通过以下方式获得它:
b_hat = b / np.linalg.norm(b) #unit vector of b
proj_p_onto_b = p.dot(b_hat)*b_hat
red_point = proj_p_onto_b + s
现在,如果将
p
在b
上的投影定义为它的起点和终点,即s
和x
(黄色星形),则有proj_p_onto_b = x - s
,因此x = proj_p_onto_b + s
?这里有错误吗?编辑:回答@cxw,下面是计算拐点的代码:
def findElbowPoint(self, rawDocScores):
dataPoints = zip(range(0, len(rawDocScores)), rawDocScores)
s = np.array(dataPoints[0])
l = np.array(dataPoints[len(dataPoints)-1])
b_vect = l-s
b_hat = b_vect/np.linalg.norm(b_vect)
distances = []
for scoreVec in dataPoints[1:]:
p = np.array(scoreVec) - s
proj = p.dot(b_hat)*b_hat
d = abs(np.linalg.norm(p - proj)) # orthgonal distance between b and the L-curve
distances.append((scoreVec[0], scoreVec[1], proj, d))
elbow_x = max(distances, key=itemgetter(3))[0]
elbow_y = max(distances, key=itemgetter(3))[1]
proj = max(distances, key=itemgetter(3))[2]
max_distance = max(distances, key=itemgetter(3))[3]
red_point = proj + s
编辑:这是图形的代码:
>>> l_curve_x_values = [x[0] for x in docScores]
>>> l_curve_y_values = [x[1] for x in docScores]
>>> b_line_x_values = [x[0] for x in docScores]
>>> b_line_y_values = np.linspace(s[1], l[1], len(docScores))
>>> p_line_x_values = l_curve_x_values[:elbow_x]
>>> p_line_y_values = np.linspace(s[1], elbow_y, elbow_x)
>>> plt.plot(l_curve_x_values, l_curve_y_values, b_line_x_values, b_line_y_values, p_line_x_values, p_line_y_values)
>>> red_point = proj + s
>>> plt.plot(red_point[0], red_point[1], 'ro')
>>> plt.show()
plt.axis('equal')
。如果轴没有相等的比例尺,则图中线之间的角度会发生扭曲。 - Warren Weckesser