In [2]:
%matplotlib notebook
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.offline
#plotly.offline.plot
In [3]:
df = pd.read_csv("https://raw.githubusercontent.com/Niederb/political_science/master/PCA-parlament/5006-2016-wintersession-d.csv")
fraktionen = df[0:1]
colors = {}
colors["V"] = 'darkgreen' #SVP
colors["S"] = 'red' #SP
colors["RL"] = 'blue' #FDP
colors["C"] = 'orange' #CVP
colors["G"] = 'green' #greens
colors["BD"] = 'yellow' #BDP
colors["GL"] = 'lightgreen' #GLP
fraktionen_colors = [colors[i] for i in fraktionen.values[0]]
In [5]:
data= df[1:]
pca = PCA(n_components=2)
coordinates = pca.fit_transform(data.T)
trace = go.Scatter(
x = coordinates[:, 0],
y = coordinates[:, 1],
mode = 'markers',
marker = dict(
size='16',
color = fraktionen_colors
),
text=list(df)
)
layout = go.Layout(
hovermode = 'closest',
title='PCA analyse Nationalrat Wintersession 2016',
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='2d-PCA-wintersession-2016')#filename='basic-scatter')
Out[5]:
In [6]:
data= df[1:]
pca = PCA(n_components=3)
coordinates = pca.fit_transform(data.T)
trace = go.Scatter3d(
x = coordinates[:, 0],
y = coordinates[:, 1],
z = coordinates[:, 2],
mode = 'markers',
marker = dict(
size='16',
color = fraktionen_colors
),
text=list(df)
)
layout = go.Layout(
hovermode = 'closest',
title='3D PCA analyse Nationalrat Wintersession 2016',
)
data = [trace]
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='3d-PCA-wintersession-2016')
#plotly.offline.plot(data, include_plotlyjs=True, output_type='div')
Out[6]:
In [ ]: