module special.elections

Inheritance diagram of ensae_teaching_cs.special.elections

Short summary

module ensae_teaching_cs.special.elections

Contains a class to process elections results (France)

source on GitHub

Classes

class truncated documentation
ElectionResults processes data coming from data.gouv.fr. …

Properties

property truncated documentation
Level returns the level (département or canton)
LevelCol returns the column associated to the level (their name depends on the level)
T0 returns the dataframe for the first round
T1 returns the dataframe for the second round
WeightsNorm return the proportion of voters for each regions
Year returns the year

Static Methods

staticmethod truncated documentation
combine_into_string combine two matrices into one before displaying it
min_max_mean_std returns the mean standard deviation, bounds of the confidence interval

Methods

method truncated documentation
__init__ loads the data downloaded from data.gouv.fr. …
__str__ usual
bootstrap uses the bootstrap method to compute confidence intervals see bootstrap
correct corrects the second round in a way there is the same number of voters
get_candidates_votes return the numbers of voters for each candidates
get_people returns the number of people per regions
GetNbCandidates returns the number of candidates
process_tour keeps the interesting columns, move candidates name as column name
resample Builds a new sample: it produces a results with the same number of rows, but each rows is randomly drawn from the current …
vote_transfer compute the votes between the two rounds using contrainsts optimization, the optimization requires cvxopt

Documentation

Contains a class to process elections results (France)

source on GitHub

class ensae_teaching_cs.special.elections.ElectionResults(file, year=None, level='Départements')[source]

Bases : object

processes data coming from data.gouv.fr.

The class uses pandas to process the data

See Elections françaises.

See Optimisation sous contraintes appliquée au calcul du report des voix.

source on GitHub

loads the data downloaded from data.gouv.fr.

Paramètres:
  • file – xls file
  • year – year (optional)
  • levelDépartements or Cantons

source on GitHub

GetNbCandidates(round)[source]

returns the number of candidates

Paramètres:round – round (0 or 1)
Retourne:number of candidates

source on GitHub

Level

returns the level (département or canton)

source on GitHub

LevelCol

returns the column associated to the level (their name depends on the level)

source on GitHub

T0

returns the dataframe for the first round

source on GitHub

T1

returns the dataframe for the second round

source on GitHub

WeightsNorm

return the proportion of voters for each regions

source on GitHub

Year

returns the year

source on GitHub

__init__(file, year=None, level='Départements')[source]

loads the data downloaded from data.gouv.fr.

Paramètres:
  • file – xls file
  • year – year (optional)
  • levelDépartements or Cantons

source on GitHub

__str__()[source]

usual

source on GitHub

bootstrap(iter=1000, method='vote_transfer', alpha=0.05, fLOG=None, **params)[source]

uses the bootstrap method to compute confidence intervals see bootstrap

Paramètres:
  • iter – number of iteration
  • method – method to bootstrap
  • alpha – confidence level
  • fLOG – logging function or none
  • params – parameters to give to method
Retourne:

four matrices, averaged results, sigma, lower bound, higher bound

source on GitHub

static combine_into_string(matrices, float_format=<class 'str'>, agg_format=<class 'str'>)[source]

combine two matrices into one before displaying it

Paramètres:
  • matrices – list of matrices (same dimension)
  • float_format – to format each float of all matrices
  • agg_format – to build the aggregated string
Retourne:

matrixes (dataframe)

Example:

def pour(x) :
    if x < 0.01 : return ""
    else : return "%2.0f" % (x*100) + "%"

boot = el.bootstrap(iter=10)
comb = el.combine_string( [boot[2],boot[3]], pour, lambda v : "%s-%s" % tuple(v))

source on GitHub

correct(method=None)[source]

corrects the second round in a way there is the same number of voters

Paramètres:method – some preprocess before going on (see below)
About method:
  • “N” –> correct the number of voters for each regions
  • “cand” –> gives the same weights to every candidates

source on GitHub

get_candidates_votes(round)[source]

return the numbers of voters for each candidates

Paramètres:round – 0 or 1
Retourne:dictionary

source on GitHub

get_people(round=0)[source]

returns the number of people per regions

Paramètres:round – first (0) or second (1) round
Retourne:series

source on GitHub

static min_max_mean_std(series, alpha=0.05)[source]

returns the mean standard deviation, bounds of the confidence interval

Paramètres:
  • series – list of numbers
  • alpha – confidence level
Retourne:

mean, std, low, high

source on GitHub

process_tour(tour)[source]

keeps the interesting columns, move candidates name as column name

Paramètres:tour – dataframe
Retourne:dataframe

source on GitHub

resample(method='uniform')[source]

Builds a new sample: it produces a results with the same number of rows, but each rows is randomly drawn from the current data. This is needed for the bootstrap procedures.

Paramètres:methodweight or uniform
Retourne:two matrices

source on GitHub

vote_transfer()[source]

compute the votes between the two rounds using contrainsts optimization, the optimization requires cvxopt

see Optimisation sous contraintes appliquée au calcul du report des voix.

Retourne:results (as a DataFrame)

source on GitHub