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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.