需要将数组元素添加到列表中。尝试使用"tolist"、"extend"和"append",但没有成功



我正在尝试创建一个具有image_id然后几个预测百分比的列表。 预测百分比 (yhat( 位于 np 数组中。 我认为使用"tolist"然后"extend"然后"append"应该有效。 我一定是做错了什么。 非常感谢您的任何帮助!

# Predict the probability across all output classes.
yhat = model.predict(image_array) #the image predictions
yhat.tolist() #change array to list
row = [i[0]] #the image id 
row.extend(yhat) #add list elements to row
result_yhat.append(row) #append row to result_yhat

结果在数组中仍具有预测数字。

result_yhat
[['5', array([6.78813876e-07, 1.14646399e-08, 5.51704140e-08, 
    9.05712838e-08,...

我认为您想将yhat.tolist()的结果分配给变量并将其传递给append().

否则,您只是将 np 数组传递给 append()

yhat = yhat.tolist() #change array to list
row = [i[0]] #the image id 
row.extend(yhat) #add list elements to row