[Python] Why does my logistic curve_fit prediction return huge values although R² ≈ 0.99?

Discussão em 'Python' iniciado por Stack, Agosto 27, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to fit a logistic function to GDP data (China GDP dataset) using scipy.optimize.curve_fit. The fitting looks very good (R² ≈ 0.99, curve matches the data), but when I try to predict a single value for a given year, the result is extremely large (≈ 1e24) while the real value is only ≈ 9e10.

    Here is a simplified version of my code:

    import numpy as np
    import pandas as pd
    from scipy.optimize import curve_fit
    from scipy.special import expit

    df = pd.read_csv("china_gdp.csv")
    msk = np.random.rand(len(df)) < 0.8
    x = df['Year'].values.astype(float)
    y = df['Value'].values.astype(float)

    # normalize x, y to [0,1]
    x_s = (x - x.min()) / (x.max() - x.min())
    y_s = (y - y.min()) / (y.max() - y.min())

    def logistic(x, B1, B2, B3, B4):
    return B3 + B4 * expit(B1*(x - B2))

    # initial guess and bounds
    p0 = (5.0, 0.5, 0.0, 1.0)
    bounds = ([0.0, 0.0, -np.inf, 0.0], [np.inf, 1.0, np.inf, np.inf])

    popt, pcov = curve_fit(logistic, x_s, y_s, p0=p0, bounds=bounds)
    print(dict(zip(['B1','B2','B3','B4'], popt)))

    # check fit
    import matplotlib.pyplot as plt
    xx = np.linspace(0, 1, 300)
    yy = logistic(xx, *popt)
    plt.scatter(x_s, y_s, s=15, label='data')
    plt.plot(xx, yy, label='fit')
    plt.legend(); plt.show()

    from sklearn.metrics import r2_score
    xtest = x_s[~msk]
    ytest = y_s[~msk]
    ypred = logistic(xtest, *popt)
    print(r2_score(ytest, ypred))

    x_in = 1970
    rs = logistic((x_in - x.min()) / (x.max() - x.min()), *popt)
    r = rs * (y.max() - y.min()) + y.min()
    print(r)


    Output:

    0.9985891574981185
    192285528141.3661


    But the real GDP value for 1970 is 91506211306.3745.

    Am I mis-scaling my input/output, or is there an issue with how I define the logistic function / initial parameters?

    I expected the prediction to be close to the real GDP values since the fit looks good and the R² score is high. I suspect I'm doing something wrong with scaling or with the logistic function parameters.

    Dataset link

    Continue reading...

Compartilhe esta Página