我已经找到了一些使用三角形区域来寻找直线的解决方案,但这不是我所需要的,因为在进一步的步骤中,并不能解决我的问题。
我一直在做一个嵌套循环,在这个循环中,我看到了点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],
}嵌套循环是一种选项,但也许是一种最简单的方法,可以通过矢量化来知道哪一条3是一行?
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。
等等..。这就是我尝试使用嵌套循环的原因。
发布于 2022-07-02 21:00:07
你可以这样做:
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解释:
diagonal
point1, point2, candidate;对于问题中的例子,这是(0,1,2),(0,1,3),(0,1,4),(0,2,3),(0,2,4),(0,3,4),(1,2,4),(1,2,4),(1,3,4),(2,3,4),(2,3,4)linex, liney, candx, candy, slope,该列是直线上某一点的x,y弦,候选点的弦,直线projy,它是候选人的x coord的投影y coord,以及表示candy是否在给定的公差范围内匹配projy的列inline,这意味着候选人确实与这两个点是共线的。中期结果:
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 Truehttps://stackoverflow.com/questions/72841776
复制相似问题