Quantcast
Channel: Active questions tagged code-formatting - Meta Stack Overflow
Viewing all articles
Browse latest Browse all 189

Why can I publish a post here, but I get an error when I try to post the same text on Stack Overflow? [duplicate]

$
0
0

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 modulesimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltfrom types import SimpleNamespace# Reading the fileData = '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 1951data = Data3.drop(Data.index[70:], axis = 0)# Rename the names of the columnsdata.index.names = ['Year'] # Rename the indexdata.columns = ['Population', 'Annual change', 'Net change', 'Density (P/Km²)', 'Urban population', 'Urban population (%)'] # Renaming of other columnspopulation = data['Population']total_growth = population[2020] - population[1951]start = population.index[-1] # 1st year of data collectedcurrent = population.index[0] # Last year of data collectedduration = current - start # Duration of the data collection periodstart_population = population[start]population_2020 = population[current]difference_population = population_2020 - start_populationannual_growth = total_growth / durationparameters = 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 resultsparameters.alpha = parameters.birth_rate - parameters.death_ratedef quadratic(t, population, parameters):"""Returns the quadratic function"""    return parameters.alpha * population + parameters.beta * population ** 2Parameters = 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:

YearWorld Population
019512.584034e+09
11952-1.201902e+16
21953-2.600222e+29
31954-1.217008e+56
41955-2.665994e+109
.........
1452096-inf
1462097-inf
1472098-inf
1482099-inf
1492100-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?


Viewing all articles
Browse latest Browse all 189

Trending Articles