【发布时间】:2017-02-15 03:49:51
【问题描述】:
我有 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 上发布了完整的数组here(它对应于下面代码中的dataPoints 列表)。
现在,我最初需要找到这条L-shape 曲线的elbow point,感谢this post。
现在,在下图中,红色矢量p 表示肘点。我想在向量b 上找到点x=(?,?)(黄色星),它对应于p 到b 的正交投影。
情节上的红点是我得到的(这显然是错误的)。我通过以下方式获得它:
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,因此@987654343 @?
我这里是不是搞错了?
编辑:回答@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')。如果坐标轴的比例不同,则线之间的角度会在图中扭曲。 -
哇,我觉得这就是诀窍……让我快点试试
-
@WarrenWeckesser 好吧,这就是事情,我觉得很愚蠢。非常感谢您指出这一点,您能否将其写为答案以便我接受?
-
好的,答案已提交。
标签: python numpy plot linear-algebra orthogonal