逻辑回归
在训练的初始阶段,我们将要构建一个逻辑回归模型来预测,某个学生是否被大学录取。
设想你是大学相关部分的管理者,想通过申请学生两次测试的评分,来决定他们是否被录取。
现在你拥有之前申请学生的可以用于训练逻辑回归的训练样本集。对于每一个训练样本,你有他们两次测试的评分和最后是被录取的结果
数据可视化
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#读取数据
path = 'ex2data1.txt'
data = pd.read_csv(path,header = None, names=['Exam1','Exam2','Admitted'])
data.head()
Exam1 | Exam2 | Admitted | |
---|---|---|---|
0 | 34.623660 | 78.024693 | 0 |
1 | 30.286711 | 43.894998 | 0 |
2 | 35.847409 | 72.902198 | 0 |
3 | 60.182599 | 86.308552 | 1 |
4 | 79.032736 | 75.344376 | 1 |
#可视化
positive = data[data['Admitted']==1]
negative = data[data['Admitted']==0]
fig,ax = plt.subplots(figsize=(12,8))
ax.scatter(positive['Exam1'],positive['Exam2'],s=50,c='b',marker = 'o',label='Admitted')
ax.scatter(negative['Exam1'],negative['Exam2'],s=50,c='r',marker = '*',label='Not Admitted')
ax.legend()
ax.set_xlabel('Exam1 Score')
ax.set_ylabel('Exam2 Score')
plt.show()
逻辑回归实现
sigmoid函数
def sigmoid(z):
return 1/(1+np.exp(-z))
代价函数和梯度
def cost(theta,X,y):
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
left = np.multiply(-y,np.log(sigmoid(X*theta.T)))
right = np.multiply((1-y),np.log(1-sigmoid(X*theta.T)))
return np.sum(left-right)/(len(X))
# 初始化X,y,theta
data.insert(0,'Ones',1)
cols = data.shape[1]
X = data.iloc[:,:cols-1]
y = data.iloc[:,cols-1:cols]
theta = np.zeros(3)
X = np.array(X.values)
y = np.array(y.values)
#确认三个矩阵的维度
X.shape,y.shape,theta.shape
((100, 3), (100, 1), (3,))
cost(theta,X,y)
0.6931471805599453
#实现梯度计算但是不更新theta
def gradient(theta,X,y):
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
fo