python:如何使用逻辑回归系数构建sklearn中的决策边界

时间:2017-11-17 02:45:55

标签: python scikit-learn logistic-regression

我想在sklearn中做更多的事情。在这里,我试图生成一个不平衡的分类集,运行逻辑回归,绘制数据点并绘制决策边界线。

为了绘制决策边界线,我首先得到系数:

coef = clf.best_estimator_.coef_
intercept = clf.best_estimator_.intercept_

然后我构建了这条线:

x1 = np.linspace(-8, 10, 100)
x2 = -(coef[0][0] * x1 + intercept[0]) / coef[0][1]
plt.plot(x1, x2, color='#414e8a', linewidth=2)

然而,该线没有绘制,因为x2全部为inf,因为coef [0] [1]等于0.这就是我遇到的问题。为什么这些系数的第二项为0?

以下完整代码:

from sklearn.datasets import make_classification
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import KFold, train_test_split
import numpy as np
import pandas as pd
import warnings

warnings.filterwarnings('ignore')
%pylab inline
pylab.rcParams['figure.figsize'] = (12, 6)
plt.style.use('fivethirtyeight')
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

# Generate data with two classes
X, y = make_classification(class_sep=1.2, weights=[0.1, 0.9], n_informative=3, n_redundant=1, n_features=5, n_clusters_per_class=1, n_samples=10000, flip_y=0, random_state=10)
pca = PCA(n_components=2)
X = pca.fit_transform(X)

y = y.astype('str')
y[y=='1'] ='L'
y[y=='0'] ='S'

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=0)

X_1, X_2 = X_train[y_train=='S'], X_train[y_train=='L']

# Fit a Logistic Regression model
clf_base = LogisticRegression()
grid = {'C': 10.0 ** np.arange(-2, 3),'penalty': ['l1', 'l2']}
cv = KFold(X_train.shape[0], n_folds=5, shuffle=True, random_state=0)
clf = GridSearchCV(clf_base, grid, cv=cv, n_jobs=8, scoring='f1_macro')
clf.fit(X_train, y_train)

# Get coefficients
coef = clf.best_estimator_.coef_
intercept = clf.best_estimator_.intercept_

# Create separation line
x1 = np.linspace(-8, 10, 100)
x2 = -(coef[0][0] * x1 + intercept[0]) / coef[0][1]

plt.scatter(X_1[:,0], X_1[:,1], color='#1abc9c')
plt.scatter(X_2[:,0], X_2[:,1], color='#e67e22')
x_coords = np.concatenate([X_1[:,0],X_2[:,0]])
y_coords = np.concatenate([X_1[:,1],X_2[:,1]])
plt.axis([min(x_coords), max(x_coords), min(y_coords), max(y_coords)])
plt.title("Original Dataset - Fitted Logistic Regression")
plt.plot(x1, x2, color='#414e8a', linewidth=2)
plt.show()

print(coef)

正如您所看到的,coef中的第二项是0.

我在这里做错了什么?

谢谢!

修改

似乎网格搜索参数导致第二个系数为零。例如:

当我将网格参数设置为:

grid = {'C': 10.0 ** np.arange(-2, 3),'penalty': ['l1', 'l2'],'class_weight': ['balanced']}

这给了我两个非零系数

当我删除班级参数时:

grid = {'C': 10.0 ** np.arange(-2, 3),'penalty': ['l1', 'l2']}

这使得我在coef中的第二个元素为零。

希望能够简化问题。那里有人有想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您的第一个系数为零,因为您使用强L1正则化,这会从模型中删除所有不太有用的功能。

您可以使用clf.best_params_查看它 - 它等于{'C': 0.01, 'penalty': 'l1'}。切换到'l2'惩罚,你将使所有系数都为非零。

如果要绘制任意形式的Ax+By+C=0行,可以使用此函数:

import matplotlib.pyplot as plt
import numpy as np

def plot_normal_line(A, B, C, ax=None, **kwargs):
    """ Plot equation of Ax+By+C=0"""
    if ax is None:
        ax = plt.gca()
    if A == 0 and B == 0:
        raise Exception('A or B should be non-zero')
    if B == 0:
        # plot vertical
        ax.vlines(-C / A, *ax.get_ylim(), **kwargs)
    else:
        # plot functoon
        x = np.array(ax.get_xlim())
        y = (A*x+C) / -B
        ax.plot(x, y, **kwargs)

然后命令plot_normal_line(*coef[0], intercept)将绘制您的决策边界。

但是,由于您的数据集是平衡的,对于几乎所有的点,最可能的类是第二个(橙色)。因此,50%概率(粗黑线)的决策边界位于分散的左侧:

enter image description here