「Machine Learning」(第2週)の復習② (2017/07/02)

今回は第2週の分の後半である。プログラムの実行は課題の「ex2_reg.m」ファイルの順序に基づいている。

内容は随時追加。

準備

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize

# Machine Learning Online Class - Exercise 2: Logistic Regression

# Initialization

# Load Data

data = np.loadtxt("ex2data2.txt",delimiter=",")
x = data[:, 0:2]
y = data[:, 2]

def plot_data(x, y):
    pos = np.where(y==1)
    neg = np.where(y==0)
    plt.plot(x[pos, 0].T, x[pos, 1].T, 'k+', linewidth=2, markersize=7,
             label='y = 1')
    plt.plot(x[neg, 0].T, x[neg, 1].T, 'ko', markerfacecolor='y', markersize=7,
             label='y = 0')

plot_data(x, y)

plt.xlabel('Microchip Test 1')
plt.ylabel('Microchip Test 2')

plt.legend(loc='upper right')
plt.savefig('ex2_reg1.png', dpi=150)  # Added.
plt.show()

画像

Part 1: Regularized Logistic Regression

# =========== Part 1: Regularized Logistic Regression ============
def map_feature(x1, x2):
    degree = 6
    out = np.ones([len(x1), 1])
    end = len(out)  # Added.
    for i in range(1, degree+1):
        for j in range(i+1):
            out = np.c_[out, x1**(i-j)*(x2**j)]
    return out

x = map_feature(x[:, 0], x[:, 1])
initial_theta = np.zeros([x.shape[1], 1])

lambda_ = 1

def sigmoid(z):
    g = np.zeros(z.shape)

    g = 1 / (1+np.exp(-z))
    return g

def cost_function_reg(theta, x, y, lambda_):
    m = len(y)

    grad = np.zeros(theta.shape)

    z = np.dot(x, theta)
    h = sigmoid(z)

    n = grad.shape[0]
    j = (
            1 / m * np.sum(-y.reshape(len(y), 1)*np.log(h)
            -(1-y.reshape(len(y), 1))*np.log(1-h)) + (lambda_ / (2*m))
            * np.sum(theta[1:n]*theta[1:n]))
    grad[0, 0] = (
            1 / m * np.sum((h-y.reshape(len(y), 1))
            *x[:, 0].reshape(len(x[:, 0]), 1)))
    for i in range(1, n):
        grad[i, 0] = (
                1 / m * np.sum((h-y.reshape(len(y), 1))
                *x[:, i].reshape(len(x[:, i]), 1)) + (lambda_ / m) * theta[i])

    return j, grad

cost, grad = cost_function_reg(initial_theta, x, y, lambda_)

print('Cost at initial theta (zeros): ', cost)
print('Expected cost (approx): 0.693')
print('Gradient at initial theta (zeros) - first five values only:')
print(' ', grad[0:5])
print('Expected gradients (approx) - first five values only:')
print(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115')

print('\nProgram paused. Press enter to continue.')

Cost at initial theta (zeros):  0.69314718056
Expected cost (approx): 0.693
Gradient at initial theta (zeros) - first five values only:
  [[  8.47457627e-03]
 [  1.87880932e-02]
 [  7.77711864e-05]
 [  5.03446395e-02]
 [  1.15013308e-02]]
Expected gradients (approx) - first five values only:
 0.0085
 0.0188
 0.0001
 0.0503
 0.0115

Program paused. Press enter to continue.

test_theta = np.ones([x.shape[1], 1])
cost, grad = cost_function_reg(test_theta, x, y, 10)

print('\nCost at test theta (with lambda = 10): ', cost)
print('Expected cost (approx): 3.16')
print('Gradient at test theta - first five values only:')
print(' ', grad[0:5])
print('Expected gradients (approx) - first five values only:')
print(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922')

print('\nProgram paused. Press enter to continue.')

Cost at test theta (with lambda = 10):  3.16450933162
Expected cost (approx): 3.16
Gradient at test theta - first five values only:
  [[ 0.34604507]
 [ 0.16135192]
 [ 0.19479576]
 [ 0.22686278]
 [ 0.09218568]]
Expected gradients (approx) - first five values only:
 0.3460
 0.1614
 0.1948
 0.2269
 0.0922

Program paused. Press enter to continue.