Home > Blockchain >  Python: How to compute multiple x intercept given two array?
Python: How to compute multiple x intercept given two array?

Time:01-07

I'm working on a project to visualize data but I've encountered an issue about finding multiple x intercept (maybe one, maybe at least two).

Given that

x = np.array([ 3. ,  3.1,  3.2,  3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,
        4.1,  4.2,  4.3,  4.4,  4.5,  4.6,  4.7,  4.8,  4.9,  5. ,  5.1,
        5.2,  5.3,  5.4,  5.5,  5.6,  5.7,  5.8,  5.9,  6. ,  6.1,  6.2,
        6.3,  6.4,  6.5,  6.6,  6.7,  6.8,  6.9,  7. ,  7.1,  7.2,  7.3,
        7.4,  7.5,  7.6,  7.7,  7.8,  7.9,  8. ,  8.1,  8.2,  8.3,  8.4,
        8.5,  8.6,  8.7,  8.8,  8.9,  9. ,  9.1,  9.2,  9.3,  9.4,  9.5,
        9.6,  9.7,  9.8,  9.9, 10. , 10.1, 10.2, 10.3, 10.4, 10.5, 10.6,
       10.7, 10.8, 10.9, 11. , 11.1, 11.2, 11.3, 11.4, 11.5, 11.6, 11.7,
       11.8, 11.9, 12. , 12.1, 12.2, 12.3, 12.4, 12.5, 12.6, 12.7, 12.8,
       12.9, 13. , 13.1, 13.2, 13.3, 13.4, 13.5, 13.6, 13.7, 13.8, 13.9,
       14. , 14.1, 14.2, 14.3, 14.4, 14.5, 14.6, 14.7, 14.8, 14.9, 15. ,
       15.1, 15.2, 15.3, 15.4, 15.5, 15.6, 15.7, 15.8, 15.9, 16. , 16.1,
       16.2, 16.3, 16.4, 16.5, 16.6, 16.7, 16.8, 16.9, 17. , 17.1, 17.2,
       17.3, 17.4, 17.5, 17.6, 17.7, 17.8, 17.9, 18. , 18.1, 18.2, 18.3,
       18.4, 18.5, 18.6, 18.7, 18.8, 18.9, 19. , 19.1, 19.2, 19.3, 19.4,
       19.5, 19.6, 19.7, 19.8, 19.9, 20. , 20.1, 20.2, 20.3, 20.4, 20.5,
       20.6, 20.7, 20.8, 20.9, 21. , 21.1, 21.2, 21.3, 21.4, 21.5, 21.6,
       21.7, 21.8, 21.9, 22. , 22.1, 22.2, 22.3, 22.4, 22.5, 22.6, 22.7,
       22.8, 22.9, 23. , 23.1, 23.2, 23.3, 23.4, 23.5, 23.6, 23.7, 23.8,
       23.9, 24. , 24.1, 24.2, 24.3, 24.4, 24.5, 24.6, 24.7, 24.8, 24.9,
       25. , 25.1, 25.2, 25.3, 25.4, 25.5, 25.6, 25.7, 25.8, 25.9, 26. ,
       26.1, 26.2, 26.3, 26.4, 26.5, 26.6, 26.7, 26.8, 26.9])

y = np.array([ 28250.,  27750.,  27250.,  26750.,  26250.,  25750.,  25250.,
        24750.,  24250.,  23750.,  23250.,  22750.,  22250.,  21750.,
        21250.,  20750.,  20250.,  19750.,  19250.,  18750.,  18250.,
        17750.,  17250.,  16750.,  16250.,  15750.,  15250.,  14750.,
        14250.,  13750.,  13250.,  12750.,  12250.,  11750.,  11250.,
        10750.,  10250.,   9750.,   9250.,   8750.,   8250.,   7750.,
         7250.,   6750.,   6250.,   5750.,   5250.,   4750.,   4250.,
         3750.,   3250.,   2750.,   2250.,   1750.,   1250.,    750.,
          250.,   -250.,   -750.,  -1250.,  -1750.,  -2250.,  -2750.,
        -3250.,  -3750.,  -4250.,  -4750.,  -5250.,  -5750.,  -6250.,
        -6750.,  -7250.,  -7750.,  -8250.,  -8750.,  -9250.,  -9750.,
       -10250., -10750., -11250., -11750., -12250., -12750., -13250.,
       -13750., -14250., -14750., -15250., -15750., -16250., -16750.,
       -17250., -17750., -18250., -18750., -19250., -19750., -20250.,
       -20750., -21250., -21750., -22250., -22750., -23250., -23750.,
       -24250., -24750., -25250., -25750., -26250., -26750., -27250.,
       -27750., -28250., -28750., -29250., -29750., -30250., -30750.,
       -31250., -31750., -31250., -30750., -30250., -29750., -29250.,
       -28750., -28250., -27750., -27250., -26750., -26250., -25750.,
       -25250., -24750., -24250., -23750., -23250., -22750., -22250.,
       -21750., -21250., -20750., -20250., -19750., -19250., -18750.,
       -18250., -17750., -17250., -16750., -16250., -15750., -15250.,
       -14750., -14250., -13750., -13250., -12750., -12250., -11750.,
       -11250., -10750., -10250.,  -9750.,  -9250.,  -8750.,  -8250.,
        -7750.,  -7250.,  -6750.,  -6250.,  -5750.,  -5250.,  -4750.,
        -4250.,  -3750.,  -3250.,  -2750.,  -2250.,  -1750.,  -1250.,
         -750.,   -250.,    250.,    750.,   1250.,   1750.,   2250.,
         2750.,   3250.,   3750.,   4250.,   4750.,   5250.,   5750.,
         6250.,   6750.,   7250.,   7750.,   8250.,   8750.,   9250.,
         9750.,  10250.,  10750.,  11250.,  11750.,  12250.,  12750.,
        13250.,  13750.,  14250.,  14750.,  15250.,  15750.,  16250.,
        16750.,  17250.,  17750.,  18250.,  18750.,  19250.,  19750.,
        20250.,  20750.,  21250.,  21750.,  22250.,  22750.,  23250.,
        23750.,  24250.,  24750.,  25250.,  25750.,  26250.,  26750.,
        27250.,  27750.])

The concept is finding the x value while the corresponding y value is 0, could you help me to figure it out? Thanks!

CodePudding user response:

Your data looks like this and you want to get the x-intercepts:

input

One simple option using only numpy is to check whenever y changes sign:

s = np.sign(y)  # if you want to check the intercept with y=n
                # use   s = np.sign(y-n)   instead
x[np.r_[s[:-1]!=s[1:], [False]]]

output:

array([ 8.6, 21.3])

NB. this is working well here as you have a nice density of the data. If this is not the case, you might want to get the point before and after the shift and to take the mean:

s = np.sign(y)
mask = s[:-1]!=s[1:]
np.c_[x[np.r_[mask, [False]]], # point before
      x[np.r_[[False], mask]], # point after
     ].mean(1)
# array([ 8.65, 21.35])

Visual output:

output

CodePudding user response:

Try np.where

coordinates_where_y_is_zero = np.where(y == 0)
print(coordinates_where_y_is_zero)
corresponding_x = x[coordinates_where_y_is_zero]
print(corresponding_x)

CodePudding user response:

Plot

We can clearly see that it is a linear absolute value function; there is an inflection point and there are 2 linear parts. We know that we can completely define a linear function by two points on it.

So using the inflection point and a point from each side (that we select using points one location removed from the inflection point) we can find intercept and slope of each portion and using them find x-intercepts.

def find_x_intercept(part):

    # since this is an absolute function, there must be an inflection point
    inflection_loc = np.argmin(y)
    # set up points
    pt1 = [x[inflection_loc], y[inflection_loc]]
    pt2 = [x[inflection_loc part], y[inflection_loc part]]

    denom, num = [i-j for i,j in zip(pt1, pt2)]

    # find slope, incercept of function
    slope = num / denom
    intercept = pt1[1] - slope * pt1[0]

    # return x_intercept
    return - intercept / slope

find_x_intercept(-1), find_x_intercept(1)

Output:

(8.650000000000023, 21.349999999999977)
  •  Tags:  
  • Related