Day 10 : Advance Python Libraries - Numpy

Hello Guys,
Day by day situation getting worse, but don't be panic. We will tackle this situation whatever the situation will be.
Just follow the guidelines of the government and help our worriers.

Previously we learn about class and object-oriented python and today we learn about some advance libraries in python.

NumPy


In simple words, We can say NumPy is a python package that is used to perform more advance mathematical operations. or NumPy is a python array.

Actual Definition:
NumPy is the fundamental package for scientific computing with Python. It contains among other things:
  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

Is Numpy a built-in library?
No, It is not a Built-in library.

How to install Numpy?
Open Cmd or Command prompt or terminal
and Type

pip install numpy

or 

python -m pip install numpy

How to use Numpy?
As like we use other libraries by importing


Some Basics of Numpy
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.

Quite Confusing?
Nope, As I said earlier, Numpy is python array and we know array can be a single dimension or multi-dimensional
array and these dimensions are termed as axes. we can have elements of the same type only. (Not Like list where we can have elements of a different type )

Example
The coordinates of a point in 3D space
 [1, 2, 1] 
has one axis. That axis has 3 elements in it, so we say it has a length of 3

 the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.
[[ 1., 0., 0.],
 [ 0., 1., 2.]]

Note : NumPy’s array class is called ndarray. It is also known by the alias array


ndarray.ndim
the number of axes (dimensions) of the array.
ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the number of axes, ndim.
ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype
an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.
ndarray.data
the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.


Example :
How to create two-dimensional array from 0 to 9 ?
In other languages what we will do is to write all those numbers from 0 to 9.
But here
>>> import numpy as np
>>> a=np.arange(10).reshape(2,5)
>>> a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

>>> a.shape
(2, 5)
>>> a.ndim
2
>>> a.dtype
dtype('int32')
>>> type(a)
<class 'numpy.ndarray'>



another way to create an array


>>> a = np.array([2,3,4])
>>> a
array([2, 3, 4])

Two-dimensional array with complex data type
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )
>>> c
array([[1.+0.j, 2.+0.j],
       [3.+0.j, 4.+0.j]])

Zeros() : array full of zero
ones() : array full of one
empty() : array full of buffer values

>>> np.zeros((3, 4))
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
>>> np.ones( (2,3,4), dtype=np.int16 )                # dtype can also be specified
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]], dtype=int16)
>>> np.empty( (2,3) )                                 # uninitialized
array([[  3.73603959e-262,   6.02658058e-154,   6.55490914e-260],  # may vary
       [  5.30498948e-313,   3.14673309e-307,   1.00000000e+000]])

Array in a particular sequence
>>> np.arange( 10, 30, 5 )
array([10, 15, 20, 25])
>>> np.arange( 0, 2, 0.3 )                 # it accepts float arguments
array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])

How to get Pi value?
>>> np.pi

3.141592653589793


Basic Mathematical operation

Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.

>>> a = np.array( [20,30,40,50] )
>>> b = np.arange( 4 )
>>> b
array([0, 1, 2, 3])
>>> c = a-b
>>> c
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9])
>>> 10*np.sin(a)
array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])
>>> a<35
array([ True,  True, False, False])

How to perform matrix multiplication?

In other languages, we write bunch of lines of code
But here

The matrix product can be performed using the @ operator (in python >=3.5) or the dot function or method:

>>> A = np.array( [[1,1],
...                [0,1]] )
>>> B = np.array( [[2,0],
...                [3,4]] )

>>> A @ B                       # matrix product
array([[5, 4],
       [3, 4]])
>>> A.dot(B)                    # another matrix product
array([[5, 4],
       [3, 4]])


Some other operations like sum, cumsum etc
>>> b = np.arange(12).reshape(3,4)
>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>>
>>> b.sum(axis=0)                            # sum of each column
array([12, 15, 18, 21])
>>>
>>> b.min(axis=1)                            # min of each row
array([0, 4, 8])
>>>
>>> b.cumsum(axis=1)                         # cumulative sum along each row
array([[ 0,  1,  3,  6],
       [ 4,  9, 15, 22],
       [ 8, 17, 27, 38]])

Indexing, Slicing, and Iterating

One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.
>>>
>>> a = np.arange(10)**3
>>> a
array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64])
# equivalent to a[0:6:2] = 1000;
# from start to position 6, exclusive, set every 2nd element to 1000
>>> a[:6:2] = 1000
>>> a
array([1000,    1, 1000,   27, 1000,  125,  216,  343,  512,  729])
>>> a[ : :-1]                                 # reversed a
array([ 729,  512,  343,  216,  125, 1000,   27, 1000,    1, 1000])
>>> for i in a:
...     print(i**(1/3.))
...
9.999999999999998
1.0
9.999999999999998
3.0
9.999999999999998
4.999999999999999
5.999999999999999
6.999999999999999
7.999999999999999
8.999999999999998

Reshaping and resizing array

Resize : Modify array by itself don' return array

>>> a=np.arange(20).reshape(4,5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> a.resize((10,2))
>>> a
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11],
       [12, 13],
       [14, 15],
       [16, 17],
       [18, 19]])

Reshape : Return modified array but not modify existing array

>>> a.reshape(4,5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> a
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11],
       [12, 13],
       [14, 15],
       [16, 17],

       [18, 19]])


That's all basic we need to know about numpy


❤❤Quarantine python group link ❤❤

8805271377 WhatsApp

Follow here ❤

@mr._mephisto_ Instagram 

There will be no restrictions just feel free to learn. 

Share and take one more step to share knowledge to others. 

Believe in yourself 🤟 you are awesome. 

Be safe, Be happy😁
Take care of yourself and your family 
Of course watch movies and series🤟😉 

And follow the guidelines of government

Comments

Popular posts from this blog

Day 16 : Pandas Basics

News website using Flask and news API - Working with templates : Part 5

Day 9 : Encapsulation and polymorphism