Posts

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 ...

Random number Generation in Python

               It can be implemented by many methods based on user requirements.Based on the fucntions,the list is given below. ‘randint()’ ‘random()’ or ‘uniform()’ ‘choice()’ ‘sample()’ ‘shuffle()’ ‘seed()’ ‘secrets.randbelow()’ ‘rand()’ Various types of Distrbutions 1. ‘randint()’:               This function is a built-in function of random package. It reads the number limit to generate random values. Code: # This code imports random package import random print("The random value is:") #prints the random integer value in the range 1 to 100. print(random.randint(1, 100))   Output: The random value is: 11 2.‘random()’ or ‘uniform()’:               This generates random float number. ‘random()’ gives a single float number. ‘uniform()’ gives you the float number within the r...

Matrix Multiplication in python

              Matrix multiplication can be done by multiplying each row with each column. It can be done when the rows of first matrix and column of second matrix should be equal. Let us implement the matrix multiplication as follows. This can be done by two ways. Using Numpy Using Lists Using Numpy:               This program uses Numpy functions dot(),@ to generate matrix multiplication. Code: import numpy as np1 # Define two matrices X = np1.array([[2, 3],               [4, 1]]) Y = np1.array([[8, 6],               [7, 5]]) # Matrix multiplication Z = np1.dot(X, Y)   # Method 1 A = X @ Y          # Method 2 (Python 3.5+) print("Matrix Multiplication Using np.dot:\n", Z) print("Matrix Multiplication Using @ operator:\n", A) Output: Matrix Multiplica...

Transpose matrix implementation in python

              Transpose means changing column into rows and rows into columns. It can be implemented in python with or without Numpy. There are three methods to implement as follows. ·        Using Numpy ·        Using Zip ·        Manual Loops Using Numpy:               Numpy is predefined. It has many built-in functions. Pyhon code: import numpy as np1 # Step 1: create a 4X4 matrix X = np1.array([[1, 2, 3, 6],               [4, 5, 6, 10],               [7, 8, 9, 2],               [3, 6, 4, 8]]) # Step 2: Transpose the matrix X X_T = X.T print("The Original Matrix:\n", X) print("Here is the Transposed Matrix:\n", X_T) Output: The Original Matrix:  [[ 1...

Implementation of Identity Matrix in Python

             Identify Matrix has the rows and columns in equal manner. It has ones on the diagonal and zeros in other places. This can be done two different ways as follows… ·        Using Numpy ·        Using Plain Python Code 1.Using Numpy:               This method uses the ‘Numpy’ to create object and calling built-in functions. Implementation: ·        It uses Numpy built-in file and creates the object. It calls the ‘identity()’ function to build the identity matrix. ·        Finally,prints the identity matrix. Code: #Using Numpy import numpy as np1 # Create a 4x4 identity matrix I = np1.identity(4) print("Here is the Identity Matrix using Numpy:") print(I) Output: Here is the Identity Matrix using Numpy: [[1. 0. 0. 0.]  [0...