根据考试成绩预测是否录取,经典二分类问题

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: 计算精度

 g(z)=\frac{1}{1+e^{-z}}

精度

# 设定阈值
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值