Week3: Logistic Regression

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt

# Importing data
data = pd.read_csv(ex2data1.txt, delimiter=,, names=[Exam1 Score, Exam2 Score, Admitted])
X = data[[Exam1 Score, Exam2 Score]].values
y = data[Admitted].values
m,n = np.shape(X)
y = y.reshape(m, 1)

# Plot the data
def plot_raw_data(X, y, show = False):
admitted = plt.scatter(X[y[:, 0] == 1, 0], X[y[:, 0] == 1, 1], marker=+, color=b)
not_admitted = plt.scatter(X[y[:, 0] == 0, 0], X[y[:, 0] == 0, 1], marker=o, color=y)
plt.xlabel(Exam1 Score)
plt.ylabel(Exam2 Score)
plt.legend((admitted, not_admitted), ("Admitted", "Not admitted"))
if show is True:
plt.show()

plot_raw_data(X, y, True)

# Define the cost function and gradient
def sigmoid(X):
return 1/(1 + np.exp(-X))

def cost_function(theta, X, y):
m, n = np.shape(X)
theta = theta.reshape(n , 1)
h = sigmoid(X.dot(theta))
j = (1/m)*(-y.T.dot(np.log(h)) - (1-y).T.dot(np.log(1-h)))
grad = (1/m)*X.T.dot(h-y)
return j, grad

# Test the accuracy of cost function
X = np.append(np.ones((m, 1)), X, axis = 1)
initial_theta = np.zeros(((n+1), 1))
print(The cost should be around 0.693)
print(cost_function(initial_theta, X, y)[0])

# Fit parameter theta using truncated Newton algorithm
initial_theta = np.zeros(((n+1))) # initial theta must be 1 d array
result = opt.fmin_tnc(func = cost_function, x0=initial_theta, args=(X, y))
optimal_theta = result[0].reshape(3,1)

# Plot decision boundary
def plot_decision_boundary(X, y, theta):
plot_raw_data(X[:,1:], y, False)
exam1 = np.linspace(30, 100, 5)
exam2 = -(theta[0] + theta[1]*exam1)/ theta[2]
plt.plot(exam1,exam2)
plt.show()

plot_decision_boundary(X, y, optimal_theta)

# Predict the admission chance based on score
score = np.array([1, 45, 85]).reshape(1,3)
admission_probability = sigmoid(score.dot(optimal_theta))
print(The probability of being admitted with Exam1 score: 45 and Exam2 score:85 should be around 0.78)
print(admission_probability)

Week3: Logistic Regression With Regularization

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as opt

# Importing data
data = pd.read_csv(ex2data2.txt, delimiter=,, names=[Microchip Test 1,Microchip Test 2, Passed])
X = data[[Microchip Test 1,Microchip Test 2]].values
y = data[Passed].values

m, n = np.shape(X)
y = y.reshape(m, 1)

# Plotting the data
def plot_raw_data(X, y, show = False):
passed = plt.scatter(X[y[:, 0] == 1, 0], X[y[:, 0] == 1, 1], marker=+, color=b)
not_passed = plt.scatter(X[y[:, 0] == 0, 0], X[y[:, 0] == 0, 1], marker=o, color=y)
plt.xlabel(Microchip Test 1)
plt.ylabel(Microchip Test 2)
plt.legend((passed, not_passed), ("Pass tests", "Fail to pass tests"))
if show is True:
plt.show()

plot_raw_data(X, y, True)

# Create higher order features
def map_features(X):
degree = 6
res = np.ones((X.shape[0], 1))
for i in range(1, degree + 1):
for j in range(0, i + 1):
append_array = np.array([np.power(X[:, 0], (i-j))*np.power(X[:, 1], j)]).reshape(len(res), 1)
res = np.append(res, append_array, axis=1)
return res

new_features = map_features(X)

# Define cost function and gradient
def sigmoid(x):
return 1/(1 + np.exp(-x))

def cost_function_reg(theta, X, y, lambda_reg):
m, n = np.shape(X)
theta = theta.reshape(n, 1)
h = sigmoid(X.dot(theta))
initial_theta = theta[0]
theta[0] = 0
j = (1 / m) * (-y.T.dot(np.log(h)) - (1 - y).T.dot(np.log(1 - h))) + lambda_reg/(2*m)*theta.T.dot(theta)
grad = (1/m)*X.T.dot(h-y) + lambda_reg /m * theta
theta[0] = initial_theta
return j, grad.flatten()

# Test the accuracy of cost function
m, n = np.shape(new_features)
initial_theta = np.zeros((n, 1))
lambda_reg = 1
cost, grad = cost_function_reg(initial_theta, new_features, y, lambda_reg)
print(The cost should be around 0.693)
print(cost)

# Fit the data to find theta using BFGS algorithm
test_theta = np.ones(n)
lambda_reg = 1
result = opt.minimize(fun=cost_function_reg, method=BFGS, jac=True, x0=test_theta, args=(new_features, y, lambda_reg)) # gradient in the fun must be 1d
optimal_theta = result.x

# Plot decision boundary with reg = 1
def plot_decision_boundary(X, y, theta):
test1 = np.linspace(-1, 1.5, 1000)
test2 = np.linspace(-1, 1.5, 1000)
X1 = []
X2 = []
for p1 in test1:
for p2 in test2:
X1.append(p1)
X2.append(p2)
X_points =np.array([X1, X2]).T
X_points_new_features = map_features(X_points)
epsilon = 0.01
X_boundary = X_points[(np.abs(X_points_new_features.dot(theta)) < epsilon).flatten()]
plt.scatter(X_boundary[:, 0], X_boundary[:, 1], color=green, marker=.)
plot_raw_data(X, y, False)
plt.show()

plot_decision_boundary(X, y, optimal_theta.reshape(n, 1))

推薦閱讀:

相關文章