利用python完成课程作业ex2的第二部分,第二部分的要求如下:
In this part of the exercise, you will implement regularized logistic regression to predict whether microchips from a fabrication plant passes quality assurance (QA). During QA, each microchip goes through various tests to ensure it is functioning correctly. Suppose you are the product manager of the factory and you have the test results for some microchips on two dierent tests. From these two tests, you would like to determine whether the microchips should be accepted or rejected. To help you make the decision, you have a dataset of test results on past microchips, from which you can build a logistic regression model.
在ex2_reg.m的代码中,我将其分为四个部分,每个部分分别如下:
Part 1: Plotting
Part 2: Regularized Logistic Regression
Part 3: Regularization and Accuracies
Part 4: PLot Decision Boundary
该算法的整体结构,实用性和可读性低,即部分是没有定义函数的,输出的时候也没有明确的指示,在完成整个作业之后会重新对算法进行进一步的修改。我的代码如下:
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 13 09:55:27 2019
@author: Lonely_hanhan
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as op
from mpl_toolkits.mplot3d import Axes3D
Data = np.loadtxt('D:\exercise\machine-learning-ex2\ex2\ex2data2.txt', delimiter=',')
X = Data[:,0:2]
[m,n] = X.shape
Y = Data[:,2] #此处读取的时一维数组,需转化为m维数组
Y = Y.reshape((m,1))
''' ==================== Part 1: Plotting ==================== '''
pos = np.where(Y == 1)
neg = np.where(Y == 0)
#由于Y为二维数组,所以where得到的值都为tuple类型,需提