希望这不是一个愚蠢的问题,但是我遇到了一些问题,试图利用range()
循环从保存的机器学习模型中进行计算,然后将所有计算值累加到结束。每次执行range():
循环时,都会返回一个estimatedKwh
的numpy数组。我试图将值存储在total_estKwh
中,并且如果我从total_estKwh
的零开始,如何附加total_estKwh
...然后在range()
循环完成后求和?
total_rows = len(data.index)
row_num = 0
total_estKwh = 0
for i in range(total_rows):
params = np.array(data.iloc[row_num])
if (params.ndim == 1):
params = np.array([params])
estimatedKwh = load_trained_model(weights_path).predict(params)
print("Estimated kWH:", int(estimatedKwh))
estimatedKwh = pd.DataFrame(estimatedKwh)
estimatedKwh.append(total_estKwh)
row_num += 1
#total_estKwh = pd.DataFrame(total_estKwh)
#print("FINISHED, total Estimated kWH:", total_estKwh)
print(total_estKwh)
代码不起作用...这是我正在尝试的内容...任何提示都可以帮助您
Using TensorFlow backend.
Estimated kWH: 1083
Traceback (most recent call last):
File "C:\Users\bbartling\Desktop\EC\ecPredic2.py", line 70, in <module>
estimatedKwh.append(total_estKwh)
File "C:\Users\bbartling\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 6211, in append
sort=sort)
File "C:\Users\bbartling\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\reshape\concat.py", line 225, in concat
copy=copy, sort=sort)
File "C:\Users\bbartling\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\reshape\concat.py", line 286, in __init__
raise TypeError(msg)
TypeError: cannot concatenate object of type "<class 'int'>"; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid
>>>
答案 0 :(得分:1)
(因为评论中的讨论似乎无济于事...)
您不能附加整数,但可以附加列表;因此,只需忘记熊猫数据框并使用列表即可:
1)将total_estKwh
初始化为
total_estKwh = []
2)删除estimatedKwh = pd.DataFrame(estimatedKwh)
3)将estimatedKwh.append(total_estKwh)
替换为
total_estKwh.append(estimatedKwh)
如果您确实想以熊猫daraframe结尾,可以随时在for
循环之后转换列表...
答案 1 :(得分:1)
取决于您实际要实现的目标可能是一种方法。
data = pd.DataFrame(np.arange(100))
all_estimates =[]
for i, row in data.iterrows():
params = row.values
if (params.ndim == 1):
params = np.array([params])
#estimatedKwh = load_trained_model(weights_path).predict(params)
estimatedKwh = np.array([1.2]) # A dummy value for this example
all_estimates.append(estimatedKwh)
df = pd.DataFrame(all_estimates)
total_estKwh = df.sum() # Total for all_estimates