Python Articles

Page 516 of 855

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

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 929 Views

In NumPy, you can print array elements within a specific range using several methods. The most common approaches are numpy.where() with numpy.logical_and(), boolean indexing, and conditional filtering. Using numpy.where() with logical_and() The numpy.where() function returns the indices of elements that meet a condition ? import numpy as np arr = np.array([1, 3, 5, 7, 10, 2, 4, 6, 8, 10, 36]) print("Original Array:") print(arr) # Find indices of elements between 4 and 20 (inclusive) indices = np.where(np.logical_and(arr >= 4, arr = 4) & (arr = 4) & (arr = min_val) & (arr

Read More

How to add a vector to a given Numpy array?

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 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 array using NumPy's broadcasting capabilities. Algorithm Step 1: Define a numpy array. Step 2: Define a vector. Step 3: Add vector to each row of the original array using broadcasting. Step 4: Print the result array. Method 1: Using Broadcasting (Recommended) NumPy automatically broadcasts the vector to each row ? import numpy as np original_array = np.array([[1, 2, 3], [4, ...

Read More

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

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 4K+ Views

In NumPy, you can calculate the sum of rows and columns of a matrix using the np.sum() function with the axis parameter. This is useful for data analysis and mathematical computations. Syntax numpy.sum(array, axis=None) Parameters: array − Input matrix or array axis − 0 for column-wise sum, 1 for row-wise sum Example Let's create a matrix and find the sum of rows and columns ? import numpy as np # Create a 2x2 matrix matrix = np.array([[10, 20], ...

Read More

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

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 5K+ Views

Checking if a point is inside a polygon is a common computational geometry problem. Python offers several approaches, with matplotlib's Path class being one of the fastest and most reliable methods for this task. Using matplotlib.path for Point-in-Polygon Testing The matplotlib library provides an efficient implementation through the mplPath.Path class, which uses optimized algorithms for point-in-polygon testing. Steps Create a list of points to define the polygon vertices. Create a path object using mplPath.Path() with the polygon coordinates. Use the contains_point() method to check if a point lies inside the polygon. Example ...

Read More

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

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 3K+ Views

NumPy provides several ways to find the dimensions of a matrix. The most common method is using the shape attribute, which returns a tuple containing the number of rows and columns. Creating a Matrix First, let's create a NumPy matrix to work with ? import numpy as np # Create a 2x3 matrix with random numbers matrix = np.random.rand(2, 3) print("Matrix:") print(matrix) Matrix: [[0.37454012 0.95071431 0.73199394] [0.59865848 0.15601864 0.15599452]] Finding Rows and Columns Using shape The shape attribute returns a tuple where the first element is the number ...

Read More

How to create an identity matrix using Numpy?

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 5K+ Views

An identity matrix is a square matrix where diagonal elements are 1 and all other elements are 0. NumPy provides the identity() function to create identity matrices efficiently. Syntax numpy.identity(n, dtype=None) Parameters n: Size of the identity matrix (n x n) dtype: Data type of the matrix elements (optional, defaults to float) Creating a Basic Identity Matrix import numpy as np # Create a 3x3 identity matrix identity_matrix = np.identity(3) print(identity_matrix) [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] Specifying Data Type ...

Read More

How to plot ROC curve in Python?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 25-Mar-2026 7K+ Views

The ROC (Receiver Operating Characteristic) curve is a graphical plot used to evaluate binary classification models. It shows the trade-off between true positive rate (sensitivity) and false positive rate (1-specificity) at various threshold settings. Python's sklearn.metrics module provides the plot_roc_curve() method to easily visualize ROC curves for classification models. Steps to Plot ROC Curve Generate a random binary classification dataset using make_classification() method Split the data into training and testing sets using train_test_split() method Train a classifier (like SVM) on the training data using fit() method Plot the ROC curve using plot_roc_curve() method Display the plot ...

Read More

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

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 25-Mar-2026 6K+ Views

The datetime module in Python can be used to find the first day of a given year. This datetime module is widely used for manipulating dates and times in various formats and calculations. Common approaches to find the first day of a given year using Python are as follows ? Datetime Module − Widely used library for manipulating dates and times in various ways. Calendar Module ...

Read More

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

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 252 Views

We need to write a Python program that removes a specific substring from a given string. Python provides several methods to accomplish this task efficiently. Algorithm Step 1: Define a string. Step 2: Use the replace() function to remove the substring from the given string. Step 3: Display the modified string. Using replace() Method The most straightforward approach is using the built-in replace() method to replace the unwanted substring with an empty string ? original_string = "C++ is a object oriented programming language" modified_string = original_string.replace("object oriented", "") print("Original:", original_string) print("Modified:", modified_string) ...

Read More

Print dates of today, yesterday and tomorrow using Numpy

Prasad Naik
Prasad Naik
Updated on 25-Mar-2026 559 Views

NumPy provides datetime functionality through the datetime64 data type, allowing you to easily work with dates. You can calculate today's, yesterday's, and tomorrow's dates using np.datetime64() and np.timedelta64() functions. Understanding DateTime64 The datetime64 function creates date objects, while timedelta64 represents time differences. The 'D' parameter specifies the unit as days − import numpy as np # Get today's date today = np.datetime64('today', 'D') print("Today's Date:", today) Today's Date: 2024-01-15 Calculating Yesterday and Tomorrow You can add or subtract timedelta64 objects to get past or future dates ? ...

Read More
Showing 5151–5160 of 8,546 articles
« Prev 1 514 515 516 517 518 855 Next »
Advertisements