Found 10476 Articles for Python

How to add a vector to a given Numpy array?

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

3K+ 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

4K+ 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

What's the fastest way of checking if a point is inside a polygon in Python?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:33:54

5K+ Views

First, we will create a polygon using the mplPath.Path method and to check whether a given point is in the polygon or not, we will use the method, poly_path.contains_point.StepsCreate a list of points to make the polygon.Create a new path with the given vertices and codes, using mplPath.Path().Check if point (200, 100) exists in the polygon or not, using contains_point() method. Return whether the (closed) path contains the given point. => TrueCheck if point (1200, 1000) exists in the polygon or not, using contains_point() method. Return whether the (closed) path contains the given point. => FalseExampleimport matplotlib.path as mplPath import ... 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

3K+ 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]]

How to create a numpy array within a given range?

Prasad Naik
Updated on 16-Mar-2021 10:21:08

1K+ Views

We have to create a numpy array in the range provided by the user. We will use the arange() function in the numpy library to get our output.AlgorithmStep1: Import numpy. Step 2: Take start_value, end_value and Step from the user. Step 3: Print the array using arange() function in numpy.Example Codeimport numpy as np start_val = int(input("Enter starting value: ")) end_val = int(input("Enter ending value: ")) Step_val = int(input("Enter Step value: ")) print(np.arange(start_val, end_val, Step_val))OutputEnter starting value: 5 Enter ending value: 50 Enter Step value: 5 [ 5 10 15 20 25 30 35 40 45]

How to plot ROC curve in Python?

Rishikesh Kumar Rishi
Updated on 16-Mar-2021 10:11:36

7K+ Views

ROC − Receiver operating characteristics (ROC) curve.Using metrics.plot_roc_curve(clf, X_test, y_test) method, we can draw the ROC curve.StepsGenerate a random n-class classification problem. This initially creates clusters of points normally distributed (std=1) about vertices of an ``n_informative``-dimensional hypercube with sides of length ``2*class_sep`` and assigns an equal number of clusters to each class.It introduces interdependence between these features and adds various types of further noise to the data. Use the make_classification() method.Split arrays or matrices into random trains, using train_test_split() method.Fit the SVM model according to the given training data, using fit() method.Plot Receiver operating characteristic (ROC) curve, using plot_roc_curve() method.To ... Read More

How to find the first date of a given year using Python?

SaiKrishna Tavva
Updated on 27-Feb-2025 17:04:07

6K+ Views

The datetime module in Python can be used to find the first day of the given year. In general, this datetime module is mostly used for manipulating dates and times. Some of the Common approaches to print the first day of the given year by using Python are as follows. Datetime Module: Widely used library for manipulating dates and times in various ways. Calendar Module: Provides functions related ... Read More

Write a Python program to remove a certain length substring from a given string

Prasad Naik
Updated on 16-Mar-2021 10:17:50

187 Views

We need to write a Python program which will remove a certain substring from a given stringAlgorithmStep 1: Define a string. Step 2: Use the replace function to remove the substring from the given string.Example CodeLive Demooriginal_string = "C++ is a object oriented programming language"modified_string = original_string.replace("object oriented", "")print(modified_string)OutputC++ is a  programming languageExplanationThe in-build Python replace() function takes the following parameters:Oldstring: The string which you want to removeNewstring: The new string you want to replace in place of the oldstringCount: Optional. Number of times you want to replace the oldstring with the newstringRead More

Advertisements