2017年8月20日日曜日

MIT OCW, Machine Learning 06日目 宿題2

Rohit Singh, Tommi Jaakkola, and Ali Mohammad. 6.867 Machine Learning. Fall 2006. Massachusetts Institute of Technology: MIT OpenCourseWare, https://ocw.mit.edu. License: Creative Commons BY-NC-SA.

Assignment

Problem set 1 Section B

1.

パーセプトロンの実装.pythonを使う.

import numpy as np
import matplotlib.pyplot as plt

def sign(r):
    if r >= 1:
        return 1
    else:
        return -1

class PerceptronClassifier:
    def get_params(self):
        gamma = np.argmin(np.abs([np.dot(self.theta, x) for x in self.train_X]))
        gamma_geom = gamma / np.linalg.norm(self.theta)
        print("theta is: {0}, k till the convergence is {1}".format(self.theta, self.k))
        print("The angle with [1, 0] and theta is: {0} (rad)".format(np.arccos(self.theta[0]/np.linalg.norm(self.theta))))
        print("The geometric margin is {0}".format(gamma_geom))

    def perceptron_train(self, X, y):
        self.train_X = X
        self.train_y = y
        self.theta = np.zeros(len(X[0]))
        self.k = 0
        while True:
            cnt = 0
            for i in range(len(self.train_y)):
                if y[i] != sign(np.dot(self.theta, self.train_X[i])):
                    self.k += 1
                    self.theta = self.theta + y[i]*X[i]
                    cnt += 1
            if cnt == 0:
                break

    def perceptron_test(self, test_X, test_y):
        self.test_X = test_X
        self.test_y = test_y
        errors = 0
        for i in range(len(test_y)):
            if test_y[i] != sign(np.dot(self.theta, test_X[i])):
                errors += 1
        print("The error ratio is: {0}".format(errors/len(test_y)))

    def draw_graph(self, train=True):
        if train:
            X = self.train_X
            y = self.train_y
        else:
            X = self.test_X
            y = self.test_y

        plus = np.array([x for x in X if np.dot(self.theta, x)>=0 ])
        minus = np.array([x for x in X if np.dot(self.theta, x) < 0])

        plt.scatter(plus[:, 0], plus[:, 1], color='red', s=2)
        plt.scatter(minus[:, 0], minus[:, 1], color='blue', s=2)
        plt.show()

模範解答とはことなった結果を示すが,収束までの更新回数は更新を行う前に定義するの初期値にわりと鋭敏に反応するので,深く考えなくてもいいかもしれない(MATLABとPythonの精度も関係しているかも?).

X_a = np.loadtxt('p1_a_X.dat' )
y_a = np.loadtxt('p1_a_y.dat')
X_b = np.loadtxt('p1_b_X.dat')
y_b = np.loadtxt('p1_b_y.dat')

per_cla = PerceptronClassifier()
per_cla.perceptron_train(X_a, y_a)
per_cla.perceptron_test(X_a, y_a)
per_cla.draw_graph('Dataset A')
per_cla.get_params()

per_cla = PerceptronClassifier()
per_cla.perceptron_train(X_b, y_b)
per_cla.perceptron_test(X_b, y_b)
per_cla.draw_graph('Dataset B')
per_cla.get_params()


enter image description here

2, 3.

SVMの実装. quadratic programを解く関数を使っていいらしいがpythonだとpipにも入ってないから飛ばす

0 件のコメント:

コメントを投稿