简体   繁体   English

如何为逻辑回归提供正确的初始猜测和界限?

[英]How to provide the proper initial guess and bounds to logistic regression?

I am trying to implement logistic regression using scipy.optimize in python.我正在尝试在 python 中使用scipy.optimize实现逻辑回归。 When I tried using sklearn, I came to know about the beta values that can be used to predict for the given dataset.当我尝试使用 sklearn 时,我开始了解可用于预测给定数据集的 beta 值。 But when I try to use it in scipy optimize function, the cost value is going to infinity or sometimes the values are fine but the intercept is not there.但是当我尝试在 scipy 优化 function 中使用它时,成本值将趋于无穷大,或者有时值很好但截距不存在。 The reason being, the bounds and initial guess it seems.原因是,它似乎是界限和最初的猜测。 How can it be formulated?如何制定? If there is any other method, Please let me know.如果还有其他方法,请告诉我。

import scipy 
import numpy as np 


def sigmoid(x,beta):
    return((np.exp(np.dot(x, beta))/(1.0 + np.exp(np.dot(x, beta)))))

def mle(beta, x, y):
    return(-(np.sum(y*np.log(sigmoid(x, beta)) + (1-y)*(np.log(1-sigmoid(x,beta))))))

# x0 value ? and bounds ? 
result = scipy.optimize.minimize(mle, x0 = np.array([-.1]), args = (x,y))

There is no intercept in this.这没有拦截。 The shape of dataframe is (100,2). dataframe的形状是(100,2)。 Please advice请指教

I have just added x = np.hstack((np.ones((x.shape[0], 1)), x)), and this solved the problem.我刚刚添加了 x = np.hstack((np.ones((x.shape[0], 1)), x)),这解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM