Posts

Numpy- How to import and install?

              ‘Numpy’- a python library which holds many built-in functions specially designed for scientific computing. It is efficient for multidimensional arrays. It supports data science, machine learning. It provides the following features. ‘ndarray’- it supports N-dimensional array. Automatic alignment can be done. Operations are vectorized. It supports many libraries. High performance is achieved. Let us follow the steps to import and install the Numpy in your system. First, open the command prompt or terminal In windows: search tab, type Command Prompt. Other Os like macOS/Linux: open the Terminal. Next, run the pip command with Numpy keyword. C:\Users>python -m pip install numpy It successfully install your Numpy. Once, it was successfully installed, check it with following code. C:\Users>pip list Package             Version ------------------ ------...

Python Program to compute the determinant of a matrix?

               Let us compute the determinant of a matrix using the Numpy Library. How to find determinant of a matrix in mathematical way?               It is a scalar value that deals with a square matrix. It is denoted by det(A) or |A|. If it is a 2×2 matrix, it follows this formula. For A=[abcd], the determinant is |A|=ad−bc. If it is a 3x3 matrix, it uses /laplace expansion or cofactor method. If you have nxn matrix, it uses Leibniz formaula,Laplace expansion or Gaussian elimination. Let us implement in three ways. 1.       Normal method. 2.       Using Numpy 3.       Using Laplace 1.Normal method.               This method uses the simple code implementation. Code: # 2x2 determinant without NumPy def det_2x2(s_ma...

How to find Eigen value and Eigen vector using Python

       These are the concepts available in Linear algebra. It has the following definitions. Eigenvalue( λ ): It is a scalar value.It shows about the eigen vector stretch and compress in terms of linear transformation. Eigenvector(v): It is a vector value.It is a non-zero vector which changes the scale alone. The formula for square matrix A is given below. Av=λv Where A represents matrix. ‘v’ denotes eigenvector. ‘λ’ is for eigenvalue. Let us implement the eigen value and eigen vector in python. This program imports numpy package with its object np. A square matrix is created with elements. Eigenvalues and eigenvectors variables are declared. There is a function ‘linalg.eig(matrix)’ which generate the eigenvalues and eigenvectors. Python Program: import numpy as np # Declare a square matrix A = np.array([[5, 3, 2],               [1,-1, 4],      ...

How to solve a system of linear equations using Numpy?

              System of linear equations have a set of equations with same variable. It has a solution of values which suits all the equations. It can be done by input validation, error handling. Let us create a python program to solve this. Code: import numpy as np def solveIt_linear_system():     try:         # Input statement         n = int(input("Enter the number of equations/variables: "))         if n <= 0:             print("Number of equations must be positive.")             return         # Read the coefficient matrix         print(f"Enter the {n}x{n} coefficient matrix (row by row):")      ...

Aggregations on matrix in Python

               Aggregations are the data transformation techniques which produces scalar values from arrays. Let us implement this two ways 1.       Using python code 2.       Using Numpy The code is given below… 1.Using python code:               This method creates a matrix.It calculates the row wise sum,column wise sum and overall sum. It also finds the maximum value in row wise and column wise. Finally,it displays the values. Code: s_matrix = [     [2, 2, 1],     [6, 5, 4],     [3, 8, 9] ] # Let us find Row-wise sum row_sums = [sum(row) for row in s_matrix] # Let us find Column-wise sum col_sums = [sum(s_matrix[r][c] for r in range(len(s_matrix))) for c in range(len(s_matrix[0]))] # Sum of all the elements total_sum = sum(sum(row) for row in s_ma...

Advance functions for Random number generation in Python

               Random Number generation can be categoried into three categories. Part1 describes the basic type. Part2 deals with the probability distributions. Part3 covers the advanced Random number Generation. To read the part1,part2,just follow the link. https://rajeeva84.blogspot.com/2026/08/random-number-generation-in-python.html https://rajeeva84.blogspot.com/2026/08/random-number-generation-in-python_02076504341.html Advanced methods are given below… ·        Random State / Seed ·        Cryptographically Secure Randoms 1.Random State/Seed:               It uses ‘seed()’ method. This helps to initialize the generator. As a default value,it seeds the current system time. First method uses randint() function to print random value. Second method uses seed() method. Python code: import random # Wi...

Random number Generation in Python part2(Probability Distributions)

               Part 1 blog post explained the basic random number generations. This blog post includes the methods in Probability distributions. The methods are listed below… ·        Normal Distribution ·        Binomial distribution ·        Poisson distribution ·        Exponential distribution 1.Normal Distribution               It is the most widely used distribution. It is symmetric with mean, standard deviation values. Code: #create an object for numpy import numpy as np1 print("Normal distribution values are:") #generate normal distribution with mean 1 std as 2 with 4 elements print(np1.random.normal(1,2,4))   Output: Normal distribution values are: [ 1.40837209  2.3509239  -0.15261431 -0.09280015] 2.Binomial ...