Last active
December 14, 2015 06:39
-
-
Save satomacoto/5044090 to your computer and use it in GitHub Desktop.
MDS to nihonseiji.com
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import numpy | |
import scipy.spatial | |
# データ読み込み | |
f = open('nihonseiji.txt') | |
head = f.readline() | |
parties = head.strip().split('\t')[1:] | |
vlist = [] | |
for line in f: | |
cols = line.strip().split('\t') | |
matches = [float(col) for col in cols[1:]] | |
vlist.append(matches) | |
A = numpy.vstack(vlist) | |
# 距離行列の計算 | |
r, c = A.shape | |
D = numpy.zeros((c, c)) | |
VI = numpy.linalg.inv(numpy.cov(A)) | |
for i in range(c): | |
for j in range(i, c): | |
u = A[:,i] | |
v = A[:,j] | |
# ユークリッド距離 | |
D[i,j] = D[j,i] = scipy.spatial.distance.euclidean(u, v) | |
# マハラノビス距離 | |
# D[i,j] = D[j,i] = scipy.spatial.distance.mahalanobis(u, v, VI) | |
# 相関行列 | |
# print numpy.corrcoef(A.T)[i,j], | |
# 分散共分散行列 | |
# print numpy.cov(A) | |
# データの個数 | |
N = len(D) | |
# 距離の2乗の行列 (arrayだと要素同士の掛け算になる) | |
S = D * D | |
# 中心化行列 | |
one = numpy.eye(N) - numpy.ones((N,N))/N | |
# ヤング・ハウスホルダー変換 | |
P = - 1.0/2 * one * S * one | |
# スペクトル分解 | |
w,v = numpy.linalg.eig(P) | |
ind = numpy.argsort(w) | |
x1 = ind[-1] # 1番 | |
x2 = ind[-2] # 2番 | |
# print w[x1],w[x2] | |
# 標準されたデータの固有値が求められているので標準偏差を掛けて座標を求める | |
s = P.std(axis=0) | |
w1 = s[x1] | |
w2 = s[x2] | |
X = [] | |
Y = [] | |
for i in range(N): | |
X += [w1*v[i,x1]] | |
Y += [w2*v[i,x2]] | |
print parties[i], w1*v[i,x1], w2*v[i,x2] |
This file contains hidden or 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
政策 民主党 自由民主党 日本未来の党 公明党 日本維新の会 みんなの党 日本共産党 社会民主党 国民新党 新党大地 新党改革 | |
消費税増税 1 1 -1 1 1 -1 -1 -1 1 -1 1 | |
TPP参加 1 -1 -1 -1 0 1 -1 -1 -1 -1 1 | |
脱原発 1 -1 1 0 -1 1 1 1 -1 1 0 | |
郵政民営化 -1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 | |
後期高齢者医療制度廃止 1 -1 1 -1 1 1 1 1 0 1 0 | |
児童手当拡充 1 -1 1 0 0 -1 0 1 1 0 1 | |
日米同盟維持 1 1 1 1 1 1 -1 -1 1 0 1 | |
年金一元化 1 -1 1 -1 1 -1 1 1 0 1 -1 | |
道州制導入 -1 1 1 1 1 1 -1 -1 1 0 1 | |
議員定数削減 1 1 1 0 1 0 -1 -1 1 1 1 | |
憲法9条改正 0 1 0 -1 0 1 -1 -1 1 1 1 | |
高校無償化 1 -1 1 1 1 -1 1 1 1 0 0 | |
最低賃金引き上げ 0 -1 0 0 0 0 1 1 0 1 -1 | |
政治献金禁止 -1 -1 0 1 0 1 1 1 -1 1 1 | |
裁判員制度維持 1 1 0 1 0 0 -1 -1 -1 -1 0 | |
マイナンバー導入 1 0 1 0 1 1 -1 -1 1 0 1 | |
尖閣諸島実効支配強化 -1 1 -1 -1 1 0 -1 -1 1 -1 0 | |
外国人参政権付与 1 -1 1 1 -1 -1 1 1 -1 1 0 | |
公共事業継続 -1 1 1 1 -1 -1 -1 -1 1 1 -1 | |
日銀法改正 1 0 1 1 1 1 -1 1 0 0 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment