File "D:\403\myworld\csv2Pic.py", line 36, in data2Pic data = list(map(dataDict[dataIndex],int)) TypeError: 'type' object is not iterable
时间: 2024-03-28 08:38:25 浏览: 78
这个错误是因为在`map()`函数中,第二个参数使用了错误的数据类型。`map()`函数的第一个参数应该是一个函数,第二个参数应该是一个可迭代对象。但是在这个错误的行中,第一个参数是`dataDict[dataIndex]`,它是一个函数,而第二个参数是`int`,它是一个类型。因此,Python解释器会认为您尝试将`int`类型视为可迭代对象并引发异常。
要解决这个问题,请将第二个参数更改为一个可迭代对象,例如列表或元组。例如,您可以将这行代码更改为`data = list(map(dataDict[dataIndex], [int]))`。
相关问题
import pickle import numpy as np import os # from scipy.misc import imread def load_CIFAR_batch(filename): with open(filename, 'rb') as f: datadict = pickle.load(f, encoding='bytes') X = datadict[b'data'] Y = datadict[b'labels'] X = X.reshape(10000, 3, 32, 32).transpose(0, 2, 3, 1).astype("float") Y = np.array(Y) return X, Y def load_CIFAR10(ROOT): xs = [] ys = [] for b in range(1, 2): f = os.path.join(ROOT, 'data_batch_%d' % (b,)) X, Y = load_CIFAR_batch(f) xs.append(X) ys.append(Y) Xtr = np.concatenate(xs) Ytr = np.concatenate(ys) del X, Y Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch')) return Xtr, Ytr, Xte, Yte def get_CIFAR10_data(num_training=5000, num_validation=500, num_test=500): cifar10_dir = r'D:\daima\cifar-10-python\cifar-10-batches-py' X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir) print(X_train.shape) mask = range(num_training, num_training + num_validation) X_val = X_train[mask] y_val = y_train[mask] mask = range(num_training) X_train = X_train[mask] y_train = y_train[mask] mask = range(num_test) X_test = X_test[mask] y_test = y_test[mask] mean_image = np.mean(X_train, axis=0) X_train -= mean_image X_val -= mean_image X_test -= mean_image X_train = X_train.transpose(0, 3, 1, 2).copy() X_val = X_val.transpose(0, 3, 1, 2).copy() X_test = X_test.transpose(0, 3, 1, 2).copy() return { 'X_train': X_train, 'y_train': y_train, 'X_val': X_val, 'y_val': y_val, 'X_test': X_test, 'y_test': y_test, } def load_models(models_dir): models = {} for model_file in os.listdir(models_dir): with open(os.path.join(models_dir, model_file), 'rb') as f: try: models[model_file] = pickle.load(f)['model'] except pickle.UnpicklingError: continue return models这是一个加载cifar10数据集的函数,如何修改使其能加载mnist数据集,不使用TensorFlow
可以使用以下代码加载MNIST数据集:
```
import gzip
import os
import pickle
import numpy as np
def load_mnist(path, kind='train'):
labels_path = os.path.join(path, '%s-labels-idx1-ubyte.gz' % kind)
images_path = os.path.join(path, '%s-images-idx3-ubyte.gz' % kind)
with gzip.open(labels_path, 'rb') as lbpath:
labels = np.frombuffer(lbpath.read(), dtype=np.uint8, offset=8)
with gzip.open(images_path, 'rb') as imgpath:
images = np.frombuffer(imgpath.read(), dtype=np.uint8, offset=16).reshape(len(labels), 784)
return images, labels
def get_mnist_data(num_training=5000, num_validation=500, num_test=500):
mnist_dir = r'D:\daima\mnist' # 修改为mnist数据集所在的目录
X_train, y_train = load_mnist(mnist_dir, kind='train')
X_test, y_test = load_mnist(mnist_dir, kind='t10k')
print(X_train.shape)
mask = range(num_training, num_training + num_validation)
X_val = X_train[mask]
y_val = y_train[mask]
mask = range(num_training)
X_train = X_train[mask]
y_train = y_train[mask]
mask = range(num_test)
X_test = X_test[mask]
y_test = y_test[mask]
X_train = X_train.astype('float32') / 255
X_val = X_val.astype('float32') / 255
X_test = X_test.astype('float32') / 255
return {
'X_train': X_train,
'y_train': y_train,
'X_val': X_val,
'y_val': y_val,
'X_test': X_test,
'y_test': y_test,
}
```
这个函数将会返回训练集、验证集和测试集的图像和标签。其中,图像是一个形如`(num_samples, 784)`的数组,标签是一个形如`(num_samples,)`的数组。
def load_CIFAR_batch(filename): """ load single batch of cifar """ with open(filename, 'rb') as f: datadict = pickle.load(f) X = datadict['data'] Y = datadict['labels'] X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float") Y = np.array(Y) return X, Y显示'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)
这段代码是用来加载CIFAR-10数据集的一个batch。具体来说,它打开一个二进制文件,读取其中的数据和标签,并对数据进行reshape和转置操作,最后返回数据和标签的numpy数组。其中的pickle.load()函数是Python标准库中的一个模块,用于将Python对象序列化为字节流,以便于存储或传输,而load_CIFAR_batch()函数则使用了这个模块来加载一个二进制文件中的数据。显示的错误信息可能是因为该文件不是一个合法的二进制文件。
阅读全文