我执行了目前的最佳答案,但在所有情况下都没有给出正确的结果。我重新实现并测试了下面的实现。我还利用了内置的梯形AUC功能,而不是从头开始重建。def line(x_coords, y_coords):
"""
Given a pair of coordinates (x1,y2), (x2,y2), define the line equation. Note that this is the entire line vs. t
the line segment.
Parameters
----------
x_coords: Numpy array of 2 points corresponding to x1,x2
x_coords: Numpy array of 2 points corresponding to y1,y2
Returns
-------
(Gradient, intercept) tuple pair
"""
if (x_coords.shape[0] < 2) or (y_coords.shape[0] < 2):
raise ValueError('At least 2 points are needed to compute'
' area under curve, but x.shape = %s' % p1.shape)
if ((x_coords[0]-x_coords[1]) == 0):
raise ValueError("gradient is infinity")
gradient = (y_coords[0]-y_coords[1])/(x_coords[0]-x_coords[1])
intercept = y_coords[0] - gradient*1