I don't understand why I can't post my question on Stack Overflow, but I can post it here.
The text is exactly the same, but when I want to post it on Stack Overflow I can't do it because a message tells me that my code is not formatted properly. Yet it is the same text word for word (which I attach below). Do you know where the problem may come?
The following is exactly the post I would like to publish on Stack Overflow:
I am trying to build a quadratic model to study the evolution of the world population. I would like to display the evolution of the world population until 2100 using the quadratic model.
I took my data from this site: Worldometers. I downloaded the page and read it with Pandas.
Here is my code:
# Import of modules import pandas as pd import numpy as np import matplotlib.pyplot as plt from types import SimpleNamespace # Reading the file Data = 'World Population by Year - Worldometer.html' Data2 = pd.read_html(Data, header = 0, index_col = 0, decimal = 'M') Data3 = Data2[0] # We extract a DataFrame from the file
Given the year jumps and the fact that the data may not have been reliable prior to 1951, I removed all data prior to 1951 to work only on data from 1951 to 2020.
# Delete the years before 1951 data = Data3.drop(Data.index[70:], axis = 0) # Rename the names of the columns data.index.names = ['Year'] # Rename the index data.columns = ['Population', 'Annual change', 'Net change', 'Density (P/Km²)', 'Urban population', 'Urban population (%)'] # Renaming of other columns population = data['Population'] total_growth = population[2020] - population[1951] start = population.index[-1] # 1st year of data collected current = population.index[0] # Last year of data collected duration = current - start # Duration of the data collection period start_population = population[start] population_2020 = population[current] difference_population = population_2020 - start_population annual_growth = total_growth / duration parameters = SimpleNamespace(start = start, end = current, start_population = start_population, annual_growth = annual_growth) def model(parameters, growth_function): result = dict() result[parameters.start] = parameters.start_population for t in range(parameters.start, parameters.end): evolution = growth_function(t, result[t], parameters) result[t + 1] = result[t] + evolution # We convert the dictionary into a DataFrame in order to plot it result_items = result.items() # To obtain an object of type set with the keys and the values of the dictionary result_list = list(result_items) # We then convert the set object into a list results = pd.DataFrame(result_list) # Finally we create a >DataFrame from a list results.columns = ['Year', 'World population'] # Name of the >columns of the DataFrame return results parameters.alpha = parameters.birth_rate - parameters.death_rate def quadratic(t, population, parameters): """Returns the quadratic function""" return parameters.alpha * population + parameters.beta * population ** 2 Parameters = SimpleNamespace(start = start, start_population = start_population, alpha = 25 / 1000, beta = -1.8 / 1000, end = 2100) projection = model(Parameters, quadratic) projection
Here is the result I get by running this code:
Year World Population 0 1951 2.584034e+09 1 1952 -1.201902e+16 2 1953 -2.600222e+29 3 1954 -1.217008e+56 4 1955 -2.665994e+109 ... ... ... 145 2096 -inf 146 2097 -inf 147 2098 -inf 148 2099 -inf 149 2100 -inf As you can see, I get totally inconsistent results from the first year calculated (i.e. 1952) and I don't know where this dysfunction can come from. Moreover, I get a
RuntimeWarning: overflow encountered in double_scalars
warning when I try to display the results of the simulation even if this does not prevent the result from being displayed.No matter how hard I look, I can't figure out what's wrong with my code and my initial values. Where do you think these two problems come from?
>
>
highlight the post I originally intended to publish on Stack Overflow. I added them last to distinguish this post from the post I wanted to publish on Stack Overflow. In any case even without>
in front of the paragraphs I could publish my post hereRuntimeWarning: overflow encountered in double_scalars
, including the quotes and see if the error persists? You do have a blank line everywhere between paragraphs and tables and codeblocks, right?-1.201902e+16
(and possibly-inf
) will likely be seen as code, @Donny; hence why I suggested not putting them as floating point numbers. You could, instead, put them in inline code markdown; that might work.