Found 1204 Articles for Numpy

Python program to reverse a Numpy array?

Prasad Naik
Updated on 16-Mar-2021 10:42:12

489 Views

This is a simple program wherein we have to reverse a numpy array. We will use numpy.flip() function for the same.AlgorithmStep 1: Import numpy. Step 2: Define a numpy array using numpy.array(). Step 3: Reverse the array using numpy.flip() function. Step 4: Print the array.Example Codeimport numpy as np arr = np.array([10,20,30,40,50]) print("Original Array: ", arr) arr_reversed = np.flip(arr) print("Reversed Array: ", arr_reversed)OutputOriginal Array: [10 20 30 40 50] Reversed Array: [50 40 30 20 10]

How to print array elements within a given range using Numpy?

Prasad Naik
Updated on 16-Mar-2021 10:41:54

584 Views

In this program, we have to print elements of a numpy array in a given range. The different numpy functions used are numpy.where() and numpy.logical_and().AlgorithmStep 1: Define a numpy array. Step 2: Use np.where() and np.logical_and() to find the numbers within the given range. Step 3: Print the result.Example Codeimport numpy as np arr = np.array([1,3,5,7,10,2,4,6,8,10,36]) print("Original Array:",arr) result = np.where(np.logical_and(arr>=4, arr

How to find set difference between two Numpy arrays?

Prasad Naik
Updated on 16-Mar-2021 10:41:10

10K+ Views

In this program, we will find the set difference of two numpy arrays. We will use the setdiff1d() function in the numpy library. This function takes two parameters: array1 and array2 and returns the unique values in array1 that are not in array2.AlgorithmStep 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find the set difference between these arrays using the setdiff1d() function. Step 4: Print the output.Example Codeimport numpy as np array_1 = np.array([2, 4, 6, 8, 10, 12]) print("Array 1: ", array_1) array_2 = np.array([4, 8, 12]) print("Array 2: ", array_2) set_diff = ... Read More

How to find intersection between two Numpy arrays?

Prasad Naik
Updated on 16-Mar-2021 10:37:11

3K+ Views

In this problem, we will find the intersection between two numpy arrays. Intersection of two arrays is an array with elements common in both the original arraysAlgorithmStep 1: Import numpy. Step 2: Define two numpy arrays. Step 3: Find intersection between the arrays using the numpy.intersect1d() function. Step 4: Print the array of intersecting elements.Example Codeimport numpy as np array_1 = np.array([1,2,3,4,5]) print("Array 1:", array_1) array_2 = np.array([2,4,6,8,10]) print("Array 2:", array_2) intersection = np.intersect1d(array_1, array_2) print("The intersection between the two arrays is:", intersection)OutputArray 1:  [1 2 3 4 5] Array 2:  [2  4  6  8 10] The intersection between the two arrays is:  [2 4]

How to add a vector to a given Numpy array?

Prasad Naik
Updated on 16-Mar-2021 10:36:06

2K+ Views

 In this problem, we have to add a vector/array to a numpy array. We will define the numpy array as well as the vector and add them to get the result arrayAlgorithmStep 1: Define a numpy array. Step 2: Define a vector. Step 3: Create a result array same as the original array. Step 4: Add vector to each row of the original array. Step 5: Print the result array.Example Codeimport numpy as np original_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) print("Original Array: ", original_array) vector = np.array([1, 1, 0]) print("Vector: ... Read More

How to find the sum of rows and columns of a given matrix using Numpy?

Prasad Naik
Updated on 16-Mar-2021 10:35:16

3K+ Views

In this problem, we will find the sum of all the rows and all the columns separately. We will use the sum() function for obtaining the sum.AlgorithmStep 1: Import numpy. Step 2: Create a numpy matrix of mxn dimension. Step 3: Obtain the sum of all the rows. Step 4: Obtain the sum of all the columns.Example Codeimport numpy as np a = np.matrix('10 20; 30 40') print("Our matrix: ", a) sum_of_rows = np.sum(a, axis = 0) print("Sum of all the rows: ", sum_of_rows) sum_of_cols = np.sum(a, axis = 1) print("Sum of all the columns: ", sum_of_cols)OutputOur ... Read More

Linear regression with Matplotlib/Numpy

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:35:45

8K+ Views

To get a linear regression plot, we can use sklearn’s Linear Regression class, and further, we can draw the scatter points.StepsGet x data using np.random.random((20, 1)). Return random floats in the half-open interval[20, 1).Get the y data using np.random.normal() method. Draw random samples from a normal (Gaussian) distribution.Get ordinary least squares Linear Regression, i.e., model.Fit the linear model.Return evenly spaced numbers over a specified interval, using linspace() method.Predict using the linear model, using predict() method.Create a new figure, or activate an existing figure, with a given figsize tuple (4, 3).Add an axis to the current figure and make it the ... Read More

How to find the sum of all elements of a given matrix using Numpy?

Prasad Naik
Updated on 16-Mar-2021 10:34:44

2K+ Views

In this program, we will add all the terms of a numpy matrix using the sum() function in the numpy library. We will first create a random numpy matrix and then, we will obtain the sum of all the elements.AlgorithmStep 1: Import numpy. Step 2: Create a random m×n matrix using the random() function. Step 3: Obtain the sum of all the elements in the matrix using the sum() function.Example Codeimport numpy as np matrix = np.random.rand(3, 3) print("The numpy matrix is: ", matrix) print("The sum of the matrix is: ", np.sum(matrix))OutputThe numpy matrix is: [[0.66411969 0.43672579 0.48448593]  [0.76110384 ... Read More

Finding the number of rows and columns in a given matrix using Numpy

Prasad Naik
Updated on 16-Mar-2021 10:20:19

3K+ Views

We will first create a numpy matrix and then find out the number of rows and columns in that matrixAlgorithmStep 1: Create a numpy matrix of random numbers. Step 2: Find the rows and columns of the matrix using numpy.shape function. Step 3: Print the number of rows and columns.Example Codeimport numpy as np matrix = np.random.rand(2,3) print(matrix) print("Total number of rows and columns in the given matrix are: ", matrix.shape)Output[[0.23226052 0.89690884 0.19813164]  [0.85170808 0.97725669 0.72454096]] Total number of rows and columns in the given matrix are:  (2, 3)

How to create an identity matrix using Numpy?

Prasad Naik
Updated on 16-Mar-2021 10:20:45

4K+ Views

In this program, we will print an identity matrix of size nxn where n will be taken as an input from the user. We shall use the identity() function in the numpy library which takes in the dimension and the data type of the elements as parametersAlgorithmStep 1: Import numpy. Step 2: Take dimensions as input from the user. Step 3: Print the identity matrix using numpy.identity() function.Example Codeimport numpy as np dimension = int(input("Enter the dimension of identitiy matrix: ")) identity_matrix = np.identity(dimension, dtype="int") print(identity_matrix)OutputEnter the dimension of identitiy matrix: 5 [[1 0 0 0 0]  [0 1 0 0 0]  [0 0 1 0 0]  [0 0 0 1 0]  [0 0 0 0 1]]

Advertisements