簡體   English   中英

使用 sklearn 進行線性回歸時出現 UFuncTypeError

[英]I'm getting a UFuncTypeError when doing a Linear Regression with sklearn

這是一個簡單的代碼,所以我遺漏了一些非常明顯的內容:

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

好,我知道了!

問題與數組無關。 它在打印方法中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM