Introduction to numpy Part-I

·

2 min read

Numpy a library stands for numerical python used to handle array's in python which is faster than list, written basically on c/c++ and partially on python. As it a module we must import it.

In Python we have lists that serve the purpose of arrays, but they are slow to process. NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy. Arrays are very frequently used in data science, where speed and resources are very important.

  import numpy

After importing numpy module its ready to use.

 import numpy as np      //as imples alias in python
 a=np.array([1,2,3,4,5])   //variable a stores the array 
 print(a)                       //print the value(array) in a
 print(type(a)                  //print the type of a  i.e ndarry

 Output:
 [1,2,3,4,5]
 <class 'numpy.ndarray'>

Creating 2-D & -D array:

   import numpy as np
   a=np.array([[1,2,3],[2,3,4]])
   b=np.array([[[1,2,3],[2,3,4]],[[3,4,5],[4,5,6]]])

   print(a)
   print(b)

Screenshot (521).png

Array Indexing: Array indexing is the same as accessing an array element.You can access an array element by referring to its index number.

   import numpy as np
   prass = np.array([1, 'prass', 3, 4])
   arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
   arr1 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
   print(prass[0])
   print(prass[1])
   print(prass[1] + prass[3])
   print('2nd element on 1st dim: ', arr[0, 1])
   print(arr1[0, 1, 2])
   print('Last element from 2nd dim: ', arr[1, -1])

Screenshot (523).png