0
Question related to Logistic Model
I try to use the Logistic Model to predict the world population, but the code doesn’t work. The graph appears to be a straight line instead of a curve. Can someone help me? [Edit] Clue: Maybe it’s related with “OptimizeWarning: Covariance of the parameters could not be estimated” You can download the CSV file from: https://population.un.org/wpp/Download/Files/1_Indicators%20(Standard)/CSV_FILES/WPP2022_TotalPopulationBySex.zip https://code.sololearn.com/cH6vlJITYYYH/?ref=app
2 Respuestas
0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Read the CSV file and filter the data
data = pd.read_csv("WPP2022_TotalPopulationBySex.csv")
data = data.loc[(data["Location"] == "World") & (data["Variant"] == "Medium") & (data["Time"] <= 2022)]
data = data[["Time", "PopTotal"]].set_index("Time")
# Define the logistic function
def logis(t, E, r, x0):
return E / (1 + (E / x0) * np.exp(-r * (t - 1949)))
0
# Get initial parameter estimates
E_guess = data["PopTotal"].max()
r_guess = 0.03
x0_guess = data.index[0]
params_guess = (E_guess, r_guess, x0_guess)
# Perform curve fitting
params, pcov = curve_fit(logis, data.index, data["PopTotal"], p0=params_guess)
# Extract the optimized parameters
E_opt, r_opt, x0_opt = params
# Generate predictions using the optimized parameters
predictions = logis(data.index, E_opt, r_opt, x0_opt)
# Plot the data and the fitted curve
plt.plot(data.index, data["PopTotal"], label="Actual Data")
plt.plot(data.index, predictions, label="Fitted Curve")
plt.xlabel("Year")
plt.ylabel("Population")
plt.legend()
plt.title("World Population Prediction")
plt.show()