•

## A numpy affair

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 n-dimensional 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 built-in sequences </p>

1From numpy user guide

# Basic operations¶

In :
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'>"


[  2  50 756]
<class 'numpy.ndarray'>

In :
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))


[[50 50]
[40 60]
[35 52]]
<class 'numpy.ndarray'>


Now you want to know the shape of your array:

In :
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


The shape is one dimension shows a tuple of its size:  (3,)
The shape is 3 rows, 2 columns:  (3, 2)


Now let us look at this

In :
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)"


(3,)
(2, 3)

In :
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([, , ]) # just as above
d =np.array([[], [], []])
# [[[]]] 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')


[1 2 3] (3,) :a is one dimension

[[1 2 3 4]] (1, 4) :b is two dimensions,hence the two brackets

[

] (3, 1) :c is two dimensions

[[]

[]

[]] (3, 1, 1) : d is three dimensions,hence the three brackets

In :
e =np.array([[[1,2,3]]])
print(e,e.shape)


[[[1 2 3]]] (1, 1, 3)


See the difference between d and e

That is for now! I will see you in the next post.