Articles on Trending Technologies

Technical articles with clear explanations and examples

Get the Outer product of two arrays in Python

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

To get the outer product of two arrays, use the numpy.outer() method in Python. The outer product takes two vectors and produces a matrix where each element is the product of corresponding elements from both vectors. Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product is ? [[a0*b0 a0*b1 ... a0*bN ] [a1*b0 a1*b1 ... a1*bN ] [ ... ... ... ... ] [aM*b0 aM*b1 ... aM*bN ...

Read More

Solve the tensor equation in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 834 Views

To solve tensor equations in Python, use the numpy.linalg.tensorsolve() method. This function solves the tensor equation by finding the solution where all indices of the unknown tensor are summed over in the product with the coefficient tensor. Syntax numpy.linalg.tensorsolve(a, b, axes=None) Parameters The function accepts the following parameters: a − Coefficient tensor of shape b.shape + Q, where Q is a tuple representing the shape of the rightmost indices b − Right-hand tensor that can be of any shape axes − Axes in tensor 'a' to reorder before inversion (optional, default is ...

Read More

Replace infinity with large finite numbers but fill NaN values in Python

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

To replace NaN values and infinity with large finite numbers in Python, use the numpy.nan_to_num() method. This function converts non-finite values (NaN, positive infinity, negative infinity) to finite numbers that can be processed normally. Syntax numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None) Parameters The nan_to_num() function accepts the following parameters ? x ? Input array or scalar copy ? Whether to create a copy (True) or modify in-place (False). Default is True nan ? Value to replace NaN. Default is 0.0 posinf ? Value to replace positive infinity. Default is very large positive number ...

Read More

Replace NaN with zero and infinity with large finite numbers in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 449 Views

In Python, NaN (Not a Number) and infinity values can cause issues in numerical computations. NumPy provides nan_to_num() to replace these non-finite values with usable finite numbers. Syntax numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None) Parameters The function accepts the following parameters: x − Input array containing the data copy − Whether to create a copy (True) or replace in-place (False). Default is True nan − Value to replace NaN with. Default is 0.0 posinf − Value to replace positive infinity with. Default is very large finite number neginf − Value to replace negative ...

Read More

Return the lowest index in the string where substring is found in a range using Python index()

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 202 Views

The numpy.char.index() method returns the lowest index where a substring is found within string arrays. It searches within a specified range and raises ValueError if the substring is not found. Syntax numpy.char.index(a, sub, start=0, end=None) Parameters The method accepts the following parameters: a − Input array of strings sub − Substring to search for start − Starting position for search (optional) end − Ending position for search (optional) Basic Example Let's find the index of substring 'AT' in string arrays − import numpy as np # ...

Read More

Return the square of the complex-value input in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 260 Views

To return the element-wise square of complex-valued arrays, use the numpy.square() method in Python. This method returns the element-wise x*x of the same shape and dtype as the input array. Syntax numpy.square(x, out=None, where=True) Parameters The numpy.square() method accepts the following parameters: x − Input array or scalar out − Optional output array where results are stored where − Condition to broadcast over input (optional) Example Let's create a 2D array with complex numbers and compute their squares ? import numpy as np # Creating a ...

Read More

Return the lowest index in the string where substring is found using Python index()

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 356 Views

The numpy.char.index() method returns the lowest index where a substring is found within each string element of a NumPy array. It raises a ValueError if the substring is not found in any string. Syntax numpy.char.index(a, sub, start=0, end=None) Parameters a − Input array of strings sub − Substring to search for start − Starting position (optional) end − Ending position (optional) Basic Example Let's find the index of substring 'AT' in string arrays ? import numpy as np # Create array of strings arr = np.array(['KATIE', 'KATE']) ...

Read More

Compute log-determinants for a stack of matrices in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 287 Views

To compute log-determinants for a stack of matrices, use the numpy.linalg.slogdet() method in Python. This method returns two arrays: the sign and the natural logarithm of the absolute determinant. The method returns a tuple (sign, logdet) where: sign: represents the sign of the determinant (1, 0, or -1 for real matrices) logdet: natural log of the absolute value of the determinant If the determinant is zero, then sign will be 0 and logdet will be -Inf. The actual determinant equals sign * np.exp(logdet). Syntax numpy.linalg.slogdet(a) Parameters: a: array_like - ...

Read More

Return matrix rank of array using Singular Value Decomposition method in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 731 Views

To return the matrix rank of an array using the Singular Value Decomposition (SVD) method, use the numpy.linalg.matrix_rank() method in Python. The rank of a matrix represents the number of linearly independent rows or columns, calculated as the count of singular values greater than a specified tolerance. Syntax numpy.linalg.matrix_rank(A, tol=None, hermitian=False) Parameters A: Input vector or stack of matrices whose rank needs to be computed. tol: Threshold below which SVD values are considered zero. If None, it's automatically set to S.max() * max(M, N) * eps, where S contains singular values and eps ...

Read More

Compute element-wise arc tangent of x1/x2 choosing the quadrant correctly in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 223 Views

The numpy.arctan2() function computes the element-wise arc tangent of y/x choosing the quadrant correctly. Unlike arctan(), it uses the signs of both arguments to determine which quadrant the angle is in, returning values in the range [-π, π]. Syntax numpy.arctan2(y, x) Parameters y: Array-like, the y-coordinates (first parameter) x: Array-like, the x-coordinates (second parameter) If shapes differ, they must be broadcastable to a common shape. Understanding Quadrants The function determines angles based on coordinate positions ? import numpy as np # Four points in different quadrants x = np.array([1, ...

Read More
Showing 2451–2460 of 61,297 articles
« Prev 1 244 245 246 247 248 6130 Next »
Advertisements