Python-AttributeError:'dict&39;对象没有属性'序列'

2022-04-05 00:00:00 python spyder

问题描述

我有这个错误:AttributeError:‘dict’对象在下面的代码中没有‘Train’属性:

def main(unused_argv):
# Load training and eval data
image_ind = 10
svhn = sio.loadmat('train_32x32.mat')
# access to the dict
x_train = svhn['X']
y_train = svhn['y']
# show sample
plt.imshow(x_train[:,:,:,image_ind])
print(y_train[image_ind])

train_data = svhn.train.images  # Returns np.array
train_labels = np.asarray(svhn.train.labels, dtype=np.int32)

eval_data = sio.loadmat('test_32x32.mat')
# access to the dict
x_test = eval_data['X']
y_test = eval_data['y']
eval_dataSVHN = eval_data.test.images  # Returns np.array
eval_labels = np.asarray(eval_data.test.labels, dtype=np.int32)

执行时,我收到错误:

train_data = svhn.train.images  # Returns np.array
AttributeError: 'dict' object has no attribute 'train'

我如何修复此问题?


解决方案

我看不出有什么问题...scipy.io.loadmatreturns a dictionary,基本词典没有"系列"属性。如果在matlab文件中有一个"系列"变量,那么它将被存储为字典键,这样您就可以svhn['train'](而不是svhn.train)来访问它。

相关文章