0

I'm trying to determine the intersection of two lines.

Blue line is my y variable calculated by df['Amount']/df['SomeNumber'].

Green line is created from 2 x_coords and 2 y_coords (coordinates), has a slope of 115.38461538461503 and intercept of -74.076923076922739.

>>> x_coords
[0.84999999999999998, 0.97999999999999998]
>>> y_coords
[24, 39]

Suggestions scipy.optimize, fsolve, or numpy's polyfit but I have been unsuccessful thus far.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({'SomeNumber': [0.85, 0.98, 1.06, 1.1, 1.13, 1.2, 1.22, 1.23, 1.31, 1.43],
                   'Events': [24, 39, 20, 28, 20, 24, 26, 29, 30, 24],
                   'Amount': [35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78]},
                  columns=['Amount', 'Events', 'SomeNumber'])

df = df.sort('SomeNumber')

x = df['SomeNumber']
y = df['Amount']/df['SomeNumber']

df_below = df[df['Events'] < y]
df_above = df[df['Events'] >= y]


x_coords = [df_below['SomeNumber'].min(), df_above['SomeNumber'].min()]
y_coords = [df_below.ix[df_below['SomeNumber'].idxmin(), 'Events'],
            df_above.ix[df_above['SomeNumber'].idxmin(), 'Events']]

slope, intercept = np.polyfit(x_coords, y_coords, 1)
#>>> slope, intercept == (115.38461538461503, -74.076923076922739)

plt.plot(x, y, label='Potential Events')
plt.scatter(x, df['Events'], label='Actual Events')
plt.plot(x_coords, y_coords)
plt.xlabel('Some Number')
plt.ylabel('Events')
plt.legend(loc='upper right')
plt.show()

enter image description here

Jarad
  • 17,409
  • 19
  • 95
  • 154
  • [This](http://stackoverflow.com/questions/29904423/how-to-find-the-points-of-intersection-of-a-line-and-multiple-curves-in-python) might be helpful. – Mahdi Dec 17 '15 at 21:26

1 Answers1

4

You could approximate the curves as piecewise polynomials:

p1 = interpolate.PiecewisePolynomial(x1, y1[:, np.newaxis])
p2 = interpolate.PiecewisePolynomial(x2, y2[:, np.newaxis])

p1 and p2 are functions of x. You can then use scipy.optimize.fsolve to find x values where p1(x) equals p2(x).


import pandas as pd
import numpy as np
from scipy import optimize
from scipy import interpolate
import matplotlib.pyplot as plt

def find_intersections(x1, y1, x2, y2):
    x1 = np.asarray(x1)
    y1 = np.asarray(y1)
    x2 = np.asarray(x2)
    y2 = np.asarray(y2)
    p1 = interpolate.PiecewisePolynomial(x1, y1[:, np.newaxis])
    p2 = interpolate.PiecewisePolynomial(x2, y2[:, np.newaxis])

    def pdiff(x):
        return p1(x) - p2(x)

    xs = np.r_[x1, x2]
    xs.sort()
    x_min = xs.min()
    x_max = xs.max()
    x_mid = xs[:-1] + np.diff(xs) / 2
    roots = set()
    for x_guess in x_mid:
        root, infodict, ier, mesg = optimize.fsolve(
            pdiff, x_guess, full_output=True)
        # ier==1 indicates a root has been found
        if ier == 1 and x_min < root < x_max:
            roots.add(root[0])
    x_roots = np.array(list(roots))
    y_roots = p1(x_roots)
    return x_roots, y_roots


df = pd.DataFrame({
    'SomeNumber': [0.85, 0.98, 1.06, 1.1, 1.13, 1.2, 1.22, 1.23, 1.31, 1.43],
    'Events': [24, 39, 20, 28, 20, 24, 26, 29, 30, 24],
    'Amount': [35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78, 35.78]},
                  columns=['Amount', 'Events', 'SomeNumber'])

df = df.sort('SomeNumber')

x = df['SomeNumber']
y = df['Amount']/df['SomeNumber']

df_below = df[df['Events'] < y]
df_above = df[df['Events'] >= y]

x_coords = [df_below['SomeNumber'].min(), df_above['SomeNumber'].min()]
y_coords = [df_below.ix[df_below['SomeNumber'].idxmin(), 'Events'],
            df_above.ix[df_above['SomeNumber'].idxmin(), 'Events']]

x_roots, y_roots = find_intersections(x, y, x_coords, y_coords)

plt.plot(x, y, label='Potential Events')
plt.scatter(x, df['Events'], label='Actual Events')
plt.plot(x_coords, y_coords)
plt.scatter(x_roots, y_roots, s=50, c='red')
plt.xlabel('Some Number')
plt.ylabel('Events')
plt.legend(loc='upper right')
plt.show()

enter image description here


The intersection was found near (0.96, 37.19):

In [218]: x_roots
Out[218]: array([0.9642754164139411])

In [219]: y_roots
Out[219]: array([ 37.18562497])
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Came across [another example](http://stackoverflow.com/questions/19215335/finding-the-intersection-of-a-curve-from-polyfit) that's similar (for what it's worth). I initially found this approach slow due to a "large" number of X values so I limited the blue line range from zero to the x value of the x, y coordinate above the blue line. I'm so grateful for your help. Thank you. – Jarad Dec 18 '15 at 15:38