【问题标题】:Logictic Regression can't fit my data逻辑回归无法拟合我的数据
【发布时间】:2016-06-25 16:12:49
【问题描述】:

我正在尝试拟合我的数据,但我无法拟合它。数据集

0.50,0
0.75,0
1.00,0
1.25,0
1.50,0
1.75,0
1.75,1
2.00,0
2.25,1
2.50,0
2.75,1
3.00,0
3.25,1
3.50,0
4.00,1
4.25,1
4.50,1
4.75,1
5.00,1
5.50,1

还有我的代码

data = np.loadtxt('dat', delimiter=',',dtype=None);

x=data[:,0:1];
y=data[:,1].reshape(x.size/x[0].size,1);
a=np.ones(shape=(y.size,x[0].size+1));
a[:,1:2]=x;
q=np.ones(shape=(a.shape[1],1));
alpha=0.003

for i in range(500000):
    h=1/(1+np.exp(-np.dot(a,q)))
    for j in range(q.size):
        q[j][0]=q[j][0]-alpha*np.sum((h-y)*a[:,j]);
plt.axis((-1,10,-1,5))
plt.plot(x,y,'x',x,h);
plt.show();

所以我尝试了不同的学习率(alpha),尝试了不同的迭代次数,但我的拟合数据看起来像这样 enter image description here

但它应该看起来像这样enter link description here

我错过了什么?是否有任何逻辑错误或类似的东西?感谢您的交易。

【问题讨论】:

  • 这个问题在交叉验证时可能会得到更好的回答。这不是真正的代码问题,而是实现问题。

标签: python machine-learning logistic-regression


【解决方案1】:

您的算法总体上看起来是正确的,numpy 实现似乎存在一个小问题,当您计算 (h - y)*a 时,您做到了

    q[j][0] = q[j][0] - alpha * np.sum((h - y) * a[:,j])

但是因为您使用的是numpy.ndarray 类型而不是numpy.matrix 类型,所以您应该这样做,例如a[:, j].dot(h - y)

    q[j][0] = q[j][0] - alpha * np.sum(a[:,j].dot(h - y))

这是我在拟合完成后从你的情节中得到的结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-26
    • 2017-03-10
    • 2022-10-04
    • 1970-01-01
    • 2019-02-15
    • 2021-06-29
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多