Skip to content

Instantly share code, notes, and snippets.

@shopetan
Created October 1, 2015 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shopetan/59b9da2e0321bb6a74fb to your computer and use it in GitHub Desktop.
Save shopetan/59b9da2e0321bb6a74fb to your computer and use it in GitHub Desktop.
マハラノビス距離
# -*- coding: utf-8 -*-
import numpy as np
import scipy as sc
from scipy import linalg
from scipy import spatial
import scipy.spatial.distance
import pandas as pd
import matplotlib.pyplot as plt
import pylab
import matplotlib.font_manager
ROW = 6
COLUMN = 10
csv = pd.read_csv('test.csv')
# row:行,column:列,ave:平均,vcm:分散共分散行列
row = []
column = []
ave = [0.0 for i in range(ROW)]
vcm = np.zeros((COLUMN, ROW, ROW))
diff = np.zeros((1, ROW))
mahal = np.zeros(COLUMN)
tmp = np.zeros(ROW)
# data欠損値の削除
trans_data = csv.dropna(axis=1)
print(trans_data)
# rowにtrans_dataの要素をリストの形式で連結
for i in range(ROW):
row.append(list(trans_data.ix[i]))
#print(row)
# 列を連結
for i in range(1, COLUMN+1):
column.append(list(trans_data.ix[:, i]))
#print(column)
# 平均値の計算
for i in range(ROW):
# スライスという技法
ave[i] = np.average(row[i][1:len(row[i])])
#print(ave)
# array()でリストを変換した.
column = np.array([column])
ave = np.array(ave)
# 分散共分散行列を求める
# np.swapaxes()で軸を変換することができる.
for i in range(COLUMN):
diff = (column[0][i] - ave)
diff = np.array([diff])
vcm[i] = (diff * np.swapaxes(diff, 0, 1)) / COLUMN
# mahalnobis distanceを求める
for i in range(COLUMN):
# 一般逆行列を生成し,計算の都合上転値をかける
vcm[i] = sc.linalg.pinv(vcm[i])
vcm[i] = vcm[i].transpose()
vcm[i] = np.identity(ROW)
# 差分ベクトルの生成
diff = (column[0][i] - ave)
for j in range(ROW):
tmp[j] = np.dot(diff, vcm[i][j])
mahal[i] = np.dot(tmp, diff)
plot = pylab.arange(0.0, ROW, 1.0)
mahal = np.sqrt(mahal)
print("マハラノビス距離")
print(mahal)
plt.bar(range(COLUMN),mahal)
plt.title("")
plt.xlabel("x")
plt.ylabel("y")
plt.savefig("plot1.png")
1 2 3 4 5 6 7 8 9 10
a 0 6 7 2 3 3 1 0 0 1
b 1 1 11 6 0 2 1 4 1 2
c 2 12 32 5 0 1 3 4 1 1
d 3 3 7 3 2 2 2 1 2 5
e 4 6 6 3 5 1 1 1 1 3
f 5 7 9 5 0 2 2 1 1 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment