NUMPY

NUMPY ARRAY

Important points:

  1. Numpy is one of the most important package in numerical and scientific computations.
  2. It is most extensively used in Data Science, Machine Learning and Artificial Intelligence.
  3. Applications: It is applied in physics,computer graphics etc.,
  4. It is one of the most powerful numerical processing library in python.
  5. Numpy contains int,float and other datatypes as objects and list, dictonaries and other data structures as containers
  6. Numpy provides us an extension to python for multidimensional arrays.
  7. Numpy is closer to hardware.
  8. Numpy is designed for scientific computations.
  9. Numpy is also known as Array Oriented computation.
  10. Numpy is Memory efficient container that provides fast numerical operation
To use Numpy library we must import it first.
The Syntax is:

import numpy as np

In the above syntax we used 'as' to give an alias name to the tool we imported and the alias name is 'np' so that we can use alias name instead of typing the large name of the library.

To create an array using Numpy the syntax is:

a=np.array(list) --------------------------------------->eg. a=np.array([1,2,3,4])---(one dimensional)
                                                      a=np.array([[1,2],[3,4]])---(two dimensional)
                           a=np.array([[[1,2],[3,4]],[[5,6],[7,8]]])---(three dimensional)
Similarly, we can create array with any dimension as requried.
The list is internally represented as an array.

                                                   or
a=np.arange(range)------------------------------------>e.g.a=np.arange(10)

  • To find the dimension of an array: array.ndim
  • To find the the shape(row x column) of an array: array.shape
  • To find the length of array: len(array)
len(array) returns the size of the first dimension

Some other functions for creating arrays:

  1. a=np.linespace(starting point,ending point, no.of points in between): Divides the given range into equal no.of points based on 'no.of points' given in parameters.
  2. a=np.ones((3,3)) : Creates a 3x3 array with all ones. The (3,3) repreents 3 rows and 3 columns. Similary
  3. a=np.zeros((3,3)): Creates a 3x3 array with zeros.
  4. a=np.eye(3): Creates a 2 dimensional identity matrix.
  5. a=np.diag([1,2,3,4]):Creates a diagonal array with given values.
  6. To extract diagonal elements use np.diag(array_name).
  7. To create an array with particular datatype np.arange(range,dtype='datatype')

Creating random arrays:

a=np.random.rand(no.of elements): returns an array with uniformly distributed random elements.
a=np.random.randn(no.ofelements):returns an array with standard normal random elements.

Some other useful information:

  • To know the datatype of elemnts used in an array array.dtype
  • To slice the array array[start:end:stepsize]
  • To know if two arrays share same memory np.shared_memory(array1,array2)


NUMERICAL OPERATIONS ON NUMPY

To add a number to each element of an array: array+number
Similarly we can perform subtraction, multiplication,division and squaring etc.,
We can also perform arithmetic operations between two arrays, if they have same dimensions.

Matrix multiplication:

array* array
      or
array.dot(array)

Equals comparison:

'=='
or
np.array_equal(array1,arra2)

Logical operations:

or:np.logical_or(array1,array2)
and: np.logical_and(array1,array2)

Transcendental functions:

To use transcendental functions like trigonomertic, logaritmic and exponential functions.
examples:
  1. For sin values of elements in array np.sin(array)
  2. Similarly we can get the cos and tan values also.
  3. For log values of elements in array np.log(array)
  4. For exponential values of elements in array np.exp(array)

Basic reduction functions:

  • To get the sum of elements in single array np.sum(array)
  • To get the sum by rows and columns in multidimensional array.
  • array.sum(axis=0): sums the elements column wise.
  • array.sum(axis=1): sums the elements row wise.
  • To get the minimum value in an array array.min()
  • To get the maximum value in an array array.max()
  • To get the index value of minimum value in an array array.argmin()
  • To get the index value of maximum value in an array array.argmax() 

Statistics:

  1. To get mean value of an array array.mean()
  2. To get the median value of array np.median(array)
  3. To get the standard deviation value of an array array.std
  • To load text files with rows : np.loadtxt
  • To get the teanspose of an array: array.T

Array shape manipulation:

  1. To convert a single dimensional array into 2 dimensional array: array[:np.newaxis]
  2. To convert a 2 dimensional array into a single dimensional array: array.ravel
  3. a.T.ravel converts 2 dimensional array into a single dimensional array after transposing it.

Sorting data:

np.sort(array,axis='0/1') i.e., 0 for column wise and 1 for row wise.
                                          or
array.sort(axis='0/1')i.e., 0 for column wise and 1 for row wise.

Fancy indexing:

np.argsort(array): sorts the array and returns the modified value of their indexes.


Note: In all the above examples np is the alias name for Numpy

No comments:

Post a Comment