gpt4 book ai didi

python - 人口必须是一个序列或集合。对于字典,使用 list(d)

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:15 30 4
gpt4 key购买 nike

我尝试执行这段代码,但出现以下错误,随机函数出现错误,我不知道如何解决,请帮助我。

def load_data(sample_split=0.3, usage='Training', to_cat=True, verbose=True,
classes=['Angry','Happy'], filepath='C:/Users/Oussama/Desktop/fer2013.csv'):
df = pd.read_csv(filepath)
# print df.tail()
# print df.Usage.value_counts()
df = df[df.Usage == usage]
frames = []
classes.append('Disgust')
for _class in classes:
class_df = df[df['emotion'] == emotion[_class]]
frames.append(class_df)
data = pd.concat(frames, axis=0)
rows = random.sample(data.index, int(len(data)*sample_split))
data = data.ix[rows]
print ('{} set for {}: {}'.format(usage, classes, data.shape))
data['pixels'] = data.pixels.apply(lambda x: reconstruct(x))
x = np.array([mat for mat in data.pixels]) # (n_samples, img_width, img_height)
X_train = x.reshape(-1, 1, x.shape[1], x.shape[2])
y_train, new_dict = emotion_count(data.emotion, classes, verbose)
print (new_dict)
if to_cat:
y_train = to_categorical(y_train)
return X_train, y_train, new_dict

我明白了:

Traceback (most recent call last):
File "fer2013datagen.py", line 71, in <module>
verbose=True)
File "fer2013datagen.py", line 47, in load_data
rows = random.sample(data, int(len(data)*sample_split))

File"
C:\Users\Oussama\AppData\Local\Programs\Python\Python35\lib\random.py",
line 311, in sample
raise TypeError("Population must be a sequence or set. For dicts, use
list(d).")
TypeError: Population must be a sequence or set. For dicts, use list(d).

最佳答案

你的代码在这里:

rows = random.sample(data.index, int(len(data)*sample_split))

但是,错误信息显示

rows = random.sample(data, int(len(data)*sample_split))

为什么不同?你修改了吗? 数据的类型是什么?是 list 吗?还是听写?

而且,错误消息已经告诉你如何修复它。这意味着 random.sample 的第一个参数必须是一个序列或集合。对于字典,使用 list(Dict)。

例如,

d = {'a':1,'b':2}
random.sample(list(d), 1)

代替

random.sample(d, 1)

关于python - 人口必须是一个序列或集合。对于字典,使用 list(d),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372231/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com