使用sklearn进行线性回归时出现UFuncTypeError

时间:2020-03-20 13:01:49

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

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

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')

1 个答案:

答案 0 :(得分:0)

好的,我明白了!

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

print("Predictions: " + str(lin_reg.predict(x_sample)))
相关问题