逻辑回归手写(初步实现)

逻辑回归手写(初步实现)

import numpy as np


def sig_mod(inx):
    return 1/(1 + np.exp(-inx))


def j():
    return 1/len(data)*(-y.T@np.log(sig_mod(x@weights)) - (1 - y).T@np.log(1 - sig_mod(x@weights)))


alpha = float(input())
data = np.loadtxt(r'D:\data\machine-learning-ex2\machine-learning-ex2\ex2\ex2data2.txt', delimiter=',')
x = np.hstack((np.ones((len(data), 1)), data[:, [0, 1]]))
y = data[:, [2]]
weights = np.ones((data.shape[1], 1))
print(weights)
for _ in range(1500):
    h = sig_mod(x@weights)
    error = y - h
    weights = weights + alpha*(x.T@error)
print(weights)

C:\Users\G3\Anaconda3\python.exe D:/test/word/logic.py
0.01
[[1.]
 [1.]
 [1.]]
[[0.93903696]]
[[-0.01418412]
 [-0.30352113]
 [-0.01813178]]
[[0.69024112]]

Process finished with exit code 0

测试样例一,超参数明显改变,代价函数从0.94降到了0.7。未做其他检测,不知道拟合效果如何。(感觉一般)

0.01
[[1.]
D:/test/word/logic.py:9: RuntimeWarning: divide by zero encountered in log
 [1.]
 [1.]]
[[nan]]
  return 1/len(data)*(-y.T@np.log(sig_mod(x@weights)) - (1 - y).T@np.log(1 - sig_mod(x@weights)))
D:/test/word/logic.py:9: RuntimeWarning: invalid value encountered in matmul
  return 1/len(data)*(-y.T@np.log(sig_mod(x@weights)) - (1 - y).T@np.log(1 - sig_mod(x@weights)))
[[-114.26013395]
 [  36.59101548]
 [ -11.95281657]]
[[nan]]

Process finished with exit code 0

测试样例2,超参数明显改变,代价函数因为log0而报错。不知道拟合效果。(感觉不理想,或者说可能根本不拟合?)还可优化。

果然造轮子很麻烦。。

附上大佬代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值