Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python.array versus numpy.array

Tags:

python

numpy

If you are creating a 1d array in Python is there any benefit to using the NumPy package?

like image 962
Hortitude Avatar asked Sep 21 '08 20:09

Hortitude


People also ask

Are NumPy arrays faster than Python arrays?

As the array size increase, Numpy gets around 30 times faster than Python List. Because the Numpy array is densely packed in memory due to its homogeneous type, it also frees the memory faster.

What are the advantages of NumPy array over array module array in Python?

NumPy uses much less memory to store data The NumPy arrays takes significantly less amount of memory as compared to python lists. It also provides a mechanism of specifying the data types of the contents, which allows further optimisation of the code. If this difference seems intimidating then prepare to have more.

What is the difference between array and Ndarray?

array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy. ndarray , but it is not the recommended way. From the docstring of numpy.


2 Answers

It all depends on what you plan to do with the array. If all you're doing is creating arrays of simple data types and doing I/O, the array module will do just fine.

If, on the other hand, you want to do any kind of numerical calculations, the array module doesn't provide any help with that. NumPy (and SciPy) give you a wide variety of operations between arrays and special functions that are useful not only for scientific work but for things like advanced image manipulation or in general anything where you need to perform efficient calculations with large amounts of data.

Numpy is also much more flexible, e.g. it supports arrays of any type of Python objects, and is also able to interact "natively" with your own objects if they conform to the array interface.

like image 179
dF. Avatar answered Sep 19 '22 23:09

dF.


Small bootstrapping for the benefit of whoever might find this useful (following the excellent answer by @dF.):

import numpy as np from array import array  # Fixed size numpy array def np_fixed(n):     q = np.empty(n)     for i in range(n):         q[i] = i     return q  # Resize with np.resize def np_class_resize(isize, n):     q = np.empty(isize)     for i in range(n):         if i>=q.shape[0]:             q = np.resize(q, q.shape[0]*2)                 q[i] = i     return q      # Resize with the numpy.array method def np_method_resize(isize, n):     q = np.empty(isize)     for i in range(n):         if i>=q.shape[0]:             q.resize(q.shape[0]*2)         q[i] = i     return q  # Array.array append def arr(n):     q = array('d')     for i in range(n):         q.append(i)     return q  isize = 1000 n = 10000000 

The output gives:

%timeit -r 10 a = np_fixed(n) %timeit -r 10 a = np_class_resize(isize, n) %timeit -r 10 a = np_method_resize(isize, n) %timeit -r 10 a = arr(n)  1 loop, best of 10: 868 ms per loop 1 loop, best of 10: 2.03 s per loop 1 loop, best of 10: 2.02 s per loop 1 loop, best of 10: 1.89 s per loop 

It seems that array.array is slightly faster and the 'api' saves you some hassle, but if you need more than just storing doubles then numpy.resize is not a bad choice after all (if used correctly).

like image 36
nivniv Avatar answered Sep 20 '22 23:09

nivniv