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 Returns the proportion of voters for each regions.
Year Returns the year.

Static Methods

staticmethod truncated documentation
combine_into_string Combines 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 Returns the numbers of voters for each candidate.
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 …
vote_transfer Computes the votes between the two rounds using contrainsts optimization, the optimization requires :epkg:`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)
Renvoie: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

Returns 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
Renvoie:

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]

Combines 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
Renvoie:

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]

Returns the numbers of voters for each candidate.

Paramètres:round – 0 or 1
Renvoie: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
Renvoie: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
Renvoie:

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
Renvoie: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
Renvoie:two matrices

source on GitHub

vote_transfer()[source]

Computes the votes between the two rounds using contrainsts optimization, the optimization requires :epkg:`cvxopt`.

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

Renvoie:results (as a DataFrame)

source on GitHub