索引器错误:在python中将结果存储为元组时,索引1超出了大小为1的轴0的界限
我想将结果以元组的形式存储在数据帧中,每个标记中都有(预测、概率)。我可以在以下行精细打印结果: 打印(每个预测,“:”,每个可能性) 我得到了该行的错误信息: 错误消息: temprow[i+1]=(预测[i],概率[i]) 索引器:索引1超出大小为1的轴0的界限索引器错误:在python中将结果存储为元组时,索引1超出了大小为1的轴0的界限,python,machine-learning,computer-vision,artificial-intelligence,imagenet,Python,Machine Learning,Computer Vision,Artificial Intelligence,Imagenet,我想将结果以元组的形式存储在数据帧中,每个标记中都有(预测、概率)。我可以在以下行精细打印结果: 打印(每个预测,“:”,每个可能性) 我得到了该行的错误信息: 错误消息: temprow[i+1]=(预测[i],概率[i]) 索引器:索引1超出大小为1的轴0的界限 from imageai.Prediction import ImagePrediction import os import pandas as pd import numpy as np from PIL import Imag
from imageai.Prediction import ImagePrediction
import os
import pandas as pd
import numpy as np
from PIL import Image
execution_path = os.getcwd()
pred_array = np.empty((0,6),dtype=object)
TEST_PATH = '/home/guest/Documents/Aikomi'
for img in os.listdir(TEST_PATH):
if img.endswith('.jpg'):
image = Image.open(os.path.join(TEST_PATH, img))
image = image.convert("RGB")
image = np.array(image, dtype=np.uint8)
prediction = ImagePrediction()
prediction.setModelTypeAsDenseNet()
prediction.setModelPath(os.path.join(execution_path, "DenseNet.h5"))
prediction.loadModel()
predictions, probabilities = prediction.predictImage(os.path.join(TEST_PATH, img), result_count=5 )
temprow = np.zeros((1,pred_array.shape[1]),dtype=object)
temprow[0] = img
for i in range(len(predictions)):
temprow[i+1] = (predictions[i],probabilities[i])
for eachPrediction, eachProbability in zip(predictions, probabilities):
#print(eachPrediction , " : " , eachProbability)
pred_array = np.append(pred_array,temprow,axis=0)
df = pd.DataFrame(data=pred_array,columns=['File_name','Tag_1','Tag_2','Tag_3','Tag_4','Tag_5'])
print(df)
df.to_csv('Image_tags.csv')
您已定义:
pred_array = np.empty((0,6),dtype=object)
应该是:
pred_array = np.empty((1,6),dtype=object)
零意味着没有轴,但如果得到pred_array.shape,它将显示为(0,6),这可能有点误导
以及:
稍后将创建索引:
for i in range(len(predictions)):
temprow[i+1] = (predictions[i],probabilities[i])
temprow的第一个轴只有一个可访问的索引室,因为您已将其形状定义为(1,pred_数组,形状[1])。numpy数组没有动态大小分配属性,无法在超出范围内进行索引,因此可以为您创建该属性。我已经解决了维度不匹配的问题
pred_array = np.empty((0,6),dtype=object)
for img in os.listdir(TEST_PATH):
if img.endswith('.jpg'):
image = Image.open(os.path.join(TEST_PATH, img))
image = image.convert("RGB")
image = np.array(image, dtype=np.uint8)
predictions, probabilities = prediction.predictImage(os.path.join(TEST_PATH, img), result_count=5)
temprow = np.zeros((1,pred_array.shape[1]),dtype=object)
temprow[0,0] = img
for i in range(len(predictions)):
temprow[0,i+1] = predictions[i]
pred_array = np.append(pred_array,temprow,axis=0)
您认为我应该如何初始化temprow以匹配索引?需要放入其中的元素数:
temprow=np.zeros((len(predictions),pred_array.shape[1]),dtype=object)
pred_array = np.empty((0,6),dtype=object)
for img in os.listdir(TEST_PATH):
if img.endswith('.jpg'):
image = Image.open(os.path.join(TEST_PATH, img))
image = image.convert("RGB")
image = np.array(image, dtype=np.uint8)
predictions, probabilities = prediction.predictImage(os.path.join(TEST_PATH, img), result_count=5)
temprow = np.zeros((1,pred_array.shape[1]),dtype=object)
temprow[0,0] = img
for i in range(len(predictions)):
temprow[0,i+1] = predictions[i]
pred_array = np.append(pred_array,temprow,axis=0)