1

I have a large text file with labels 0 or 1 in each line like this:

1
0
0
1
...

I load it, convert it into a numpy array, and then I want to convert the array into dtype=int64 (as I assume these are strings). I do it like this:

def load_data(infile):
    text_file = open(infile,'r')
    text = text_file.readlines()
    text = map(str.strip,text)
    return text
labels = load_data('labels.txt')
labels_encoded = np.array(labels)
labels_encoded = labels_encoded.astype(int)

It works fine in Python 2.7, and I can work on the array later with my code, but for now I'm stuck with Python 3.6 and when I ran my code, I get an error:

Traceback (most recent call last):
   File "dText.py", line 77, in <module>
   labels_encoded = labels_encoded.astype(int)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'map'

Can anyone help me figure out what is going on here and how to get it to work on Python 3.6? I also tried:

labels_encoded = np.int_(labels_encoded)

but I got the same error. I'm using numpy version 1.13.3. Thanks.

1
  • 1
    What do you think map returns? Are you sure? Commented Jan 3, 2018 at 21:44

2 Answers 2

2

You are passing a map object into the array and trying to convert it. Take a look at the array once it has been created. It looks like this:

array(<map object at 0x127680cf8>, dtype=object)

Try using list(map(...)) instead.

def load_data(infile):
    text_file = open(infile,'r')
    text = text_file.readlines()
    text = list(map(str.strip,text))
    return text
labels = load_data('labels.txt')
labels_encoded = np.array(labels)
labels_encoded = labels_encoded.astype(int)
labels_encoded
array([1, 0, 1, 0])

If you are just making the jump from 2.7 you should note that map no longer returns a list but an iterable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I did not know that, and yes I've been working only with Python 2.7 before. This solves my problem.
0

I had same problem. My code did not work:

train_size = np.ceil(len(dataset) * 0.8).astype(int)
print(type(train_size))  # --> numpy.int32

But this works great:

train_size = int(np.ceil(len(dataset) * 0.8))
print(type(train_size))  # --> int

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.