import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
pdData = pd.read_csv('log_data.txt', header = None,names = ['exam1','exam2','admitted'])
pdData.head()
positive = pdData[pdData['admitted'] == 1] # 正例
negative = pdData[pdData['admitted'] == 0]
fig,ax = plt.subplots(figsize=(10,5))
ax.scatter(positive['exam1'],positive['exam2'],s=30,c='b',marker='o',label='admitted')
ax.scatter(negative['exam1'],negative['exam2'],s=30,c='r',marker='o',label='not admitted')
ax.legend()
plt.show()
ax.set_xlabel('exam1 score')
ax.set_ylabel('exam2 score')
目标:建立分类器(求解出三个参数)
设定阈值,根据阈值判断录取结果
要完成的模块:
1. sigm映射到概率的函数
2. model返回预测的结果
3. cost 根据参数计算损失
4. gradient计算每个参数的梯度方向
5. descent 进行参数更新
6. accuracy: 计算精度
精度
# 设定阈值
def predict(X, theta):
return [1 if x >= 0.5 else 0 for x in model(X, theta)]
scaled_X = scaled_data[:, :3]
y = scaled_data[:, 3]
predictions = predict(scaled_X, theta)
correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 for (a, b) in zip(predictions, y)]
accuracy = (sum(map(int, correct)) % len(correct))
print('accuracy = {0}%'.format(accuracy))
accuracy = 60%
notation: 老版的教程把dataFrame转换成矩阵时,用的.as_matrix(),新版是.value