在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()附近找到了交叉點: