Numpy is a module for Python. The name is an acronym for "Numeric Python" or "Numeric Python".
It is an extensiion module for Python, mostly written in C . This makes sure that the precompiled mathematical and
numerical functions and functionalities of Numpy guarantee great execution speed.
Furthermore, Numpy enriches the programming language Python with powerful data structures,implementing multi-dimensional arrays and matrices. These data structured guarantee efficient calculations with matrices and arrays ,
better know under the heading of "big data". Besides that the module suppllies a large library of high-level mathmatical functions to operate on these matrices and arrays.
cvalues = [20.1, 20.8, 21.9, 22.5, 22.7, 22.3, 21.8, 21.2, 20.9, 20.1]
we will turn our list "cvalues" into a one-dimensional numpy array:
C = np.array(cvalues)
Let's assume, we want to turn the values into degrees into degrees Fahrenheit. This is very easy to accomplishment with a numpy array. The solution to our problem can be achieved bu simple scalar multiplication
print(C * 9 / 5 + 32)
The array C has not been changed by this expression
compared to this , the solution for our Pyhton list looks awkward:
fvalues = [ x*9/5 + 32 for x in cvalues]
So far, We referred to C as an array. The internal type is "ndarray" or to be even more precise
type(C) -> numpy.ndarray
import matplotlib.plot as plt
plt.plot()
plot.show()
The main benefit of using numpy arrays should be samller memory consumption and better runtime behaviour.
We want to look at the memory usage of numpy arrays in this subsumption of Python lists
from sys import getsizeof as size
lst = [24, 12, 57]
size_of_list_object = size(lst) # only green box
size_of_elements = len(lst) * size(lst[0]) # 24, 12, 17
total_list_size = size_of_list_object + size_of_elements
The size of a Python list consists of the general list information, the size needed for the references to the elements and the size of all the elements of the list.If we apply sys.getsizeof to a list, we get only the size
without the size of the elements.Memory consumption will be higher for larger interges.
Time Comparison between Python Lists and Numpy Arrays
One of the main advantages of Numpy is its advantage in time compared to standard python.
网友评论