目前我正在做基于深度学习的调制识别方法的课题,采用的是RML2016.10a数据集,在用Python3.5版本运行一段代码时出现了以下问题:
源代码是:
# Partition the data
# into training and test sets of the form we can train/test on
# while keeping SNR and Mod labels handy for each
np.random.seed(2016)
n_examples = X.shape[0]
n_train = n_examples * 1//2
train_idx = np.random.choice(range(0,n_examples), size=n_train, replace=False)
test_idx = list(set(range(0,n_examples))-set(train_idx))
X_train = X[train_idx]
X_test = X[test_idx]
def to_onehot(yy):
yy1 = np.zeros([len(yy), max(yy)+1])
yy1[np.arange(len(yy)),yy] = 1
return yy1
Y_train = to_onehot(map(lambda x: mods.index(lbl[x][0]), train_idx))
Y_test = to_onehot(map(lambda x: mods.index(lbl[x][0]), test_idx))
运行之后出现错误:
TypeError Traceback (most recent call last)
<ipython-input-50-7b85c17b80b7> in <module>()
13 yy1[np.arange(len(yy)),yy] = 1
14 return yy1
---> 15 Y_train = to_onehot(map(lambda x: mods.index(lbl[x][0]), train_idx))
16 Y_test = to_onehot(map(lambda x: mods.index(lbl[x][0]), test_idx))
17
<ipython-input-50-7b85c17b80b7> in to_onehot(yy)
10 X_test = X[test_idx]
11 def to_onehot(yy):
---> 12 yy1 = np.zeros([len(yy), max(yy)+1])
13 yy1[np.arange(len(yy)),yy] = 1
14 return yy1
TypeError: object of type 'map' has no len()
想亲各位博客帮我看一看。帮我解答一下我的问题
Y_train = to_onehot(list(map(lambda x: mods.index(lbl[x][0]), train_idx)))#将map转换成list,len(list(map(a)))
作者的代码:
# 作者的代码
# Y_train = to_onehot(map(lambda x: mods.index(lbl[x][0]), train_idx))
# Y_test = to_onehot(map(lambda x: mods.index(lbl[x][0]), test_idx))
修改后的代码:
#修改后的代码
trainy = list(map(lambda x: mods.index(lbl[x][0]), train_idx))
Y_train = to_onehot(trainy)
Y_test = to_onehot(list(map(lambda x: mods.index(lbl[x][0]), test_idx)))