1 逻辑回归
逻辑回归算法是用于预测分类的有监督算法,预测结果的正确或错误分类
1.1 作业介绍
在本部分练习中,您将构建一个逻辑回归模型来预测学生是否被大学录取
假设您是一个大学院系的管理员,希望根据两次考试的结果确定每个申请人的入学机会。您可以使用以前申请者的历史数据作为逻辑回归的训练集。对于每个培训示例,您都有申请人在两次考试中的分数和录取决定。
你的任务是建立一个分类模型,根据这两次考试的分数来估计申请者被录取的可能性。
1.2 导入模块和数据
导入模块
import numpy as np
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import scipy.optimize as opt
加载数据
plt.ion()
# 导入数据
data = np.loadtxt('ex2data1.txt', delimiter=',')
data[:5]
共三列,前两列是两个学科的成绩,第三列是否录取
array([[34.62365962, 78.02469282, 0. ],
[30.28671077, 43.89499752, 0. ],
[35.84740877, 72.90219803, 0. ],
[60.18259939, 86.3085521 , 1. ],
[79.03273605, 75.34437644, 1. ]])
前两列赋给x,第三列赋给y
X = data[:, 0:2]
y = data[:, 2]
X[:2]
array([[34.62365962, 78.02469282],
[30.28671077, 43.89499752]])
y值,全部为0或者1,逻辑回归是分类算法,根据输入,判断输出是否录取
y
array([0., 0., 0., 1., 1., 0., 1., 1., 1., 1., 0., 0., 1., 1., 0., 1., 1.,
0., 1., 1., 0., 1., 0., 0., 1., 1., 1., 0., 0., 0., 1., 1., 0., 1.,
0., 0., 0., 1., 0., 0., 1., 0., 1., 0., 0., 0., 1., 1., 1., 1., 1.,
1., 1., 0., 0., 0., 1., 0., 1., 1., 1., 0., 0., 0., 0., 0., 1., 0.,
1., 1., 0., 1., 1., 1., 1., 1., 1., 1., 0., 0., 1., 1., 1., 1., 1.,
1., 0., 1., 1., 0., 1., 1., 0., 1., 1., 1., 1., 1., 1., 1.])
1.3 绘图函数,根据正类或负类汇总散点图
def plot_data(X, y):
plt.figure()
X0 = X[y==0]
X1 = X[y==1]
plt.scatter(X0[:, 0], X0[:, 1], c='r', marker='x')
plt.scatter(X1[:, 0], X1[:, 1], c='b', marker='v')
1.4 调用函数绘图
plot_data(X, y)
plt.axis([30, 100, 30, 100])
plt.legend(['Admitted', 'Not admitted'], loc='best')
plt.xlabel('Exam 1 score')
plt.ylabel('Exam 2 score')
1.5 初始化数据,输入数据增加偏置单元
# Setup the data array appropriately, and add ones for the intercept term
(m, n) = X.shape
# Add intercept term
X = np.c_[np.ones(m), X]
1.6 sigmoid函数
sigmoid函数为逻辑回归激活函数,根据将输入数据将输出数据控制在0~1范围内
def sigmoid(z):
g = 1 / (1 + np.exp(-z))
return g
# 测试sigmoid函数
t1 = np.linspace(-10, 10, 21)
plt.plot(t1, sigmoid(t1), c='r')
plt.show()
1.7 逻辑回归代价函数
初始化theta
#Initialize fitting parameters
initial_theta = np.zeros(n + 1)
逻辑回归代价函数
def cost_function(theta, X, y):
m = y.size
hypothesis = sigmoid(np.dot(X, theta)) # 逻辑回归假设函数
left_cost = np.sum(np.multiply(-y, np.log(hypothesis))) # 左部分代价
right_cost = np.sum(np.multiply(np.subtract(1, y), np.log(1 - hypothesis))) #右部分代价
cost = (left_cost - right_cost)/m # 总代价函数
grad = np.dot(X.T, (hypothesis - y)) / m # 逻辑货柜梯度
return cost, grad
cost_function(initial_theta, X, y)
(0.6931471805599453, array([ -0.1 , -12.00921659, -11.26284221]))
测试逻辑回归代价函数
# Compute and display cost and gradient with non-zero theta
test_theta = np.array([-24, 0.2, 0.2])
cost_function(test_theta, X, y)
(0.2183301938265977, array([0.04290299, 2.56623412, 2.64679737]))
1.8 高级优化
还有一些高级优化方法,比如共轭梯度法 BFGS (变尺度法) 和 L-BFGS (限制变尺度法) ,就是其中一些更高级的优化算法,它们需要有一种方法来计算 J(theta),以及需要一种方法计算导数项,然后使用比梯度下降更复杂的算法来最小化代价函数
def cost_func(t):
return cost_function(t, X, y)[0]
def grad_func(t):
return cost_function(t, X, y)[1]
# 调用scipy.optimize.fmin_bfgs方法进行高级优化,迭代400次
theta, cost, *unused = opt.fmin_bfgs(f=cost_func, fprime=grad_func, x0=initial_theta, maxiter=400, full_output=True, disp=False)
打印优化后的损失值和theta参数
print('Cost at theta found by fmin: {:0.4f}'.format(cost))
print('Expected cost (approx): 0.203')
print('theta: \n{}'.format(theta))
print('Expected Theta (approx): \n-25.161\n0.206\n0.201')
Cost at theta found by fmin: 0.2035
Expected cost (approx): 0.203
theta:
[-25.16133284 0.2062317 0.2014716 ]
Expected Theta (approx):
-25.161
0.206
0.201
输入变量数大于等于3时才用到,第二部分才用到
def map_feature(x1, x2):
degree = 6
x1 = x1.reshape((x1.size, 1))
x2 = x2.reshape((x2.size, 1))
result = np.ones(x1[:, 0].shape)
for i in range(1, degree + 1):
for j in range(0, i + 1):
result = np.c_[result, (x1**(i-j)) * (x2**j)]
return result
1.9 Decision boundary(决策边界)函数
根据上文优化结果得出的theta为: [-25.16133284 0.2062317 0.2014716 ]
当 theta[0] + theta1* X1 + theta2 * X2 >= 0 时 y为1,即 theta[0] + theta1* X1 + theta2 * X2 = 0为决策分界线
X2 = - ( theta[0] + theta1* X1 ) / theta[2]
def plot_decision_boundary(theta, X, y):
plot_data(X[:, 1:3], y)
if X.shape[1] <= 3:
# 取第一门考试成绩最大最小值坐标
plot_x = np.array([np.min(X[:, 1]) - 2, np.max(X[:, 1]) + 2])
# 计算决策边界线算法通过第一门考试成绩最大最小值和theta计算决策边界线的第二门考试成绩两点坐标,以便绘制决策边界线
# 即通过两点绘制一条直线(决策边界线)
plot_y = (-1/theta[2]) * (theta[1]*plot_x + theta[0])
plt.plot(plot_x, plot_y)
plt.legend(['Decision Boundary', 'Admitted', 'Not admitted'], loc=1)
plt.axis([30, 100, 30, 100])
else:
# Here is the grid range
u = np.linspace(-1, 1.5, 50)
v = np.linspace(-1, 1.5, 50)
z = np.zeros((u.size, v.size))
# Evaluate z = theta*x over the grid
for i in range(0, u.size):
for j in range(0, v.size):
z[i, j] = np.dot(map_feature(u[i], v[j]), theta)
z = z.T
# Plot z = 0
# Notice you need to specify the range [0, 0]
cs = plt.contour(u, v, z, levels=[0], colors='r', label='Decision Boundary')
plt.legend([cs.collections[0]], ['Decision Boundary'])
绘制决策编辑线,观察效果
# Plot boundary
plot_decision_boundary(theta, X, y)
plt.xlabel('Exam 1 score')
plt.ylabel('Exam 2 score')
1.10 预测算法
假设函数
用优化后的theta根据假设函数预测样本分类:
def predict(theta, X):
m = X.shape[0]
hypothesis = sigmoid(np.dot(X, theta))
p = np.where(hypothesis >= 0.5, 1, 0)
return p
预测结果,训练集训练后预测训练集准确率达89%。
正常应该将数据分成三部分:
a、训练集:不同算法训练样本
b、验证集:不同算法在验证集上验证,最小的损失值算法作为最优算法
c、测试集:用最优算法在测试集上验证
p = predict(theta, X)
print('Train accuracy: {}'.format(np.mean(y == p) * 100))
print('Expected accuracy (approx): 89.0')
For a student with scores 45 and 85, we predict an admission probability of 0.7763
Expected value : 0.775 +/- 0.002
Train accuracy: 89.0
Expected accuracy (approx): 89.0
前一篇 EX1 多变量线性回归
后一篇 EX2 正则化逻辑回归