'How to measure approximate match between the numpy data and a pattern/model/function using Python?
I have some data points (x and y) and I want to check which of the following models matches the best with the data points:
y = x^2y = -x^2y = 0
I want to consider a window of size 35, and move the window, and verify in each step the data points are matching with which of the HEAD of above-mentioned models.
In other words, I intend to discover if each data point is most likely to continue a straight line; or the data point is going to continue an increasing-trend curve (it is a curve with minimum behind); or it is going to continue a decreasing-trend curve (it is a curve with maximum behind).
y = np.array([[1.6396475],
[1.6396625],
[1.639665 ],
[1.6395325],
[1.6396125],
[1.6394225],
[1.6389825],
[1.6388175],
[1.63901 ],
[1.6389325],
[1.63897 ],
[1.638955 ],
[1.638855 ],
[1.638905 ],
[1.639 ],
[1.639305 ],
[1.6393875],
[1.6393675],
[1.63916 ],
[1.63879 ],
[1.638545 ],
[1.6384525],
[1.638495 ],
[1.6385625],
[1.6383125],
[1.6382875],
[1.6384075],
[1.6382775],
[1.6382975],
[1.6383525],
[1.6387725],
[1.639285 ],
[1.6397525],
[1.6400825],
[1.6403925],
[1.64065 ],
[1.641025 ],
[1.6409375],
[1.6407275],
[1.64112 ],
[1.6416275],
[1.641705 ],
[1.6419825],
[1.642025 ],
[1.641535 ],
[1.6412825],
[1.641395 ],
[1.6413375],
[1.641145 ],
[1.6411725],
[1.6412975],
[1.641525 ],
[1.641715 ],
[1.6416025],
[1.64136 ],
[1.640975 ],
[1.6402425],
[1.64007 ],
[1.640575 ],
[1.6408675],
[1.6407075],
[1.6403825],
[1.6402525],
[1.64014 ],
[1.6400675],
[1.6397975],
[1.639635 ],
[1.63979 ],
[1.6397275],
[1.639685 ],
[1.63938 ],
[1.6392375],
[1.63906 ],
[1.6387775],
[1.638465 ],
[1.638495 ],
[1.6387 ],
[1.6384675],
[1.6382575],
[1.63833 ],
[1.6382725],
[1.63815 ],
[1.6381175],
[1.63801 ],
[1.637945 ],
[1.637915 ],
[1.637885 ],
[1.6381025],
[1.638095 ],
[1.637975 ],
[1.6378325],
[1.637945 ],
[1.6382925],
[1.63824 ],
[1.63781 ],
[1.6375275],
[1.6375875],
[1.63764 ],
[1.637715 ],
[1.637785 ]])
x = np.array([[ 0.],
[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.],
[ 6.],
[ 7.],
[ 8.],
[ 9.],
[10.],
[11.],
[12.],
[13.],
[14.],
[15.],
[16.],
[17.],
[18.],
[19.],
[20.],
[21.],
[22.],
[23.],
[24.],
[25.],
[26.],
[27.],
[28.],
[29.],
[30.],
[31.],
[32.],
[33.],
[34.],
[35.],
[36.],
[37.],
[38.],
[39.],
[40.],
[41.],
[42.],
[43.],
[44.],
[45.],
[46.],
[47.],
[48.],
[49.],
[50.],
[51.],
[52.],
[53.],
[54.],
[55.],
[56.],
[57.],
[58.],
[59.],
[60.],
[61.],
[62.],
[63.],
[64.],
[65.],
[66.],
[67.],
[68.],
[69.],
[70.],
[71.],
[72.],
[73.],
[74.],
[75.],
[76.],
[77.],
[78.],
[79.],
[80.],
[81.],
[82.],
[83.],
[84.],
[85.],
[86.],
[87.],
[88.],
[89.],
[90.],
[91.],
[92.],
[93.],
[94.],
[95.],
[96.],
[97.],
[98.],
[99.]])
Solution 1:[1]
It was a long-time this was a question in my mind. And I hope that others can provide better answers. Here, I am mentioning an answer, before I am attaching the image of the final answer:
import numpy as np
from scipy.interpolate import interp1d
from scipy.misc import derivative
import matplotlib.pyplot as plt
y = np.array([[1.6396475],
[1.6396625],
[1.639665 ],
[1.6395325],
[1.6396125],
[1.6394225],
[1.6389825],
[1.6388175],
[1.63901 ],
[1.6389325],
[1.63897 ],
[1.638955 ],
[1.638855 ],
[1.638905 ],
[1.639 ],
[1.639305 ],
[1.6393875],
[1.6393675],
[1.63916 ],
[1.63879 ],
[1.638545 ],
[1.6384525],
[1.638495 ],
[1.6385625],
[1.6383125],
[1.6382875],
[1.6384075],
[1.6382775],
[1.6382975],
[1.6383525],
[1.6387725],
[1.639285 ],
[1.6397525],
[1.6400825],
[1.6403925],
[1.64065 ],
[1.641025 ],
[1.6409375],
[1.6407275],
[1.64112 ],
[1.6416275],
[1.641705 ],
[1.6419825],
[1.642025 ],
[1.641535 ],
[1.6412825],
[1.641395 ],
[1.6413375],
[1.641145 ],
[1.6411725],
[1.6412975],
[1.641525 ],
[1.641715 ],
[1.6416025],
[1.64136 ],
[1.640975 ],
[1.6402425],
[1.64007 ],
[1.640575 ],
[1.6408675],
[1.6407075],
[1.6403825],
[1.6402525],
[1.64014 ],
[1.6400675],
[1.6397975],
[1.639635 ],
[1.63979 ],
[1.6397275],
[1.639685 ],
[1.63938 ],
[1.6392375],
[1.63906 ],
[1.6387775],
[1.638465 ],
[1.638495 ],
[1.6387 ],
[1.6384675],
[1.6382575],
[1.63833 ],
[1.6382725],
[1.63815 ],
[1.6381175],
[1.63801 ],
[1.637945 ],
[1.637915 ],
[1.637885 ],
[1.6381025],
[1.638095 ],
[1.637975 ],
[1.6378325],
[1.637945 ],
[1.6382925],
[1.63824 ],
[1.63781 ],
[1.6375275],
[1.6375875],
[1.63764 ],
[1.637715 ],
[1.637785 ]])
x = np.zeros((100,1))
for i in range(0, 100):
x[i,0] = i
x = np.array([[ 0.],
[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.],
[ 6.],
[ 7.],
[ 8.],
[ 9.],
[10.],
[11.],
[12.],
[13.],
[14.],
[15.],
[16.],
[17.],
[18.],
[19.],
[20.],
[21.],
[22.],
[23.],
[24.],
[25.],
[26.],
[27.],
[28.],
[29.],
[30.],
[31.],
[32.],
[33.],
[34.],
[35.],
[36.],
[37.],
[38.],
[39.],
[40.],
[41.],
[42.],
[43.],
[44.],
[45.],
[46.],
[47.],
[48.],
[49.],
[50.],
[51.],
[52.],
[53.],
[54.],
[55.],
[56.],
[57.],
[58.],
[59.],
[60.],
[61.],
[62.],
[63.],
[64.],
[65.],
[66.],
[67.],
[68.],
[69.],
[70.],
[71.],
[72.],
[73.],
[74.],
[75.],
[76.],
[77.],
[78.],
[79.],
[80.],
[81.],
[82.],
[83.],
[84.],
[85.],
[86.],
[87.],
[88.],
[89.],
[90.],
[91.],
[92.],
[93.],
[94.],
[95.],
[96.],
[97.],
[98.],
[99.]])
def Get_Trends(data=None):
k1 = 0
k2 = 35
KL = k2 - k1
x_list = []
y_list = []
for i in range(k1, k2):
cndl = data[i]
y_list.append(cndl)
x_list.append(i - k1)
x = np.array(x_list)
y = np.array(y_list)
f = interp1d(x, y, fill_value="extrapolate")
x_fake = np.arange(0, KL, 0.1)
df_dx = derivative(f, x_fake, dx=1e-6)
trend1 = np.average(df_dx)
k1 = 9
k2 = 19
KL = k2 - k1
x_list = []
y_list = []
for i in range(k1, k2):
cndl = data[i]
y_list.append(cndl)
x_list.append(i - k1)
x = np.array(x_list)
y = np.array(y_list)
f = interp1d(x, y, fill_value="extrapolate")
x_fake = np.arange(0, KL, 0.1)
df_dx = derivative(f, x_fake, dx=1e-6)
trend2 = np.average(df_dx)
k1 = 19
k2 = 27
KL = k2 - k1
x_list = []
y_list = []
for i in range(k1, k2):
cndl = data[i]
y_list.append(cndl)
x_list.append(i - k1)
x = np.array(x_list)
y = np.array(y_list)
f = interp1d(x, y, fill_value="extrapolate")
x_fake = np.arange(0, KL, 0.1)
df_dx = derivative(f, x_fake, dx=1e-6)
trend3 = np.average(df_dx)
k1 = 24
k2 = 30
KL = k2 - k1
x_list = []
y_list = []
for i in range(k1, k2):
cndl = data[i]
y_list.append(cndl)
x_list.append(i - k1)
x = np.array(x_list)
y = np.array(y_list)
f = interp1d(x, y, fill_value="extrapolate")
x_fake = np.arange(0, KL, 0.1)
df_dx = derivative(f, x_fake, dx=1e-6)
trend4 = np.average(df_dx)
k1 = 30
k2 = 35
KL = k2 - k1
x_list = []
y_list = []
for i in range(k1, k2):
cndl = data[i]
y_list.append(cndl)
x_list.append(i - k1)
x = np.array(x_list)
y = np.array(y_list)
f = interp1d(x, y, fill_value="extrapolate")
x_fake = np.arange(0, KL, 0.1)
df_dx = derivative(f, x_fake, dx=1e-6)
trend5 = np.average(df_dx)
return trend1, trend2, trend3, trend4, trend5
c = 0
tlist2 = []
trends_list = []
for i in range(35, 100):
trend1, trend2, trend3, trend4, trend5 = Get_Trends(y[i - 35:i, 0])
if trend1 >= -0.0015678 and (trend5 >= trend4 or trend5 >= trend3 or trend5 >= trend2) \
and (trend4 >= trend3 or trend4 >= trend2) and trend4 > 0 and trend5 > 0:
print(str(i+35) + " -X^2")
plt.plot(x[i], y[i],"^g")
elif trend1 <= 0.0015678 and (trend5 <= trend4 or trend5 <= trend3 or trend5 <= trend2) \
and (trend4 <= trend3 or trend4 <= trend2) and trend4 < 0 and trend5 < 0:
print(str(i+35) + " X^2")
plt.plot(x[i], y[i], ".r")
elif np.abs(trend1-trend2) <= 0.001 and np.abs(trend2-trend3) <= 0.001 and np.abs(trend3-trend4) <= 0.001 and np.abs(trend4-trend5) <= 0.001 and np.abs(trend1-trend5) <= 0.001:
print(str(i+35) + " y= a")
plt.plot(x[i], y[i],".b")
else:
print(str(i+35) + " No model")
plt.plot(x[i], y[i], ".y")
plt.plot(x,y)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Farzad Amirjavid |


