Coefficients from Logistic Regression using Scikit-Learn


I was trying to implement a model to distinguish between low or high pass filters acting on a white noise signal by using Scikit Learn's logistic regression. It seems to be working fine but when I extract the parameters b=intercept_, and m=coef_ and use them to plot 1/(1+np.exp(-m*x-b), the plot differs from when I use the predict function of the logistic regression. I can get it to be exactly by multiplying everything in np.exp() by an arbitrarily large number but I am not sure what the exact value is. My aim was to use the intercept_ parameter as an estimate for the cut-off frequency that's why I need the exact value of the parameter, the other one is just for determining between high and low pass so all I need is the sign. Anyone by any chance know what it is? I looked everywhere but I still cannot seem to find the answer.

Thank you for your time.


Posted 2018-03-19T21:09:49.350

Reputation: 109

This question was answered on the stats SE where I also posted it.

– Jepsilon – 2018-03-20T22:11:20.607



Can you post some sample code of what you're trying to do? I can match predict_proba exactly when using the coefficients and intercept.

xmin, xmax = -5, 5
n_samples = 100
X = np.random.normal(size=n_samples)
y = (X > 0).astype(np.float)
X[X > 0] *= 4
X += .3 * np.random.normal(size=n_samples)

X = X[:, np.newaxis]
x_train, x_test, y_train, y_test = train_test_split(X,y)
clf = linear_model.LogisticRegression(C=1e5), y_train)

def model(x):
    return 1 / (1 + np.exp(-x))

model(x_test * clf.coef_ + clf.intercept_).ravel()


Posted 2018-03-19T21:09:49.350

Reputation: 2 027


In logistic regression, we don't fit a linear line to our data points. Instead, we fit a linear line to the log-likelihood of our data point. Therefore, you need to take exp to fit it accurately.

Vivek Khetan

Posted 2018-03-19T21:09:49.350

Reputation: 353