【发布时间】:2017-04-24 08:53:48
【问题描述】:
我正在尝试在 scikit learn 中绘制逻辑回归的决策边界
features_train_df : 650 columns, 5250 rows
features_test_df : 650 columns, 1750 rows
class_train_df = 1 column (class to be predicted), 5250 rows
class_test_df = 1 column (class to be predicted), 1750 rows
分类代码;
tuned_logreg = LogisticRegression(penalty = 'l2', tol = 0.0001,C = 0.1,max_iter = 100,class_weight = "balanced")
tuned_logreg.fit(x_train[sorted_important_features_list[0:650]].values, y_train['loss'].values)
y_pred_3 = tuned_logreg.predict(x_test[sorted_important_features_list[0:650]].values)
我得到了分类器代码的正确输出。
在线获取此代码:
code:
X = features_train_df.values
# evenly sampled points
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 50),
np.linspace(y_min, y_max, 50))
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
#plot background colors
ax = plt.gca()
Z = tuned_logreg.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
cs = ax.contourf(xx, yy, Z, cmap='RdBu', alpha=.5)
cs2 = ax.contour(xx, yy, Z, cmap='RdBu', alpha=.5)
plt.clabel(cs2, fmt = '%2.1f', colors = 'k', fontsize=14)
# Plot the points
ax.plot(Xtrain[ytrain == 0, 0], Xtrain[ytrain == 0, 1], 'ro', label='Class 1')
ax.plot(Xtrain[ytrain == 1, 0], Xtrain[ytrain == 1, 1], 'bo', label='Class 2')
# make legend
plt.legend(loc='upper left', scatterpoints=1, numpoints=1)
错误:
ValueError: X has 2 features per sample; expecting 650
请建议我哪里出错了
【问题讨论】:
-
您的逻辑回归分类器代码在哪里?我想问题出在分类器的预测方法上。
-
@WasiAhmad 我已经添加了分类器代码,但我没有收到任何错误。
-
你能解释一下你想用这个语句做什么 -
ax.plot(Xtrain[ytrain == 0, 0], Xtrain[ytrain == 0, 1], 'ro', label='Class 1')? -
@WasiAhmad 我不确定,从 scikit 获取代码 - 学习和 stackoverflow.com/questions/28256058/…
-
我理解你的代码错误,很抱歉。你能告诉我你在哪一行出错吗?或者你能提供你的完整代码以便我调试。
标签: python matplotlib scikit-learn