module special.elections
#
Short summary#
module ensae_teaching_cs.special.elections
Contains a class to process elections results (France)
Classes#
class |
truncated documentation |
---|---|
Processes data coming from data.gouv.fr. … |
Properties#
property |
truncated documentation |
---|---|
Returns the level ( |
|
Returns the column associated to the level (their name depends on the level). |
|
Returns the dataframe for the first round. |
|
Returns the dataframe for the second round. |
|
Returns the proportion of voters for each regions. |
|
Returns the year. |
Static Methods#
staticmethod |
truncated documentation |
---|---|
Combines two matrices into one before displaying it. |
|
returns the mean standard deviation, bounds of the confidence interval |
Methods#
method |
truncated documentation |
---|---|
Loads the data downloaded from data.gouv.fr. … |
|
usual |
|
Uses the bootstrap method to compute confidence intervals see bootstrap. … |
|
Corrects the second round in a way there is the same number of voters. |
|
Returns the numbers of voters for each candidate. |
|
Returns the number of people per regions. |
|
Returns the number of candidates. |
|
Keeps the interesting columns, move candidates name as column name. |
|
Builds a new sample: it produces a results with the same number of rows, but each rows is randomly drawn from the … |
|
Computes the votes between the two rounds using contrainsts optimization, the optimization requires cvxopt. … |
Documentation#
Contains a class to process elections results (France)
- class ensae_teaching_cs.special.elections.ElectionResults(file, year=None, level='Départements')#
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.
Loads the data downloaded from data.gouv.fr.
- Paramètres:
file – xls file
year – year (optional)
level –
Départements
orCantons
- GetNbCandidates(round)#
Returns the number of candidates.
- Paramètres:
round – round (0 or 1)
- Renvoie:
number of candidates
- property Level#
Returns the level (
département
orcanton
).
- property LevelCol#
Returns the column associated to the level (their name depends on the level).
- property T0#
Returns the dataframe for the first round.
- property T1#
Returns the dataframe for the second round.
- property WeightsNorm#
Returns the proportion of voters for each regions.
- property Year#
Returns the year.
- __init__(file, year=None, level='Départements')#
Loads the data downloaded from data.gouv.fr.
- Paramètres:
file – xls file
year – year (optional)
level –
Départements
orCantons
- __str__()#
usual
- bootstrap(iter=1000, method='vote_transfer', alpha=0.05, fLOG=None, **params)#
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
- static combine_into_string(matrices, float_format=<class 'str'>, agg_format=<class 'str'>)#
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))
- correct(method=None)#
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
- get_candidates_votes(round)#
Returns the numbers of voters for each candidate.
- Paramètres:
round – 0 or 1
- Renvoie:
dictionary
- get_people(round=0)#
Returns the number of people per regions.
- Paramètres:
round – first (0) or second (1) round
- Renvoie:
series
- static min_max_mean_std(series, alpha=0.05)#
returns the mean standard deviation, bounds of the confidence interval
- Paramètres:
series – list of numbers
alpha – confidence level
- Renvoie:
mean, std, low, high
- process_tour(tour)#
Keeps the interesting columns, move candidates name as column name.
- Paramètres:
tour – dataframe
- Renvoie:
dataframe
- resample(method='uniform')#
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:
method –
weight
oruniform
- Renvoie:
two matrices
- vote_transfer()#
Computes 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.
- Renvoie:
results (as a DataFrame)