2017-12-18 115 views
-2

我試圖計算方誤差之和(SSE),下面提到雖然計算SSE收到錯誤:「numpy.float64」對象不是可迭代


def SSEadver(tv_train,radio_train,newsppr_train,y_train): 
y_train_predct = [] 
sse_train = 0 
y_train_predct_srs = 0 

# Calculating the predicted sales values on training data 
for i in range(0,len(tv_train)): 
    y_train_predct.append(2.8769666223179353 + (0.04656457* tv_train.iloc[i])+ (0.17915812*(radio_train.iloc[i])) + (0.00345046*(newsppr_train.iloc[i]))) 

# ** Here I Convert y_train_predct's type List to Series, but still it is showing type as list** 
y_train_predct_srs = pd.Series(y_train_predct) 

# *** Due above converting not working here y_train_predct_srs.iloc[j]) is not working*** 
# Now calculate SSE (sum of Squared Errors) 
for j in range (len(y_train)): 
    sse_train += sum((y_train.iloc[j] - y_train_predct_srs.iloc[j])**2) 

return y_train_predct, y_train_predct_srs 

sse_train = SSEadver(tv_train,radio_train,newsppr_train, y_train) 

代碼,而我運行此代碼我收到錯誤:


TypeError         Traceback (most recent call last) 
<ipython-input-446-576e1af02461> in <module>() 
20  return y_train_predct, y_train_predct_srs 
21 
    ---> 22 sse_train = SSEadver(tv_train,radio_train,newsppr_train, y_train) 
    23 

    <ipython-input-446-576e1af02461> in SSEadver(tv_train, radio_train, newsppr_train, y_train) 
14  # Now calculate SSE (sum of Squared Errors) 
15  for j in range (len(y_train)): 
---> 16 sse_train += sum((y_train.iloc[j] -   y_train_predct_srs.iloc[j])**2) 
17 
18 

TypeError: 'numpy.float64' object is not iterable 

爲什麼我收到THI錯誤?我使用Python 3.X.X

+0

['numpy.float64'對象的可能重複是不可迭代的 - meanshift集羣](https://stackoverflow.com/questions/39048355/numpy-float64-object-is-not-iterable-meanshift-clustering) – Sombrero

+0

#這裏我嘗試將y_train_predct類型List轉換爲Series,但它被轉換爲元組而不是Pandas Data Series。 ** y_train_predct_srs = pd.Series(y_train_predct)** – user3631357

+0

但是相同的代碼行,一旦我運行在一個單獨的單元格中,它將轉換列表爲Pandas Series。 ----爲什麼雙行爲?請幫忙。 – user3631357

回答

0

我不能看到所有的代碼,但是,它看起來像任何

y_train.ilocy_train_predct_srs.iloc

不是列表,但實際上numpy.float64。你應該檢查他們是絕對是列表,然後再試一次。

+0

謝謝@ jgd10,問題已解決,此代碼沒有問題,問題是別的。 – user3631357

相關問題