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

问题描述 投票:0回答:1

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

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个回答
0
投票

好,我明白了!

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

print("Predictions: " + str(lin_reg.predict(x_sample)))
© www.soinside.com 2019 - 2024. All rights reserved.