Server Side Programming Articles

Page 224 of 2109

Determine common type following standard coercion rules in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 248 Views

In NumPy, find_common_type() determines the common data type following standard coercion rules. This function helps when working with mixed data types in arrays and scalars, returning the most appropriate common type. Syntax numpy.find_common_type(array_types, scalar_types) Parameters The function takes two parameters: array_types − A list of dtypes or dtype convertible objects representing arrays scalar_types − A list of dtypes or dtype convertible objects representing scalars How It Works The method returns the common data type, which is the maximum of array_types ignoring scalar_types, unless the maximum of scalar_types is of ...

Read More

Return the length of a string array element-wise in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To return the length of a string array element-wise, use the numpy.char.str_len() method in Python NumPy. The method returns an output array of integers representing the length of each string element. Syntax numpy.char.str_len(a) Parameters: a − Array-like of str or unicode Returns: Array of integers representing the length of each string element. Basic Example Let's create a simple string array and find the length of each element ? import numpy as np # Create array of strings names = np.array(['Amy', 'Scarlett', 'Katie', 'Brad', 'Tom']) # Get ...

Read More

Test whether similar int type of different sizes are subdtypes of integer class in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 148 Views

To test whether similar int type of different sizes are subdtypes of integer class, use the numpy.issubdtype() method in Python NumPy. The parameters are the dtype or object coercible to one. Syntax numpy.issubdtype(arg1, arg2) Parameters: arg1: dtype or object coercible to one arg2: dtype or object coercible to one Returns: Boolean value indicating whether arg1 is a subtype of arg2. Testing Signed Integer Subtypes First, let's check if different sized integer types are subtypes of np.signedinteger − import numpy as np # Testing different signed integer sizes ...

Read More

Python - Ranking Rows of Pandas DataFrame

Prateek Jangid
Prateek Jangid
Updated on 26-Mar-2026 1K+ Views

Pandas DataFrame ranking allows you to assign rank values to rows based on a specific column's values. The rank() method is useful for ordering data and identifying the relative position of elements. Creating Sample DataFrame Let's start by creating a DataFrame with game data ? import pandas as pd games = { 'Name': ['Call Of Duty', 'Total Overdose', 'GTA 3', 'Bully'], 'Play Time(hours)': [45, 46, 52, 22], 'Rate': ['Better than Average', 'Good', 'Best', 'Average'] } df = pd.DataFrame(games) print(df) ...

Read More

How to compute the Logarithm of elements of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 2K+ Views

To compute the logarithm of elements of a tensor in PyTorch, we use the torch.log() method. It returns a new tensor with the natural logarithm values of the elements of the original input tensor. Syntax torch.log(input, *, out=None) → Tensor Parameters input − The input tensor containing positive values out (optional) − The output tensor to store the result Steps Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it. ...

Read More

How to get the data type of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 15K+ Views

A PyTorch tensor is homogeneous, meaning all elements share the same data type. You can access the data type of any tensor using the .dtype attribute, which returns the tensor's data type. Syntax tensor.dtype Where tensor is the PyTorch tensor whose data type you want to retrieve. Example 1: Random Tensor Data Type The following example shows how to get the data type of a randomly generated tensor − import torch # Create a tensor of random numbers of size 3x4 T = torch.randn(3, 4) print("Original Tensor T:") print(T) ...

Read More

How to compute the sine of elements of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 663 Views

To compute the sine of elements of a tensor, we use the torch.sin() method. It returns a new tensor with the sine values of the elements of the original input tensor. This function is element-wise and preserves the original tensor's shape. Syntax torch.sin(input, out=None) → Tensor Parameters input − Input tensor containing elements in radians out − Optional output tensor to store the result Example 1: 1D Tensor Computing sine values for a one-dimensional tensor − import torch # Create a 1D tensor T = torch.tensor([1.3, 4.32, ...

Read More

How to squeeze and unsqueeze a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 5K+ Views

In PyTorch, you can modify tensor dimensions using torch.squeeze() and torch.unsqueeze() methods. The squeeze operation removes dimensions of size 1, while unsqueeze adds new dimensions of size 1 at specified positions. Understanding Squeeze Operation The torch.squeeze() method removes all dimensions of size 1 from a tensor. For example, if a tensor has shape (2 × 1 × 3 × 1), squeezing will result in shape (2 × 3). Example import torch # Create a tensor with dimensions of size 1 tensor = torch.ones(2, 1, 2, 1) print("Original tensor shape:", tensor.shape) print("Original tensor:", tensor) ...

Read More

How to compute the histogram of a tensor in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 2K+ Views

The histogram of a tensor is computed using torch.histc(). It returns a histogram represented as a tensor. It takes four parameters: input, bins, min and max. It sorts the elements into equal width bins between min and max. It ignores the elements smaller than the min and greater than the max. Syntax torch.histc(input, bins=100, min=0, max=0) Parameters input − Input tensor bins − Number of histogram bins (default: 100) min − Lower range of bins (default: 0) max − Upper range of bins (default: 0) Basic Example Let's create a ...

Read More

How to find mean across the image channels in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 26-Mar-2026 3K+ Views

RGB images have three channels: Red, Green, and Blue. Computing the mean of pixel values across these channels is a common preprocessing step in computer vision. In PyTorch, we use torch.mean() on image tensors with dim=[1, 2] to calculate channel-wise means. Understanding Image Tensors PyTorch image tensors have shape [C, H, W] where C is channels, H is height, and W is width. Setting dim=[1, 2] computes the mean across height and width dimensions, leaving us with three values (one per channel). Method 1: Using PIL and torch.mean() This approach reads images using PIL and applies ...

Read More
Showing 2231–2240 of 21,090 articles
« Prev 1 222 223 224 225 226 2109 Next »
Advertisements