python如何知道哪个3点是一条线

发布于 2025-02-12 15:29:50 字数 1301 浏览 0 评论 0原文

我找到了一些使用三角形区域的解决方案来查找线条,但这不是我需要的,因为在进一步的步骤中,这不是解决我的问题的解决方案。

我一直在嵌套循环进行工作,在那里我看到点0和1之间的斜率,如果价格相同,我找到了一个线,就会给我下一个点的预测(每个点的价格)。

现在,我是第一个循环回到我的斜坡,但是我正在为第二个循环而努力以获得EXP_PRICE比较...关于如何简单地找到此行的任何想法?


initial_data = {'index': [4, 17, 24, 36, 42],
    'High': [805.000000, 1094.939941, 1243.489990, 1201.949951, 1172.839966],
}
test = pd.DataFrame(initial_data)

slope_data = {'index': [4, 17, 24, 36, 42],
    'High': [805.000000, 1094.939941, 1243.489990, 1518.7, 1172.839966],
    'Slope': [0, 22.30307, 21.22144, 22.93417, -57.64334],
}

嵌套循环是一种选择,但也许是最简单的换行方法,即知道哪个是一条线?

for i in range(len(test)):
    #test.loc[:,'slope'] = (test.loc[i,'High'] - test.loc[i,'High'])   / (test.loc[i,'index'] - test.loc[i,'index'])
    test['slope'] = round((test['High']-test['High'].shift(1)) / (test['index']-test['index'].shift(1)),8)
    print(test)

    for j in range(len(test)):
         exp_price = (test['High'].shift(1) + (test['slope']*(test['index']-test['index'].shift(j))))
         #If exp_price of j equals High of i  (+- 0,5% tolerance) I've found a line!

         # print(values)

    test['result'] = values
    print(test)

请注意,我想将0,1与2、3和4。0、2

与3、4。0、3

4.1、2与3、4。

等等相提并论。这就是为什么我尝试使用嵌套循环。

I've found some solutions using the triangle area to find lines but It's not what I need because in further steps will not be a solution for my problem.

I've been working on a nested loop where I see the slope between point 0 and 1 and gives me a projection for next points (expexcted price for each point) If the price is the same +- tolerance I have found a line.

Now I've the first loop returning me Slopes but I'm struggling with the second loop to get the exp_price comparision... Any idea on how to find this lines simple?


initial_data = {'index': [4, 17, 24, 36, 42],
    'High': [805.000000, 1094.939941, 1243.489990, 1201.949951, 1172.839966],
}
test = pd.DataFrame(initial_data)

slope_data = {'index': [4, 17, 24, 36, 42],
    'High': [805.000000, 1094.939941, 1243.489990, 1518.7, 1172.839966],
    'Slope': [0, 22.30307, 21.22144, 22.93417, -57.64334],
}

The nested loop is an option, but maybe is an easiest way vecotrizing to know which 3 are a line?

for i in range(len(test)):
    #test.loc[:,'slope'] = (test.loc[i,'High'] - test.loc[i,'High'])   / (test.loc[i,'index'] - test.loc[i,'index'])
    test['slope'] = round((test['High']-test['High'].shift(1)) / (test['index']-test['index'].shift(1)),8)
    print(test)

    for j in range(len(test)):
         exp_price = (test['High'].shift(1) + (test['slope']*(test['index']-test['index'].shift(j))))
         #If exp_price of j equals High of i  (+- 0,5% tolerance) I've found a line!

         # print(values)

    test['result'] = values
    print(test)

Note I want to compare 0,1 with 2, 3 and 4.

0, 2 with 3, 4.

0, 3 with 4.

1, 2 with 3, 4.

and so on... That's why I try to use the nested loop.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2025-02-19 15:29:50

您可以执行此操作:

coords = list(zip(test['index'], test.High))
slope = pd.DataFrame([[(coly - rowy)/(colx - rowx) if colx > rowx else None for colx, coly in coords] for rowx, rowy in coords], columns=coords, index=coords)
pointsAndCandidate = [(i, j, k) for i in range(len(test.index) - 2) for j in range(i + 1, len(test.index) - 1) for k in range(j + 1, len(test.index))]
df3 = pd.DataFrame(columns=[
    'linex','liney','candx','candy', 'slope'], index = pointsAndCandidate, data = [
    (*coords[row[1]], *coords[row[2]], slope.iloc[
    row[0], row[1]]) for row in pointsAndCandidate])
df3['projy'] = df3.liney + df3.slope * (df3.candx - df3.linex)
df3['inline'] = (df3.candy / df3.projy - 1).abs() < 0.01
print(df3)

输出:

           linex        liney  candx        candy      slope        projy  inline
(0, 1, 2)     17  1094.939941     24  1243.489990  22.303072  1251.061448    True
(0, 1, 3)     17  1094.939941     36  1201.949951  22.303072  1518.698316   False
(0, 1, 4)     17  1094.939941     42  1172.839966  22.303072  1652.516751   False
(0, 2, 3)     24  1243.489990     36  1201.949951  21.924500  1506.583984   False
(0, 2, 4)     24  1243.489990     42  1172.839966  21.924500  1638.130981   False
(0, 3, 4)     36  1201.949951     42  1172.839966  12.404686  1276.378067   False
(1, 2, 3)     24  1243.489990     36  1201.949951  21.221436  1498.147217   False
(1, 2, 4)     24  1243.489990     42  1172.839966  21.221436  1625.475830   False
(1, 3, 4)     36  1201.949951     42  1172.839966   5.632106  1235.742586   False
(2, 3, 4)     36  1201.949951     42  1172.839966  -3.461670  1181.179932    True

说明:

  • 创建一个包含对角上的斜率斜率的数据框架
  • 创建所有感兴趣的索引组合的列表:point1,point2,cant2,cantute;对于问题中的示例,这是(0,1,2),(0,1,3),(0,1,4),(0,2,3),(0,2,4),( 0,3,4),(1,2,3),(1,2,4),(1,3,4),(2,3,4)
  • 创建一个带有列的数据框candx,糖果,坡度是线上点之一的X,Y坐标,候选点的坐标,以及线的斜率
  • 添加到其列Projy,这是候选人的预计y坐标X COORD,以及列inline,指示candy在给定的公差内是否匹配projy,这意味着候选人确实是与两个点联合。

中间结果:

test
   index         High
0      4   805.000000
1     17  1094.939941
2     24  1243.489990
3     36  1201.949951
4     42  1172.839966
coords
[(4, 805.0), (17, 1094.939941), (24, 1243.48999), (36, 1201.949951), (42, 1172.839966)]
slope
                  (4, 805.0)  (17, 1094.939941)  (24, 1243.48999)  (36, 1201.949951)  (42, 1172.839966)
(4, 805.0)              None          22.303072         21.924500          12.404686           9.679999
(17, 1094.939941)       None                NaN         21.221436           5.632106           3.116001
(24, 1243.48999)        None                NaN               NaN          -3.461670          -3.925001
(36, 1201.949951)       None                NaN               NaN                NaN          -4.851664
(42, 1172.839966)       None                NaN               NaN                NaN                NaN
pointsAndCandidate
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
result
           linex        liney  candx        candy      slope        projy  inline
(0, 1, 2)     17  1094.939941     24  1243.489990  22.303072  1251.061448    True
(0, 1, 3)     17  1094.939941     36  1201.949951  22.303072  1518.698316   False
(0, 1, 4)     17  1094.939941     42  1172.839966  22.303072  1652.516751   False
(0, 2, 3)     24  1243.489990     36  1201.949951  21.924500  1506.583984   False
(0, 2, 4)     24  1243.489990     42  1172.839966  21.924500  1638.130981   False
(0, 3, 4)     36  1201.949951     42  1172.839966  12.404686  1276.378067   False
(1, 2, 3)     24  1243.489990     36  1201.949951  21.221436  1498.147217   False
(1, 2, 4)     24  1243.489990     42  1172.839966  21.221436  1625.475830   False
(1, 3, 4)     36  1201.949951     42  1172.839966   5.632106  1235.742586   False
(2, 3, 4)     36  1201.949951     42  1172.839966  -3.461670  1181.179932    True

You can do this:

coords = list(zip(test['index'], test.High))
slope = pd.DataFrame([[(coly - rowy)/(colx - rowx) if colx > rowx else None for colx, coly in coords] for rowx, rowy in coords], columns=coords, index=coords)
pointsAndCandidate = [(i, j, k) for i in range(len(test.index) - 2) for j in range(i + 1, len(test.index) - 1) for k in range(j + 1, len(test.index))]
df3 = pd.DataFrame(columns=[
    'linex','liney','candx','candy', 'slope'], index = pointsAndCandidate, data = [
    (*coords[row[1]], *coords[row[2]], slope.iloc[
    row[0], row[1]]) for row in pointsAndCandidate])
df3['projy'] = df3.liney + df3.slope * (df3.candx - df3.linex)
df3['inline'] = (df3.candy / df3.projy - 1).abs() < 0.01
print(df3)

Output:

           linex        liney  candx        candy      slope        projy  inline
(0, 1, 2)     17  1094.939941     24  1243.489990  22.303072  1251.061448    True
(0, 1, 3)     17  1094.939941     36  1201.949951  22.303072  1518.698316   False
(0, 1, 4)     17  1094.939941     42  1172.839966  22.303072  1652.516751   False
(0, 2, 3)     24  1243.489990     36  1201.949951  21.924500  1506.583984   False
(0, 2, 4)     24  1243.489990     42  1172.839966  21.924500  1638.130981   False
(0, 3, 4)     36  1201.949951     42  1172.839966  12.404686  1276.378067   False
(1, 2, 3)     24  1243.489990     36  1201.949951  21.221436  1498.147217   False
(1, 2, 4)     24  1243.489990     42  1172.839966  21.221436  1625.475830   False
(1, 3, 4)     36  1201.949951     42  1172.839966   5.632106  1235.742586   False
(2, 3, 4)     36  1201.949951     42  1172.839966  -3.461670  1181.179932    True

Explanation:

  • Create a dataframe containing the slopes of pairs of interest in the upper diagonal
  • Create a list of all index combinations of interest: point1, point2, candidate; for the example in the question, this is (0,1,2), (0,1,3), (0,1,4), (0,2,3), (0,2,4), (0,3,4), (1,2,3), (1,2,4), (1,3,4), (2,3,4)
  • Create a dataframe with columns linex, liney, candx, candy, slope which are the x, y coords of one of the points on the line, the coords of the candidate point, and the slope of the line
  • Add to it the column projy which is the projected y coord for the candidate's x coord, and also the column inline which indicates whether candy matches projy within a given tolerance, meaning the candidate is indeed collinear with the two points.

Intermediate results:

test
   index         High
0      4   805.000000
1     17  1094.939941
2     24  1243.489990
3     36  1201.949951
4     42  1172.839966
coords
[(4, 805.0), (17, 1094.939941), (24, 1243.48999), (36, 1201.949951), (42, 1172.839966)]
slope
                  (4, 805.0)  (17, 1094.939941)  (24, 1243.48999)  (36, 1201.949951)  (42, 1172.839966)
(4, 805.0)              None          22.303072         21.924500          12.404686           9.679999
(17, 1094.939941)       None                NaN         21.221436           5.632106           3.116001
(24, 1243.48999)        None                NaN               NaN          -3.461670          -3.925001
(36, 1201.949951)       None                NaN               NaN                NaN          -4.851664
(42, 1172.839966)       None                NaN               NaN                NaN                NaN
pointsAndCandidate
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
result
           linex        liney  candx        candy      slope        projy  inline
(0, 1, 2)     17  1094.939941     24  1243.489990  22.303072  1251.061448    True
(0, 1, 3)     17  1094.939941     36  1201.949951  22.303072  1518.698316   False
(0, 1, 4)     17  1094.939941     42  1172.839966  22.303072  1652.516751   False
(0, 2, 3)     24  1243.489990     36  1201.949951  21.924500  1506.583984   False
(0, 2, 4)     24  1243.489990     42  1172.839966  21.924500  1638.130981   False
(0, 3, 4)     36  1201.949951     42  1172.839966  12.404686  1276.378067   False
(1, 2, 3)     24  1243.489990     36  1201.949951  21.221436  1498.147217   False
(1, 2, 4)     24  1243.489990     42  1172.839966  21.221436  1625.475830   False
(1, 3, 4)     36  1201.949951     42  1172.839966   5.632106  1235.742586   False
(2, 3, 4)     36  1201.949951     42  1172.839966  -3.461670  1181.179932    True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文