【发布时间】:2021-10-25 03:29:36
【问题描述】:
我正在尝试为逻辑回归绘制决策边界,但出现此错误:
TypeError: scatter() 为参数 's' 获得了多个值
这是我的代码:
logreg = LogisticRegression()
logreg.fit(X_train,y_train)
b = logreg.intercept_[0]
w1, w2 = logreg.coef_.T
c = -b/w2
m = -w1/w2
# Plot the data and the classification with the decision boundary.
xmin, xmax = -1, 2
ymin, ymax = -1, 2.5
xd = np.array([xmin, xmax])
yd = m*xd + c
plt.plot(xd, yd, 'k', lw=1, ls='--')
plt.fill_between(xd, yd, ymin, color='tab:blue', alpha=0.2)
plt.fill_between(xd, yd, ymax, color='tab:orange', alpha=0.2)
plt.scatter(*X[y==0].T, s=8, alpha=0.5)
plt.scatter(*X[y==1].T, s=8, alpha=0.5)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.show()
由于我是初学者,我还想知道是否有可能绘制我的逻辑回归,包括所有特征或其他内容(2 个最重要的特征)。我必须在这里只选择 2 列。
编辑:我也收到此错误:
TypeError: scatter() 接受 2 到 13 个位置参数,但给出了 1715 个
供您参考:
X = df
X = X.drop(columns=['answer']) #features
y = df['answer'] #target
X.shape # (4948, 2)
y.shape # (4948,)
X[y==0].shape # (1715,2)
任何帮助将不胜感激!谢谢。
【问题讨论】:
-
什么是
X?它在哪里定义?另外,y是什么? -
@liorr X 是一个仅包含两个特征的 df。我应该将其重塑为 numpy 吗? y 是我的目标变量,我刚刚编辑了我的帖子,包括它们的形状
-
我不能肯定地说,因为你没有包括你试图绘制的变量的定义,但我最清楚问题在于它们的形状。
X[y==0]的形状是什么? -
@liorr X[y==0] 的形状:(1715, 2)
标签: python matplotlib machine-learning logistic-regression