# ml-class / ex8.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62``` ```#!/usr/bin/env python import numpy as np import matplotlib.pyplot as plt from scipy.io import loadmat from sklearn.covariance import EmpiricalCovariance, MinCovDet def p1(x, var, mue): return (1/np.sqrt(2*np.pi*var))*(np.e**(-(((x-mue)**2)/2*var))) def p(x, mue, var): total = 1 for xi, vari, muei in zip(x, var, mue): total *= p1(xi, vari, muei) return total def calc_contour(data, fn): xs = np.linspace(data[:,0].min(), data[:,0].max(), 100) ys = np.linspace(data[:,1].min(), data[:,1].max(), 100) z = np.zeros(shape=(len(xs), len(ys))) for x, xv in enumerate(xs): for y, yv in enumerate(ys): z[x, y] = fn(np.array([xv, yv])) return xs, ys, z def anplot(data, fn): xs, ys, z = calc_contour(data, lambda x: fn(x)) plt.scatter(data[:,0], data[:,1], marker='x') exps = np.arange(-20, -1, 3) fn = np.vectorize(lambda n: 10**n) plt.contour(xs, ys, z) #, fn(exps)) plt.grid() plt.show() def anomaly(): data = loadmat('ex8/ex8data1.mat') train = data['X'] mue = train.mean(0) var = train.var(0) fn = lambda x: p(x, mue, var) anplot(train, fn) def anomaly_skl(): data = loadmat('ex8/ex8data1.mat') X = data['X'] cov = MinCovDet().fit(X) fn = lambda x: cov.mahalanobis([x]) anplot(X, cov.score) if __name__ == '__main__': anomaly() raw_input() ```