Social Icons

Monday, February 24, 2020

The Python NumPy Library

 1.  About numpy 

1.1 What is numpy ?

Numpy is an open source library associated with the Python language, created specially for scientific computing, notably matrix computing, while providing multiple functions allowing the direct creation and manipulation of matrices, vectors, etc... via data. Official documentation: https://numpy.org/doc/

1.2 How to install numpy ?

Installing the numpy library is a trivial operation, just launch the cmd command prompt and type:
pip install numpy

2. Matrix or table with numpy 

2.1 Syntax

import numpy as np
a = np.array([ligne1, ligne2 , ligne3])

Exemple: matrix with 2 rows and 3 columns 2x3

import numpy as np
a = np.array([[2,7,3] , [8,5,4]])
print(a)
#The output is as below::
[[2 7 3]  
[8 5 4]]

2.2 Product term by term

import numpy as np
a = np.array([[5, 3],[2, 3]])
b = np.array([[1, 3],[7, 3]])
print(a*b)
#The output is as below:
[[ 5  9]  
[14  9]]

2.3 Matrix product

import numpy as np
a = np.array([[5, 3] , 
              [2, 3]])
b = np.array([[1, 3],
              [1, 3]])
product = np.dot(a,b)
print(product)
#The output is as below: 
[[ 8 24]  
[ 5 15]]

2.4 Transpose of a matrix

import numpy as np
a = np.array([[5, 3] , [2, 3]])
print(a.T)
# The output is:
[[5 2]  
[3 3]]

2.5 Unit matrix

import numpy as np
a = np.eye(3)
print(a)
# The output is:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

2.6 Create a matrix by subdividing an interval 

 numpy.linspace() returns a one-dimensional array from a start value to an end value following a given step.

Exemple

import numpy as np
m = np.linspace(0, 20, 5)   
print("The matrix obtained using step 5 is: ",m)
# The output is:
The matrix obtained using step 5 is : [ 0.  5.  10.  15.  20.]

2.7 Action of a mathematical function on an array

NumPy has a large number of mathematical functions that can be applied directly to an array. In this case, the function is applied to each of the elements of the array.

Example

import numpy as np  
x = np.linspace(0, 3,4)
y = np.exp(x)
print(y)
# The output is: [ 1.   2.71828183  7.3890561  20.08553692]

3.  The sublibrary linear algebra

3.1  Inverse of a matrix with linalg numpy

To invert a matrix with numpy, we use the method: NumPy linear algebra. For more details on this function, see the official documentation: https://docs.scipy.org/doc/numpy/reference/routines.linalg.html
import numpy as np 
a = np.array([[2, 1],
              [5, 3]])
b = np.linalg.inv(a)
print("The matrix inverse of a is : " , b)
# The output is:
The matrix inverse of a is :  
[[ 3. -1.]
[-5.  2.]]

3.2 The eigenvalues of matrix with linalg numpy

To calculate the eigenvalues ​​of a matrix with numpy, we use the eig() method:

Example:

import numpy as np  
a = np.array([[1, 2],
              [3, 2]])
eg = np.linalg.eigvals(a)
print("The eigenvalues  of matrix a are : " , eg)
# The output is: The eigenvalues  of matrix a are :  [-1.  4.]

Remark

You can use several other notions of linear algebra with the linalg numpy library, such as characteristic polynomial, minimal polome, rank of a matrix ... See the official documentation: https://docs.scipy.org/doc/numpy/reference/routines.linalg.html


Younes Derfoufi
my-courses.net

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (1) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (167) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (32) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (138) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates