Python 当我尝试使用curve_fit绘制对数回归曲线时,会收到运行时警告。我还得到了一个未找到的最佳参数

Python 当我尝试使用curve_fit绘制对数回归曲线时,会收到运行时警告。我还得到了一个未找到的最佳参数,python,numpy,parameters,curve-fitting,logarithm,Python,Numpy,Parameters,Curve Fitting,Logarithm,您好,我正在为这个数据做对数回归。每当我使用timereg数组来计算我得到的回归和错误时 “RuntimeWarning:日志中遇到无效值。” 然后给出了这个错误: '未找到最佳参数:函数调用数已达到maxfev=800。' 最糟糕的是,当我使用yearsreg数组代替timereg数组时,它工作得非常好 import matplotlib.pyplot as plt %matplotlib inline import pandas as pd import numpy as np from s

您好,我正在为这个数据做对数回归。每当我使用timereg数组来计算我得到的回归和错误时 “RuntimeWarning:日志中遇到无效值。” 然后给出了这个错误: '未找到最佳参数:函数调用数已达到maxfev=800。' 最糟糕的是,当我使用yearsreg数组代替timereg数组时,它工作得非常好

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit

这似乎是两种反常现象:

第一:它们是x的25个值,但y的值只有24个

第二:这是主要的异常现象,可能是故障的原因。方程式

y=a*log(b*x)+c

涉及三个参数,但它们不是独立的参数。方程式应仅使用两个独立参数编写:

y=a*log(x)+C

C=a*log(b)+C


同样地,您可以指定b=1,然后仅对a和c进行回归。

始终将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图,也不是指向外部门户的链接)进行讨论(不是注释)。还有其他有用的信息。可能首先使用
print()
查看变量中的值-可能这有助于您了解出现问题的时间和原因。
numevents = np.array([48561, 41991, 50973, 46383, 52007, 48875, 50936, 52956, 52409,
        53976, 59010, 71190, 57398, 62807, 79091, 64503, 59985, 59465,
        57788, 56003, 57025, 62693, 67652, 57001])

#### Creating a new array with the same number of values as there are years in the data in order to 
#### get a proper logarithmic curve from curve_fit.
yearsreg = np.arange(1, 25)
timereg = np.arange(1996, 2020)

#### Creating a new set of years that predicts into the future.
t2060 = np.arange(1, 46)


#### Creating a new array of the years to use as the xticks on the graph.
yearspredict = np.arange(1996, 2041)

#### Creating a Logarithmic Function
def logs(x, a, b, c):
    y = a * np.log(b * x) +c
    return y

#### Using this fucntion in curve_fit.
popt, pcov = curve_fit(logs, timereg, numevents, p0 = (1, 1, 50000))


#### Extracting the curve_fit parameter guesses.
a = popt[0]
b = popt[1]
c = popt[2]


# Finding the curve of the logarithmic regression and using it to predict into the future.
ev_logfit = logs(yearsreg, a, b, c)
w2060 = logs(t2060, a, b, c)