【问题标题】:Logistic regression: objects are not aligned逻辑回归:对象未对齐
【发布时间】:2014-11-10 21:40:41
【问题描述】:

我正在尝试对来自 A Ng 的 coursera 机器学习课程的 this dataset 进行逻辑回归。

我们的想法是我们有一个成本函数,我们需要将其最小化以找到参数 theta。

import numpy as np
from scipy.optimize import fmin_bfgs

data = np.loadtxt('ex2data1.txt',delimiter=",")
m,n = data.shape
X = np.array(np.column_stack((np.ones(m),data[:,:-1])))
y = np.array(data[:,2].reshape(m,1))
theta = np.array(np.zeros(n).reshape(n,1))

def sigmoid(z):
    return 1/(1+np.exp(-z))

def hypothesis(X,theta):
    return sigmoid( X.dot(theta) )

def cost(theta):
    print theta.shape
    h = hypothesis(X,theta)
    cost = (-y.T.dot(np.log(h))-(1-y).T.dot(np.log(1-h)))/m
    return cost

def gradient(theta):
    h = hypothesis(X,theta)
    grad = ((h-y).T.dot(X)).T/m
    return grad.flatten()

def fmin():
    initial_theta=np.zeros(n).reshape(n,1)
    theta=fmin_bfgs(cost,initial_theta,fprime=gradient)
    return theta

打印 fmin()

我收到了ValueError: Objects are not aligned,但我检查了所有实体的形状,但仍然无法弄清楚。这是回溯:

---> 32     theta=fmin_bfgs(cost,initial_theta,fprime=gradient)
     33 

/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.pyc in fmin_bfgs(f, x0, fprime, args, gtol, norm, epsilon, maxiter, full_output, disp, retall, callback)
    775             'return_all': retall}
    776 
--> 777     res = _minimize_bfgs(f, x0, args, fprime, callback=callback, **opts)
    778 
    779     if full_output:

/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.pyc in _minimize_bfgs(fun, x0, args, jac, callback, gtol, norm, eps, maxiter, disp, return_all, **unknown_options)
    844     gnorm = vecnorm(gfk, ord=norm)
    845     while (gnorm > gtol) and (k < maxiter):
--> 846         pk = -numpy.dot(Hk, gfk)
    847         try:
    848             alpha_k, fc, gc, old_fval, old_old_fval, gfkp1 = \

ValueError: objects are not aligned

【问题讨论】:

    标签: python numpy machine-learning scipy


    【解决方案1】:

    我修改了你的代码,它可以得到与 c=inf sklearn 中的 LogisticRegression 相同的结果:

    import numpy as np
    from scipy.optimize import fmin_bfgs
    import io
    data = np.loadtxt('ex2data1.txt',delimiter=",")
    m,n = data.shape
    X = np.array(np.column_stack((np.ones(m),data[:,:-1])))
    y = np.array(data[:,2].reshape(m,1))
    theta = np.array(np.zeros(n).reshape(n,1))
    
    def sigmoid(z):
        return 1/(1+np.exp(-z))
    
    def hypothesis(X,theta):
        return sigmoid( X.dot(theta) )
    
    def cost(theta):
        h = hypothesis(X,theta)
        cost = (-y.T.dot(np.log(h))-(1-y).T.dot(np.log(1-h)))/m
        r = cost[0]
        if np.isnan(r):
            return np.inf
        return r
    
    def gradient(theta):
        theta = theta.reshape(-1, 1)
        h = hypothesis(X,theta)
        grad = ((h-y).T.dot(X)).T/m
        return grad.flatten()
    
    def fmin():
        initial_theta=np.zeros(n)
        theta=fmin_bfgs(cost,initial_theta,fprime=gradient)
        return theta
    
    theta = fmin()
    

    【讨论】:

    • 谢谢。我知道成本可能是nan。但是 reshape(-1,1) 是什么意思?文档告诉我“一个形状维度可以是-1。在这种情况下,该值是从数组的长度和剩余维度推断出来的。”我不明白。
    猜你喜欢
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 2020-12-18
    • 2020-01-31
    • 2016-12-24
    • 2022-11-27
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多