In [7]:
import pandas as pd
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
df = pd.read_csv("https://raw.githubusercontent.com/Niederb/political_science/master/gewinner-kantone/alle-volksabstimmungen-resultate.csv", sep=";")#, encoding="ascii")
stimmberechtigte = pd.read_csv("https://raw.githubusercontent.com/Niederb/political_science/master/gewinner-kantone/stimmberechtigte.csv", sep=";")
data = df[list(df.columns[2:-1])] #df.ix[:,[2:5]]#'Schweiz':]
data_mat = data.as_matrix()
schweiz = data_mat[:, 0]
kantone = data_mat[:, 1:]
schweiz_thresholded = schweiz > 50
kantone_thresholded = kantone > 50
korrekt = schweiz_thresholded == kantone_thresholded.T
korrekt = korrekt.T
success_rate = np.mean(korrekt, 0)
L1_error = np.abs(schweiz - kantone.T)
L2_error = np.sqrt(np.sum(np.square(L1_error), 1))
mean_L1_error = np.mean(L1_error, 1)
stimmberechtigte_2 = np.mean(stimmberechtigte.as_matrix(), 0)
kantonsnamen = list(stimmberechtigte)
In [21]:
trace = go.Scatter(
x = success_rate,
y = stimmberechtigte_2,
mode = 'markers',
marker = dict(
size = 20,
color = 'rgba(255, 161, 0, 1.0)',
line = dict(
width = 2,
)
),
text=kantonsnamen
)
layout = go.Layout(
hovermode = 'closest',
title='Erfolgsrate vs Stimmberechtigte',
xaxis=dict(title='Erfolgsrate in %'),
yaxis=dict(title='Anzahl Stimmberechtigte (Durchschnitt)')
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='erfolgsrate')
Out[21]:
In [22]:
trace = go.Scatter(
x = mean_L1_error,
y = stimmberechtigte_2,
mode = 'markers',
marker = dict(
size = 20,
color = 'rgba(255, 161, 0, 1.0)',
line = dict(
width = 2,
)
),
text=kantonsnamen
)
layout = go.Layout(
hovermode = 'closest',
title='Abweichung vs Stimmberechtigte',
xaxis=dict(title='Durchschnittliche Abweichung in % (kleiner ist Genauer)'),
yaxis=dict(title='Anzahl Stimmberechtigte (Durchschnitt)')
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='genauigkeit')
Out[22]:
In [20]:
trace = go.Scatter(
x = mean_L1_error,
y = success_rate,
mode = 'markers',
marker = dict(
size = 20,
color = 'rgba(255, 161, 0, 1.0)',
line = dict(
width = 2,
)
),
text=kantonsnamen
)
layout = go.Layout(
hovermode = 'closest',
title='Abweichung vs Erfolgsrate',
xaxis=dict(title='Durchschnittliche Abweichung in % (kleiner ist Genauer)'),
yaxis=dict(title='Erfolgsrate in %')
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='korrelation-erfolg-genauigkeit')
Out[20]:
In [ ]: