【问题标题】:TypeError: scatter() got multiple values for argument 's' (Plot Logistic Regression)TypeError: scatter() 为参数 's' 获得了多个值(绘制 Logistic 回归)
【发布时间】: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


【解决方案1】:

问题在于您尝试绘制的形状,即X[y==0]。这里我重新创建了一个 1715x2 形状相同的矩阵,并尝试scatter()它:

import numpy as np
from matplotlib import pyplot as plt

x = np.random.rand(1715, 2)
print(np.shape(x)) # (1715, 2)
plt.scatter(*x, s= 8)

我收到了TypeError: scatter() got multiple values for argument 's'

但是,如果我转置它:

x = np.random.rand(1715, 2)
print(np.shape(x))
plt.scatter(*x.T, s= 8)

plt.show()

我明白了:

【讨论】:

    猜你喜欢
    • 2019-09-15
    • 2019-07-05
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2014-03-12
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多