Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy taking only first character of string

Tags:

python

numpy

Following is the simplified version of my problem. I want to create a (N, 1) shape numpy array, which would have strings as their values. However, when I try to insert the string, only the first character of the string gets inserted.

What am I doing wrong here?

>>> import numpy as np
>>> N = 23000
>>> Y = np.empty((N, 1), dtype=str)
>>> Y.shape
(23000, 1)
>>> for i in range(N):
...     Y[i] = "random string"
...
>>> Y[10]
array(['r'], dtype='<U1')
like image 636
Parthapratim Neog Avatar asked Mar 27 '19 12:03

Parthapratim Neog


1 Answers

By default data type str takes length of 1. So, you will only get one character. we can set max data length by using np.dtype('U100'). Un where U is unicode and n is number of characters in it.

Try below code

>>> import numpy as np
>>> N = 23000
>>> Y = np.empty((N, 1), dtype=np.dtype('U100'))
>>> Y.shape
(23000, 1)
>>> for i in range(N):
...     Y[i] = "random string"
...
>>> Y[10]
array(['random string'], dtype='<U100')
like image 115
anjaneyulubatta505 Avatar answered Oct 19 '22 21:10

anjaneyulubatta505