【问题标题】:While calculate SSE getting Error : 'numpy.float64' object is not iterable计算 SSE 时出现错误:“numpy.float64”对象不可迭代
【发布时间】:2017-12-19 09:15:54
【问题描述】:

我正在尝试计算平方误差之和(SSE),下面提到的代码


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

为什么会出现这个错误?我正在使用 Python 3.X.X

【问题讨论】:

  • # 这里我尝试将 y_train_predct 类型的 List 转换为 Series,但是转换为 tuple 而不是 Pandas Data Series。 y_train_predct_srs = pd.Series(y_train_predct)
  • 但是同一行代码,一旦我在单个单元格中运行,它就会将列表转换为 Pandas 系列。 ---- 为什么双重行为?请帮忙。

标签: python numpy jupyter


【解决方案1】:

我看不到你的所有代码,但是,它看起来像

y_train.ilocy_train_predct_srs.iloc

不是列表,实际上是 numpy.float64。您应该检查它们是绝对列表,然后再试一次。

【讨论】:

  • 谢谢@jgd10,问题已经解决,这段代码没有问题,问题是别的。
猜你喜欢
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 2016-12-27
  • 2019-08-03
  • 2017-02-21
  • 2011-12-10
相关资源
最近更新 更多