2017-12-18 259 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

相关问题