Posts

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

Numpy mathematical operations on array using Python:

              Mathematical operations on array uses matrices. It uses Numpy to perform element-wise arithmetic, aggregate functions and linear algebra functions in effective ways. ·        This program starts with including numpy object. ·        Create two arrays for input purpose. ·        Next,element wise operations like Addition, Subtraction ,Multiplication and Division. ·        A scalar function is done by multiplying the array elements with 5. ·        Aggregate functions like sum, min, max and mean are done using built-in functions. ·        The advanced functions like square root and exponential are also done. Python Code: import numpy as np # Create arrays s_arr1 = np.array([1, 2, 3, 4]) s_arr2 = np.array([10, 20, 30, 40]) print("The origina...

Reshaping an array in python

               You have created an array. you want to reshape the array without altering data, this is method for you. Here, built-in method reshape() is used in Numpy. Let us do this for 1D and 2D array. Reshaping a 1D array into 2D array:               This code starts from importing numpy and its object. A single dimensional array is created with elements. The array is reshaped with 3 rows and 2 columns by the use of reshape() function. Original array and reshaped array is printed as output. Python Code: import numpy as np1 # Create a 1D array with elements s_arr = np1.array([11, 22, 33, 44, 55, 66]) # Let us Reshape into 3 rows and 2 columns reshaped_arr = s_arr.reshape(3, 2) print("Original array:", s_arr) print("Reshaped array:\n", reshaped_arr) Output: This is the output for the above program Original array: [11 22 33 44 55 66] Reshaped array:...

How to read inputs from the user for array in python?

               Array is a collection of similar data elements. It may be a single dimensional, two dimensional or more.. Let us create an array of elements and read inputs from the user. For this purpose, let us use the built-in functions like input(), split() and map(). Based on the data type, you can use these functions. Numpy directly converts the list into array. Python Program to read inputs from the user for 1D array:               1D array has a set of elements arranged in sequential manner. Let us create and read the input. Code: import numpy as np1 # Read the number of elements no = int(input("Enter the number of elements: ")) # Read the array of elements s_arr = list(map(int, input(f"Enter {no} numbers separated by space: ").split())) # Let us Convert to NumPy array s_arr = np1.array(s_arr) print("Array:", s_arr) Output: Enter the number of ele...

Sum of all elements in 1D and 2D array in Python

          This program adds the elements from beginning to end. It can be done by built-in functions or using loop. Let us discuss many methods as follows… 1D array elements: The methods are ·        Using built-in method sum () ·        Using loops ·        Using numpy Using built-in method sum (): #create an array of elements n_array = [100, 200, 300, 400, 500] #find the total from the sum() a_total = sum(n_array) #print the sum print("Sum of the elements in the array:", a_total) Output: Sum of the elements in the array: 1500 Using loops: #Array creation n_array = [11, 22, 33, 44, 55] a_total = 0 #using a for loop to find the sum for no in n_array:     a_total += no #Print the sum print("Sum of the elements are:", a_total) Output: Sum of the elements are: 165 Using Numpy:      ...