Posts

Boolean indexing in Python using Numpy

               It is the advanced indexing technique which makes you to select the elements of an array. It uses a Boolean mask (has two values True/False). Let us create this in 3 different ways as follows. Boolean indexing in 1D array Boolean indexing in 2D array Boolean indexing using Multiple conditions. Each one is explained with sample code as below. 👉Boolean indexing in 1D array               This program reads a single dimensional array as input. It uses a single condition to find Boolean mask. It finds the value and displays the result. 𜰘Code: import numpy as np1 s_arr = np1.array([11, 22, 33, 40, 55 ,66, 75]) mask = s_arr > 33 print("The resultant values are:\n",mask) result = s_arr[mask] print("The result which satisfies the condition s_arr>33:\n",result) 🖵Output: The resultant values are:   [False False False   True ...

Linear algrbra programs in Python Numpy

 What is Linear algebra?               It is a branch of mathematics which manipulates on matrices, vectors and linear equations.it isa foundation for Science, Engineering, Machine Learning, Data Science and so on.   Key Terms: Linear algebra deals with Scalars, Vectors, Matrices and Linear Equations. Let us discuss each one of it. Scalars: It deals with Single numbers.Eg :-5,3,4.5. Vector: It is an ordered lists of numbers. Matrices : It is a rectangular array of numbers. Linear Algebra: Equations where variables have power 1. Python: import numpy as np1 def main():     try:         # Example matrix         X = np1.array([[1, 1, 2],                       [1, 3, 2],      ...

Array operations in NumPy – Aggregate and Advanced Operations:

               Arrays in numpy has variety of operations like element-wise arithmetic, Scalar Operations, Aggregate and advanced functions. The basic operations are already discussed. Now,we discuss about the aggregate and advanced functions. Python Numpy Code for Aggregate and Advanced Operations:               This code starts from importing Numpy library. First,create two arrays with elements. Next,Aggregate functions like sum of all elements,Mean value,max and min values are calculated with built-in functions. Advanced functions like Dot product,square root and exponential are also calculated with built-in function. Code:   import numpy as np # Let us Create two arrays s_arr1 = np.array([1, 3, 5, 7]) s_arr2 = np.array([2, 4, 6, 8]) #Aggregate functions print("Sum:", np.sum(s_arr1))             ...

Scalar Multiplication in Numpy

               Here, we use Scalar multiplications in different ways. Let us try three methods listed as follows.. ·        1D array scalar multiplication ·        2D array scalar multiplication ·        Different scalars for each column The sample code is given below. 1D array scalar multiplication:   This code multiplies the array by 3. First, it creates an array of elements. Each element is multiplied by 3. The result is displayed. Code: import numpy as np1 # Create an array with elements s_arr = np1.array([1, 3, 5, 7, 9, 11, 13, 15]) # Scalar multiplication by 3 result = s_arr * 3 print("Original array:", s_arr)    print("Multiplied by 3:", result) Output: Original array: [ 1   3   5   7   9 11 13 15] Multiplied by 3: [ 3   9 15 21 27 33 39 45] 2D array scalar multiplication: ...

How to implement element-wise addition,subtraction and multiplication in Numpy?

              Array is a collection of similar data elements. When you implement the arithmetic operations, you have to do each element. Let us implement this as follows. Element-wise Arithmetic Operations in Numpy:               It starts from importing numpy library. First, two arrays are included as input. Element wise operations are performed and result is printed. Code: import numpy as np1 # Two arrays as input s_arr1 = np1.array([11, 22, 33, 44]) s_arr2 = np1.array([5, 10, 15, 20]) # Arithmetic operations on element wise print("Element wise Arithmetic Operations:") print("Addition:", s_arr1 + s_arr2)         print("Subtraction:", s_arr2 - s_arr1)      print("Multiplication:", s_arr1 * s_arr2)   Output: Element wise Arithmetic Operations: Addition: [16 32 48 64] Subtraction: [ -6 -12 -18 -24] ...

Indexing and slicing arrays in python numpy

               Indexing is the process of retrieving specific elements, whereas slicing extracts subarrays in a range. Both methods support negative indices and returns the original array elements. Mainly, these two methods make the user to access, extract the elements and manipulates the array in efficient manner. Let us discuss the methods with sample code as follows… Indexing in Numpy:               It supports three methods as follows. ·        Single element indexing ·        Multi-dimensional indexing ·        Index arrays The sample code is given below.. Single element indexing:               It follows list concept in python. Code: import numpy as np1 s_arr = np1.arange(10)    # [0,1,2,3,4,...

How to practice with .dtype and .shape in Numpy?

       Numpy has attributes for arrays. These attributes describes the structure and the content. These are given below… ·        .dtype ·        .shape .dtype:               It means ‘data type’. It gives the information about the values to be stored in the array. eg. Integers,floats,strings. .shape:               It provides the dimensions of the array. It may be of single dimensional, two dimensional or three dimensional. Let us create python program to implement using Numpy. Sample Python Numpy code for .dtype: # Example 1: Integer array Import numpy as np s_int = np.array([11, 22, 33, 44]) print("Array Elements:", s_int) print("Data type of the Array is:", s_int.dtype) Output: Array Elements: [11 22 33 44] Data type of the Array is: int64 #...