Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I get a big array (image with 12 Mpix) in the array format from the python standard lib. Since I want to perform operations on those arrays, I wish to convert it to a numpy array. I tried the following:

import numpy 

import array 

from datetime import datetime 

test = array.array('d', [0]*12000000) 

t = datetime.now() 

numpy.array(test) 

print datetime.now() - t

I get a result between one or two seconds: equivalent to a loop in python.

Is there a more efficient way of doing this conversion?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code for efficient python array to numpy array conversion:-

np.array(test) 

np.fromiter(test, dtype=int)

np.frombuffer(test)

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 5, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Oct 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...