Search

NumPy - Part1

NumPy (Numerical Python) is the fundamental package for scientific computing in Python. It provides a multidimensional arrayobject, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays,including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.


Why we need numpy when we have list?/Why numpy is so popular?

  • Provide faster operation for Large Scale operation
  • Behind the scene optimizations written in C
  • Vectorization via broadcasting(avoiding loops)s
  • Backbone of other python scientifi c packages


Numpy dataTypes and Attributes

Main datatype of Numpy is ndarray, where ndarray represent n dimentional array.

import numpy as np
import pandas as pd #Details of pandas library will be cover in Pandas chapter.
a1 = np.array([1,2,3])
print(a1)
print("Type of the array is {}".format(type(a1)))
print("\nData type of the array is {}".format(a1.dtype))
print("\nShape of the array is {}".format(a1.shape))
print("\nSize of the array is {}".format(a1.size))
print("\nDimention of the array is {}".format(a1.ndim))
print("\n",pd.DataFrame(a1))

O/P

[1 2 3]
Type of the array is <class 'numpy.ndarray'>

Data type of the array is int64

Shape of the array is (3,)

Size of the array is 3

Dimention of the array is 1

    0
0  1
1  2
2  3


a2 = np.array([[1,2.5,3],[4,5,6],
               [7,8,9],[10,11.8,12]])
print(a2)
print("Type of the array is {}".format(type(a2)))
print("\nData type of the array is {}".format(a2.dtype))
print("\nShape of the array is {}".format(a2.shape))
print("\nSize of the array is {}".format(a2.size))
print("\nDimention of the array is {}".format(a2.ndim))
print("\n",pd.DataFrame(a2))

O/P

[[ 1.   2.5  3. ]
 [ 4.   5.   6. ]
 [ 7.   8.   9. ]
 [10.  11.8 12. ]]
Type of the array is <class 'numpy.ndarray'>

Data type of the array is float64

Shape of the array is (4, 3)

Size of the array is 12

Dimention of the array is 2

       0     1     2
0   1.0   2.5   3.0
1   4.0   5.0   6.0
2   7.0   8.0   9.0
3  10.0  11.8  12.0

a3 = np.array([[[1,2,3],[4,5,6],[7,8,9]],
               [[10.4,11,12],[13,14,15],[16,17,18]],
               [[19,20,21],[22,23,24],[25,26,27]]])
print(a3)
print("Type of the array is {}".format(type(a3)))
print("\nData type of the array is {}".format(a3.dtype))
print("\nShape of the array is {}".format(a3.shape))
print("\nSize of the array is {}".format(a3.size))
print("\nDimention of the array is {}".format(a3.ndim))

O/P

[[[ 1.   2.   3. ]
  [ 4.   5.   6. ]
  [ 7.   8.   9. ]]

 [[10.4 11.  12. ]
  [13.  14.  15. ]
  [16.  17.  18. ]]

 [[19.  20.  21. ]
  [22.  23.  24. ]
  [25.  26.  27. ]]]
Type of the array is <class 'numpy.ndarray'>

Data type of the array is float64

Shape of the array is (3, 3, 3)

Size of the array is 27

Dimention of the array is 3



Creating NumPy Arrays

Array filded with Zeros

arrays_zeros = np.zeros((3,5))
print(arrays_zeros)

O/P

[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]


Array filded with Ones

arrays_one = np.ones((3,4))
print(arrays_one)

O/P

[[1. 1. 1. 1.]

 [1. 1. 1. 1.]

 [1. 1. 1. 1.]]


Arrays with range 

arrays_range = np.arange(0,10,1.5)
print(arrays_range)


O/P

 [0.  1.5 3.  4.5 6.  7.5 9. ]


Array with random value

arrays_Randint = np.random.randint(0,10,size=(3,5))

print(arrays_Randint)

O/P 

[[0 4 4 8 6]

 [4 2 4 3 5]

 [1 6 5 1 7]]


arrays_random = np.random.random((3,6))

print(arrays_random)

O/P 

 [[0.64084004 0.33433793 0.35160611 0.47617328 0.93902808 0.28369407]

 [0.51331203 0.1081699  0.67188269 0.65432508 0.11384678 0.34118088]

 [0.97374071 0.77074441 0.83331702 0.6284412  0.02806067 0.51628191]]


arrays_rand = np.random.rand(3,6)

print(arrays_rand)

O/P 

 [[0.27696762 0.57172661 0.26943377 0.69402891 0.3736599  0.2917181 ]

 [0.47850275 0.56993397 0.14296559 0.40478406 0.19765258 0.07971088]

 [0.02969733 0.58517323 0.27424075 0.07429239 0.65130441 0.13077611]]


random seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number.

By default the random number generator uses the current system time.

np.random.seed(1)

arrays_random = np.random.random((2,2))

print(arrays_random)

O/P 

 [[4.17022005e-01 7.20324493e-01]

 [1.14374817e-04 3.02332573e-01]]


np.random.seed(1)

arrays_random = np.random.random((2,2))

print(arrays_random)

O/P 

 [[4.17022005e-01 7.20324493e-01]

 [1.14374817e-04 3.02332573e-01]]


Find the unique value of an array

np.unique(arrays_Randint)

O/P 

 array([0, 1, 2, 3, 4, 5, 6, 7, 8])


Viewing Arrays and matrices

a4 = np.random.randint(0,10,size=(2, 3, 4,5))
print(a4)
print(a4.ndim)

O/P 

 [[[[0 0 1 7 6]

   [9 2 4 5 2]

   [4 2 4 7 7]

   [9 1 7 0 6]]


  [[9 9 7 6 9]

   [1 0 1 8 8]

   [3 9 8 7 3]

   [6 5 1 9 3]]


  [[4 8 1 4 0]

   [3 9 2 0 4]

   [9 2 7 7 9]

   [8 6 9 3 7]]]



 [[[7 4 5 9 3]

   [6 8 0 2 7]

   [7 9 7 3 0]

   [8 7 7 1 1]]


  [[3 0 8 6 4]

   [5 6 2 5 7]

   [8 4 4 7 7]

   [4 9 0 2 0]]


  [[7 1 7 9 8]

   [4 0 1 9 8]

   [2 3 1 2 7]

   [2 6 0 9 2]]]]

4


print only 3 values on the inner most matrices

print(a4[:, :, :, :3])

O/P 

[[[[0 0 1]

   [9 2 4]

   [4 2 4]

   [9 1 7]]


  [[9 9 7]

   [1 0 1]

   [3 9 8]

   [6 5 1]]


  [[4 8 1]

   [3 9 2]

   [9 2 7]

   [8 6 9]]]



 [[[7 4 5]

   [6 8 0]

   [7 9 7]

   [8 7 7]]


  [[3 0 8]

   [5 6 2]

   [8 4 4]

   [4 9 0]]


  [[7 1 7]

   [4 0 1]

   [2 3 1]

   [2 6 0]]]]

 

print 2nd matrices

print(a4[0, 1, :,: ])

O/P 

 [[9 9 7 6 9]

 [1 0 1 8 8]

 [3 9 8 7 3]

 [6 5 1 9 3]]

No comments:

Post a Comment