
A numpy affair
Mar 18, 2019
Introduction to numpy:NumPy is the fundamental package for scientiﬁc computing in Python. It is a Python library that provides a multidimensional array object, various derived objects(such as masked arrays and matrices).
It provides an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.^{1}
Combines the speed of operations written in C with the simplicity of Python as it uses ndimensional arrays of homogeneous data types (size is fixed at creation), with many operations being performed in compiled code for performance.^{1}
There are several important differences between NumPy arrays and the standard Python sequences:

NumPy arrays have a ﬁxed size at creation, unlike Python lists (which can grow dynamically). Changing the size of a ndarray will create a new array and delete the original.

The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory.
The exception: one can have arrays of(Python,includingNumPy)objects, thereby allowing for arrays of different sized elements.
NumPy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efﬁciently and with less code than is possible using Python’s builtin sequences </p>
For more info about the capabilities of Numpy, click on this link :https://docs.scipy.org/doc/numpy/reference/
^{1}From numpy user guideBasic operations¶
In [1]:import numpy as np # To create an ndarray, import the numpy package and use the method array and pass a list as an argument clara= np.array([2, 50, 756]) # Create a rank 1 array print(clara) print(type(clara)) # It shows the type of ndarray it is: "<class 'numpy.ndarray'>"
In [2]:nancy = np.array([[50,50],[40,60],[35,52]]) # To create an ndarray, import the numpy package and use the method array and pass a list as an argument print (nancy) print(type(nancy))
Now you want to know the shape of your array:
In [3]:print("The shape is one dimension shows a tuple of its size: ", clara.shape) print("The shape is 3 rows, 2 columns: ", nancy.shape) # rows x columns
Now let us look at this
In [4]:import numpy as np ai = np.array([1, 2, 3]) # Create a rank 1 array print (ai.shape) # Prints "(3,)" bi = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array print (bi.shape) # Prints "(2, 3)"
In [5]:a= np.array([1, 2, 3]) b= np.array([[1, 2, 3,4]]) # [[]] 2 brackets: Outer bracket for no of rows, inner bracket for no of columns c= np.array([[1], [2], [3]]) # just as above d =np.array([[[1]], [[2]], [[3]]]) # [[[]]] 3 brackets: Outer bracket for no of rows, inner bracket for no of columns,innermost for no of layers print(a,a.shape, ':a is one dimension') print() print(b,b.shape,':b is two dimensions,hence the two brackets') print() print(c,c.shape, ':c is two dimensions') print() print(d,d.shape,': d is three dimensions,hence the three brackets')
In [6]:e =np.array([[[1,2,3]]]) print(e,e.shape)
See the difference between d and e
That is for now! I will see you in the next post.

Comments
 (no comments)