Numpy String Indexing Array

I have an array of strings

>>> lines array(['RL5\\Stark_223', 'RL5\\Stark_223', 'RL5\\Stark_223', ..., 'RL5\\Stark_238', 'RL5\\Stark_238', 'RL5\\Stark_238'], dtype='|S27') 

Why can I index a row for the first element of an array

 >>> lines[0][0:3] 'RL5' 

But not in the same place for all array elements

 >>> lines[:][0:3] array(['RL5\\Stark_223', 'RL5\\Stark_223', 'RL5\\Stark_223'], dtype='|S27') 

Can anyone suggest a method to get the following result:

array (['RL5', 'RL5', 'RL5', ... 'RL5', 'RL5')

+6
source share
5 answers

To extract the first n characters of each line, you can abuse .astype :

 >>> s = np.array(['RL5\\Stark_223', 'RL5\\Stark_223', 'RL5\\Stark_223']) >>> s array(['RL5\\Stark_223', 'RL5\\Stark_223', 'RL5\\Stark_223'], dtype='|S13') >>> s.astype('|S3') array(['RL5', 'RL5', 'RL5'], dtype='|S3') 
+5
source

Do not forget chararrays!

 lines.view(np.chararray).ljust(3) chararray(['RL5', 'RL5', 'RL5', 'RL5', 'RL5', 'RL5'], dtype='|S3') 

Although its strangely slower:

 #Extend lines to 600000 elements %timeit lines.view(np.chararray).ljust(3) 1 loops, best of 3: 542 ms per loop %timeit np.vectorize(lambda x: x[:3])(lines) 1 loops, best of 3: 239 ms per loop %timeit map(lambda s: s[0:3], lines) 1 loops, best of 3: 243 ms per loop %timeit arr.astype('|S3') 100 loops, best of 3: 4.72 ms per loop 

Maybe because its data duplication, the advantage of this is the dtype of the output array is minimized: S3 vs S64 .

+3
source

try it

 map(lambda s:s[0:3],lines) 
+1
source

You can use numpy vectorize :

 In [11]: np.vectorize(lambda x: x[:3])(lines) Out[11]: array(['RL5', 'RL5', 'RL5', 'RL5', 'RL5', 'RL5'], dtype='|S64') 
0
source

If you are looking for a quick and (somewhat more) flexible way, try:

 lines.view('|S1').reshape(-1, lines.dtype.itemsize)[:, :3].reshape(-1).view('|S3') 

What can be used for more arbitrary slicing and slicing.

Time Information:

 import numpy as np lines = np.array(['RL5\\Stark_223', 'RL5\\Stark_223', 'RL5\\Stark_223', 'RL5\\Stark_238', 'RL5\\Stark_238', 'RL5\\Stark_238'], dtype='|S27').repeat(100000) %timeit lines.view(np.chararray).ljust(3) 1 loop, best of 3: 231 ms per loop %timeit np.vectorize(lambda x: x[:3])(lines) 1 loop, best of 3: 226 ms per loop %timeit map(lambda s: s[0:3], lines) 1 loop, best of 3: 171 ms per loop %timeit lines.astype('|S3') 100 loops, best of 3: 3.58 ms per loop %timeit lines.view('|S1').reshape(-1, lines.dtype.itemsize)[:, :3].reshape(-1).view('|S3') 100 loops, best of 3: 5.16 ms per loop 
0
source

Source: https://habr.com/ru/post/953723/


All Articles