使用梯度下降法的逻辑回归

#加载包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
path = 'LogiReg_data.txt'
#header=None:表示文件中的第一行不会被默认设置成列名。
#names用来设置列名
pdData = pd.read_csv(path,header=None,sep = ',',names=['Exam 1','Exam 2','Admitted'])
pdData.head() %head函数默认读取前五行

  

pdData.shape
#数据可视化展示
positive=pdData[pdData['Admitted']==1]
negative=pdData[pdData['Admitted']==0]
fig,ax = plt.subplots(figsize=(10,5))
#plt.subplots()是一个函数,返回一个包含figure和axes对象的元组。
#因此,使用fig,ax = plt.subplots()将元组分解为fig和ax两个变量。
ax.scatter(positive['Exam 1'],positive['Exam 2'],s=30,c='b',marker='o',label='Admitted')
ax.scatter(negative['Exam 1'], negative['Exam 2'], s=30, c='r', marker='x', label='Not Admitted')
ax.legend()
ax.set_xlabel('Exam 1 Score')
ax.set_ylabel('Exam 2 Score')

 

The logistic regression
目标:建立分类器(求解出三个参数012)
设定阈值,根据阈值判断录取结果

要完成的模块
sigmod:映射到概率的函数​


model:返回预测结果值


cost:根据参数计算损失


gradient:计算每个参数的梯度方向


descent:进行参数更新
accuracy:计算精度

 

 

 

#sigmod:映射到概率的函数
def sigmoid(z):
    return 1/(1+np.exp(-z))
#创建一个从-10到10大小为20的向量
nums=np.arange(-10,10,1)
fig,ax = plt.subplots(fig
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值