Лучший способ найти это - использовать scipy.optimize.curve_fit
. Ниже приведен пример кода, использующий данные примера выше:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
# Define lognormal PDF as function of x, mu, and sigma
def lognorm_fit(x, mu, sigma):
y = (1/(sigma*x*np.sqrt(2*np.pi))) * np.exp(-1* ((np.log(x) - mu)**2/(2*sigma**2)))
return y
# Open data
df = pd.read_csv('./data.csv')
# Read data, using the lower bin limit as x
count = df['count']
x = df['lower_bin_limit']
width = df['upper_bin_limit'] - df['lower_bin_limit']
# Divide by bin width and normalize by total count
y = count/width/(count.sum())
plt.plot(x,y)
# Use curve_fit to find the two parameters in lognom_fit function
# Curve fit returns a tuple of (mu, sigma) and the covariance,
# Which isn't needed.
(mu,sigma), _ = curve_fit(lognorm_fit, xdata=x, ydata=y)
print(mu, sigma)
# Generate lognorm PDF using values of mu, sigma
yy = lognorm_fit(x, mu, sigma)
# Plot Results
plt.plot(x,y)
plt.plot(x, yy)
plt.xscale('log')
plt.show()