Basic Introduction to Numpy

Abhishek Jaiswal
Analytics Vidhya
Published in
5 min readAug 17, 2020

--

Photo by Fabio on Unsplash

According to Wikipedia, NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

Being the Library of Python, it can perform algorithm Faster than the default Python’s library. Without wasting much time, Lets Dive into the Basics of Numpy that can help in the Data Science field. We will be going through Every Case important in the Numpy

Case 1:Memory Allocation

Fig. 1

In Fig. 1, getsizeof in default python library and itemsize in Numpy library is used to find the size of the datatype used in the program.Here, sys.getsizeof(int) has a memory of 28 bytes and the same type(np.type(int).itemsize) in Numpy has size of 8 bytes .So, processing a large number can be done faster in Numpy. (Fig. 2)

Fig. 2

in Fig. 2, Numpy here takes 24.9ms compared to default Python that takes here 3.94s.

Case 2: Basic Numpy Array

Fig. 3
  1. np.array([……….]) -It is used to pass the number/decimal/char into the List(Ordered sequence + Mutable) and later-on can play with it.
  2. np.a[index_position] -It is used to access the stored item in List directly by their index_position in the array name a(here).

NOTE: index_position in python starts from 0 base Indexing.

3. np.a[starting_index: ending+1_index: jump) -It is used to access the stored item in List by Slicing starting from starting_index(included) and ending_index(excluded) with the jump in the List.

NOTE 3.a) jump if not written by default is 1.

3.b) If no subscript is written by default assumed to be its default location i.e starting_index = 0,ending_index=-1,jump=1

4. b[0], b[2], b[-1] are treated as same as b[[0, 2, -1]] in the numpy.

Case 3: Array type and its Conversion

Fig. 4
  1. b.dtype: It is used to find the data type of any item in the Python.
  2. dtype = np.float & dtype = np.int8 -It is used to convert datatype from one format to another.

Case 4: Dimensions and Shapes

Fig. 5
  1. np.array([…….], [……..]) -It is used to store a Multi-dimensional array in it.
  2. A.shape -It is used here for the array named A to find the Shape of the array in Numpy.
  3. A.ndim -It is used here for the array named A to find the dimension of the array(1D/2D/3D).
  4. A.size -It is used here for the array named A to find the size of the array.

Here, B represents a 3D array as its first subscript represents l1*b1 and others represent l2*b2.

Case 5: Indexing and Slicing of a Matrices

Fig. 6

All the Slicing and Indexing remains the same as what we have learned so far in Case 2

  1. A[1][0] or A[1,0] -It is used to access the 0th element of the 1th List.
  2. A[:2, 2:] -It is used to access the 2nd-last element of the 0th-2–1th list.
Fig. 7

Since the List/Lists of List are Mutable, so they can change their Value at any point in time as shown in Fig. 7.

Case 6:Some Statistics Function

Fig. 8
  1. a.sum( ) -It is used to find the sum of the elements in the Array a
  2. a.mean( ) -It is used to find the mean/average of the elements in the Array a.
  3. a.std( ) -It is used to find the standard deviation of the elements in the Array a.
  4. a.var( ) -It is used to find the Variance of the elements in the Array a.
Fig. 9

By default is no attribute is passed , then all the Rows and Columns are included.Axis=0 represents the formulation done in along Y-axis and Axis=1 represents the formulation done in along X-axis.

Case 7: Vectorized Operation

Fig. 10

In Fig. 10,

  1. Adding 10 to array a , adds 10 to every element in a, But it does not change the Original Value of the element stored in the array a.
  2. Adding a = a+10 or a+=10 changes the Original Value of the element stores in the array a.
  3. We can add 2 arrays (E.g: a+b) not shown here.

Case 8: Masking / Boolean Arrays

Fig. 11

We can take the help of the Boolean Expression to find the element we want.

Fig. 12

Case 9: Basics Linear Algebra

Fig. 13
  1. A.dot(B) or A@B -It is used to find the dot product of A and B.
  2. B.T -It is used to find the Transpose of Matric B.

Note: Cross Product is generally not used in the Data Field.

Case 10: Random and Arrange Function

Fig. 14
  1. np.random.random(size=…) -It is used to create an array of specified shapes and fills it with random values.
  2. np.random.normal(size=…) -It is used to create an array of specified shapes and fills it with random values which are actually a part of Normal(Gaussian)Distribution.
  3. np.random.rand(a,b) -It is used to create an array of a specified matrix of a rows and b columns and fills it with random values.
  4. np.arange(Initial_index, Final_index, jump) -It is used to create an array with a starting index, Ending index, and jump of the array.

Some Other Useful Functions

Fig. 15
Fig. 16
Fig. 17

This much Information is Enough for anyone to get started with the Numpy.

To access this Material, You can refer to https://drive.google.com/file/d/12jjql5gX3dR3Pkx04OVgnhvRnymLHMCt/view?usp=sharing

--

--