{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [ ], "source": [ "data = pd.read_csv('http://petras.kudaras.lt/notebooks/manoseimas.csv').set_index('Partija')/100." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PartijaTTDPLSDPAWLRLSMGTSLK
Šauktiniai0.920.890.960.720.950.790.91
Meras0.820.940.890.850.310.890.55
Internetinis_balsavimas0.490.800.910.111.000.740.22
Darbo_kodeksas0.910.790.980.150.950.410.42
Švietimas0.910.920.860.600.290.630.42
Vaiko_teisės0.740.780.910.440.760.400.34
Alkoholis0.590.530.550.680.440.750.82
Pensijos0.790.850.890.250.880.240.30
Pabėgėliai0.960.910.970.890.590.840.84
Dviguba_pilietybė0.940.900.940.830.910.810.64
Asmenvardžiai0.810.610.690.830.510.290.15
Privati_informacija0.760.760.810.590.750.630.74
\n", "
" ], "text/plain": [ "Partija TT DP LSDP AW LRLS MG TSLK\n", "Šauktiniai 0.92 0.89 0.96 0.72 0.95 0.79 0.91\n", "Meras 0.82 0.94 0.89 0.85 0.31 0.89 0.55\n", "Internetinis_balsavimas 0.49 0.80 0.91 0.11 1.00 0.74 0.22\n", "Darbo_kodeksas 0.91 0.79 0.98 0.15 0.95 0.41 0.42\n", "Švietimas 0.91 0.92 0.86 0.60 0.29 0.63 0.42\n", "Vaiko_teisės 0.74 0.78 0.91 0.44 0.76 0.40 0.34\n", "Alkoholis 0.59 0.53 0.55 0.68 0.44 0.75 0.82\n", "Pensijos 0.79 0.85 0.89 0.25 0.88 0.24 0.30\n", "Pabėgėliai 0.96 0.91 0.97 0.89 0.59 0.84 0.84\n", "Dviguba_pilietybė 0.94 0.90 0.94 0.83 0.91 0.81 0.64\n", "Asmenvardžiai 0.81 0.61 0.69 0.83 0.51 0.29 0.15\n", "Privati_informacija 0.76 0.76 0.81 0.59 0.75 0.63 0.74" ] }, "execution_count": 3, "metadata": { }, "output_type": "execute_result" } ], "source": [ "data.T.ix[:,:7]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PartijaTTDPLSDPAWLRLSMGTSLK
Partija
TT1.000.570.500.45-0.080.040.30
DP0.571.000.850.070.100.360.13
LSDP0.500.851.00-0.230.510.120.00
AW0.450.07-0.231.00-0.570.480.52
LRLS-0.080.100.51-0.571.00-0.19-0.06
MG0.040.360.120.48-0.191.000.68
TSLK0.300.130.000.52-0.060.681.00
\n", "
" ], "text/plain": [ "Partija TT DP LSDP AW LRLS MG TSLK\n", "Partija \n", "TT 1.00 0.57 0.50 0.45 -0.08 0.04 0.30\n", "DP 0.57 1.00 0.85 0.07 0.10 0.36 0.13\n", "LSDP 0.50 0.85 1.00 -0.23 0.51 0.12 0.00\n", "AW 0.45 0.07 -0.23 1.00 -0.57 0.48 0.52\n", "LRLS -0.08 0.10 0.51 -0.57 1.00 -0.19 -0.06\n", "MG 0.04 0.36 0.12 0.48 -0.19 1.00 0.68\n", "TSLK 0.30 0.13 0.00 0.52 -0.06 0.68 1.00" ] }, "execution_count": 4, "metadata": { }, "output_type": "execute_result" } ], "source": [ "data.T.ix[:,:7].corr().round(2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PartijaTTDPLSDPAWLRLSMGTSLKPETRAS
Partija
TT1.000.570.500.45-0.080.040.300.61
DP0.571.000.850.070.100.360.130.12
LSDP0.500.851.00-0.230.510.120.000.07
AW0.450.07-0.231.00-0.570.480.520.41
LRLS-0.080.100.51-0.571.00-0.19-0.06-0.01
MG0.040.360.120.48-0.191.000.68-0.24
TSLK0.300.130.000.52-0.060.681.00-0.07
PETRAS0.610.120.070.41-0.01-0.24-0.071.00
\n", "
" ], "text/plain": [ "Partija TT DP LSDP AW LRLS MG TSLK PETRAS\n", "Partija \n", "TT 1.00 0.57 0.50 0.45 -0.08 0.04 0.30 0.61\n", "DP 0.57 1.00 0.85 0.07 0.10 0.36 0.13 0.12\n", "LSDP 0.50 0.85 1.00 -0.23 0.51 0.12 0.00 0.07\n", "AW 0.45 0.07 -0.23 1.00 -0.57 0.48 0.52 0.41\n", "LRLS -0.08 0.10 0.51 -0.57 1.00 -0.19 -0.06 -0.01\n", "MG 0.04 0.36 0.12 0.48 -0.19 1.00 0.68 -0.24\n", "TSLK 0.30 0.13 0.00 0.52 -0.06 0.68 1.00 -0.07\n", "PETRAS 0.61 0.12 0.07 0.41 -0.01 -0.24 -0.07 1.00" ] }, "execution_count": 5, "metadata": { }, "output_type": "execute_result" } ], "source": [ "data.T.corr().round(2)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[ 3.20154150e-01 +0.00000000e+00j,\n", " -3.42352852e-01 +0.00000000e+00j,\n", " -1.63719781e-01 +0.00000000e+00j,\n", " -1.58295388e-01 +0.00000000e+00j,\n", " 8.57586129e-02 +0.00000000e+00j,\n", " -4.71657375e-02 +0.00000000e+00j,\n", " -1.48061657e-16 +0.00000000e+00j,\n", " -3.82778902e-17 +0.00000000e+00j,\n", " -1.65287341e-16 +0.00000000e+00j,\n", " 1.77182475e-16 +1.36613093e-17j,\n", " 1.77182475e-16 -1.36613093e-17j,\n", " -2.26405114e-16 +0.00000000e+00j],\n", " [ 4.16700182e-01 +0.00000000e+00j,\n", " -2.29248261e-01 +0.00000000e+00j,\n", " 1.46657006e-01 +0.00000000e+00j,\n", " -4.92214565e-03 +0.00000000e+00j,\n", " -1.06137660e-01 +0.00000000e+00j,\n", " -4.93872292e-02 +0.00000000e+00j,\n", " 2.52659466e-16 +0.00000000e+00j,\n", " 3.35547327e-18 +0.00000000e+00j,\n", " 1.33891767e-17 +0.00000000e+00j,\n", " -4.65968536e-17 +9.32450063e-18j,\n", " -4.65968536e-17 -9.32450063e-18j,\n", " 4.07423015e-17 +0.00000000e+00j]])" ] }, "execution_count": 23, "metadata": { }, "output_type": "execute_result" } ], "source": [ "import statsmodels.sandbox.tools as smt\n", "proj = smt.tools_pca.pca(data.ix[:7,:])\n", "proj[1][:2]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 3.72602052e-01 +0.00000000e+00j,\n", " 1.50310469e-01 +0.00000000e+00j,\n", " 5.72978565e-02 +0.00000000e+00j,\n", " 3.25974011e-02 +0.00000000e+00j,\n", " 3.88242528e-03 +0.00000000e+00j,\n", " 2.32408111e-03 +0.00000000e+00j,\n", " 1.45793837e-17 +0.00000000e+00j,\n", " 5.49062001e-18 +0.00000000e+00j,\n", " 8.71149841e-19 +0.00000000e+00j,\n", " -2.63689929e-18 +2.38692229e-18j,\n", " -2.63689929e-18 -2.38692229e-18j, -9.44840897e-18 +0.00000000e+00j])" ] }, "execution_count": 25, "metadata": { }, "output_type": "execute_result" } ], "source": [ "proj[2]" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "%matplotlib inline\n", "import matplotlib\n", "matplotlib.style.use('ggplot')\n", "import matplotlib.pyplot as plt\n", "\n", "def add_label(row):\n", " ax.annotate(row.name, row.values, xytext=(10,-5), \n", " textcoords='offset points',\n", " size=16, color='k'); return row\n", "\n", "pca_df = -pd.DataFrame(data=proj[1], index=data.ix[:7,:].index, columns=['PC1', 'PC2'])\n", "ax = pca_df.plot('PC1','PC2', kind='scatter', c='k', s=50, figsize=(10,7))\n", "pca_df.apply(add_label, axis=1)\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": false }, "outputs": [ ], "source": [ "from IPython.html.widgets import *\n", "\n", "def pca_plot(Sauktiniai, Meras, Internetinis_balsavimas,\n", " Darbo_kodeksas, Svietimas, Vaiko_teises,\n", " Alkoholis, Pensijos, Pabegeliai, Dviguba_pilietybe,\n", " Asmenvardziai, Privati_informacija):\n", " \n", " # Petras projekcija\n", " data_with_petras = pd.concat([data.ix[:7,:], pd.DataFrame([range(12)], index=['PETRAS'], columns=data.columns)])\n", " proj = smt.tools_pca.pca(data_with_petras, keepdim=2)\n", " pca_df = -pd.DataFrame(data=proj[1], index=data_with_petras.index, columns=['PC1', 'PC2'])\n", " ax = pca_df.plot('PC1','PC2', kind='scatter', c='k', s=50, figsize=(10,7))\n", "\n", " def add_label(row):\n", " ax.annotate(row.name, row.values, xytext=(10,-5), \n", " textcoords='offset points',\n", " size=16, color='k'); return row\n", "\n", " pca_df.apply(add_label, axis=1)\n", " plt.show()\n", "\n", "interact(pca_plot, Sauktiniai=0.80, Meras=0.60, Internetinis_balsavimas=0.10,\n", " Darbo_kodeksas=0.55, Svietimas=0.50, Vaiko_teises=0.40,\n", " Alkoholis=0.30, Pensijos=0.60, Pabegeliai=0.30, \n", " Dviguba_pilietybe=0.80, Asmenvardziai=0.90, Privati_informacija=0.30)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "collapsed": true }, "outputs": [ ], "source": [ ] } ], "metadata": { "kernelspec": { "display_name": "Python 2 (system-wide)", "language": "python", "metadata": { "cocalc": { "description": "Python 2 programming language", "priority": 5, "url": "https://www.python.org/" } }, "name": "python2", "resource_dir": "/ext/jupyter/kernels/python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }