2

I have the following code that used to work fine with python 2.7 but when i upgraded to python 3.6 i got an error with the map() function

code:

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))

the error:

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()

I read this answer but i don't understand how to fix in my code

A.SDR
  • 177
  • 2
  • 11
  • `Y_train = to_onehot(list(map(lambda x: mods.index(lbl[x][0]), train_idx)))` – Rakesh Mar 18 '19 at 17:30
  • `map` doesn't return a `list` in Python 3; it returns a `map` object that is iterable, with the function applied to each element of the original list only as you request the corresponding result. – chepner Mar 18 '19 at 17:30
  • Change `to_onehot(map(...))` to `to_onehot(list(map(...)))`. – Aran-Fey Mar 18 '19 at 17:30

0 Answers0