我有以下用于python 2.7的代码,但是当我升级到python 3.6时,map()函数出错了
代码:
mods = ['BPSK', 'QPSK', '8PSK', 'PAM4', 'QAM16', 'QAM64', 'GFSK', 'CPFSK']
# X contain a dataset
p.random.seed(2017)
n_examples = X.shape[0]
n_train = int(n_examples * 0.5)
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))
错误:
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/AMC/classification.py", line 46, in <module>
Y_train = to_onehot(map(lambda x: mods.index(lbl[x][0]), train_idx))
File "C:/Users/user/PycharmProjects/AMC/classification.py", line 42, in to_onehot
yy1 = np.zeros([len(yy), max(yy)+1])
TypeError: object of type 'map' has no len()
我读过此answer,但我不知道如何在代码中修复