欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

吴恩达 机器学习课程 coursera 第二次编程作业(Logistic Regression Regularized) python实现

程序员文章站 2022-07-13 08:52:05
...

本文是吴恩达机器学习课程的第二次编程作业:Logistic Regression Regularized 的扩展作业,用python实现。

 

本作业包含5个文件,分别是:

ex2_reg.py 主程序入口

predict.py 预测函数

gradientReg.py 梯度下降算法

costFunctionReg.py 代价函数算法

sigmoid.py 函数计算方法

 

作业文件和训练集数据下载地址:https://github.com/toanoyx/MachineLearning-AndrewNg-coursera-python/tree/master/ex2%20Logistic%20Regression/ex2_reg

 

下文是文件的源代码:

ex2_reg.py 主程序入口

import pandas as pd
import matplotlib.pyplot as plt
import scipy.optimize as opt
from costFunctionReg import *
from gradientReg import *
from predict import *

""" 第1部分 可视化数据集 """

path = 'ex2data2.txt'
data2 = pd.read_csv(path, header=None, names=['Test 1', 'Test 2', 'Accepted'])

positive = data2[data2['Accepted'].isin([1])]
negative = data2[data2['Accepted'].isin([0])]

fig, ax = plt.subplots(figsize=(12,8))
ax.scatter(positive['Test 1'], positive['Test 2'], s=50, c='black', marker='+', label='y = 1')
ax.scatter(negative['Test 1'], negative['Test 2'], s=50, c='yellow', marker='o', label='y = 0')
ax.legend()
ax.set_xlabel('Microchip Test 1')
ax.set_ylabel('Microchip Test 2')
plt.show()

degree = 5
x1 = data2['Test 1']
x2 = data2['Test 2']

data2.insert(3, 'Ones', 1)

for i in range(1, degree):
    for j in range(0, i):
        data2['F' + str(i) + str(j)] = np.power(x1, i-j) * np.power(x2, j)

data2.drop('Test 1', axis=1, inplace=True)
data2.drop('Test 2', axis=1, inplace=True)

""" 第2部分 正则化代价函数 """

cols = data2.shape[1]
X2 = data2.iloc[:,1:cols]
y2 = data2.iloc[:,0:1]

X2 = np.array(X2.values)
y2 = np.array(y2.values)
theta2 = np.zeros(11)

learningRate = 1

print("cost = " + str(costFunctionReg(theta2, X2, y2, learningRate)) + "(cost should be 0.693)")
print(str(gradientReg(theta2, X2, y2, learningRate)))

result2 = opt.fmin_tnc(func=costFunctionReg, x0=theta2, fprime=gradientReg, args=(X2, y2, learningRate))
print(result2)

theta_min = np.matrix(result2[0])
predictions = predict(theta_min, X2)
correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 for (a, b) in zip(predictions, y2)]
accuracy = (sum(map(int, correct)) % len(correct))
print('accuracy = {0}%'.format(accuracy))

draw_boundary(power=6, l=1)

 

predict.py 预测函数

from sigmoid import *


def predict(theta, X):
    probability = sigmoid(X * theta.T)
    return [1 if x >= 0.5 else 0 for x in probability]

 

gradientReg.py 梯度下降算法

import numpy as np
from sigmoid import *


def gradientReg(theta, X, y, learningRate):
    theta = np.matrix(theta)
    X = np.matrix(X)
    y = np.matrix(y)
    parameters = int(theta.ravel().shape[1])
    grad = np.zeros(parameters)
    error = sigmoid(X * theta.T) - y
    for i in range(parameters):
        term = np.multiply(error, X[:, i])
        if i == 0:
            grad[i] = np.sum(term) / len(X)
        else:
            grad[i] = (np.sum(term) / len(X)) + ((learningRate / len(X)) * theta[:, i])
    return grad

 

costFunctionReg.py 代价函数算法

import numpy as np
from sigmoid import *


def costFunctionReg(theta, X, y, learningRate):
    theta = np.matrix(theta)
    X = np.matrix(X)
    y = np.matrix(y)
    first = np.multiply(-y, np.log(sigmoid(X * theta.T)))
    second = np.multiply((1 - y), np.log(1 - sigmoid(X * theta.T)))
    reg = (learningRate / (2 * len(X))) * np.sum(np.power(theta[:, 1:theta.shape[1]], 2))
    return np.sum(first - second) / len(X) + reg

 

sigmoid.py 函数计算方法

import numpy as np


def sigmoid(z):
    return 1 / (1 + np.exp(-z))