Created
December 30, 2018 06:15
-
-
Save hokuma/660c5533eeaab8b8a1256a2f51020836 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "IdeologyAnalyzer.ipynb", | |
"version": "0.3.2", | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"metadata": { | |
"id": "iU5O8J5DCKGN", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"from google.colab import drive\n", | |
"drive.mount('/content/gdrive')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "fi7ynVsgC8AR", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"\n", | |
"df = pd.read_csv('/content/gdrive/PATH/TO/YOUR/VOTES/votes.csv', header=None, usecols=range(2,11)).transpose()\n", | |
"votes = np.where(df < 0, 0, df)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "FqCxCZCSEjU7", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"from sklearn.decomposition import PCA\n", | |
"\n", | |
"pca = PCA(n_components=2)\n", | |
"reduced = pca.fit_transform(votes)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "DgN1LIbDsU-W", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"ev_ratio = pca.explained_variance_ratio_\n", | |
"print(ev_ratio)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "bbWz67G6FR5m", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"colors = ['#ff2800', '#faf500', '#35a16b', '#0041ff', '#66ccff', '#ff99a0', '#ff9900', '#9a0079', '#663300']\n", | |
"parties = ['Kashiwa-Seifu', 'Komei-to', 'NihonKyosan-to', 'Hakuai Club', 'Simin Side Net', 'Goken Shimin Kaigi', 'Hiroki Uchida', 'Kamihashi Izumi', 'Kitamura Kazuyuki']\n", | |
"for i, row in enumerate(reduced):\n", | |
" plt.scatter(row[0], row[1], color=colors[i], label=parties[i])\n", | |
"\n", | |
"plt.legend(loc='upper left')\n" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"id": "JTIk1p8YTKhz", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"import json\n", | |
"\n", | |
"class MyEncoder(json.JSONEncoder):\n", | |
" def default(self, obj):\n", | |
" if isinstance(obj, np.integer):\n", | |
" return int(obj)\n", | |
" elif isinstance(obj, np.ndarray):\n", | |
" return obj.tolist()\n", | |
" else:\n", | |
" return super(MyEncoder, self).default(obj)\n", | |
"\n", | |
"party_names = ['柏清風', '公明党', '日本共産党', '柏愛倶楽部', '市民サイド・ネット', '護憲市民会議', '内田博紀', '上橋泉', '北村和之']\n", | |
"parties = [{'name': name, 'point': reduced[i]} for i, name in enumerate(party_names)]\n", | |
" \n", | |
"with open('/content/gdrive/PATH/TO/YOUR/ANALYZED/RESULT/ideology.json', 'w') as out:\n", | |
" out.write(json.dumps({'ideology': {'parties': parties}}, cls = MyEncoder))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment