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.
ndarray.ndim
ndarray.shape
ndarray.size
ndarray.dtype
ndarray.itemsize
ndarray.data
>>> a.shape
(2, 5)
>>> a.ndim
2
>>> a.dtype
dtype('int32')
>>> type(a)
<class 'numpy.ndarray'>
another way to create an array
Two-dimensional array with complex data type
Array in a particular sequence
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.
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
Some other operations like sum, cumsum etc
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
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.
Note : NumPy’s array class is called
ndarray
. It is also known by the alias array
.
the number of axes (dimensions) of the array.
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
.
the total number of elements of the array. This is equal to the product of the elements of
shape
.
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.
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
.
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
Two-dimensional array with complex data type
Zeros() : array full of zero
ones() : array full of one
empty() : array full of buffer values
Array in a particular sequence
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.
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:Some other operations like sum, cumsum etc
Indexing, Slicing, and Iterating
One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.
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
Post a Comment