【问题标题】:I'm getting a UFuncTypeError when doing a Linear Regression with sklearn使用 sklearn 进行线性回归时出现 UFuncTypeError
【发布时间】:2020-07-01 14:58:46
【问题描述】:

这是一个简单的代码,所以我遗漏了一些很明显的东西:

print(X.dtype)
print(y.dtype)

lin_reg = LinearRegression()
lin_reg.fit(X, y)
print("Score: " + str(lin_reg.score(X,y)))
print("Coefs: " + str(lin_reg.coef_))
print("Intercept: " + str(lin_reg.intercept_))

输出是:

float64
int64
Score: 0.8725949819648744
Coefs: [[825.09663073]]
Intercept: [-122.41197463]

现在,问题是当我尝试预测时。首先我得到一个样本:

x_sample = X[:144]
y_sample = y[:144]

print("Predictions: " + lin_reg.predict(x_sample))

这给了我这个我不明白的错误:

---------------------------------------------------------------------------
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-791-1d189ee47b31> in <module>
      2 y_sample = y[:144]
      3 
----> 4 print("Predictions: " + lin_reg.predict(x_sample))

UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')

【问题讨论】:

    标签: python-3.x scikit-learn linear-regression


    【解决方案1】:

    好的,我明白了!

    问题与数组无关。它在打印方法中:

    print("Predictions: " + str(lin_reg.predict(x_sample)))
    

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 2018-08-11
      • 2015-06-19
      • 2016-10-05
      • 2020-07-08
      • 2021-02-20
      • 2014-07-06
      • 2018-05-16
      相关资源
      最近更新 更多