Programming Articles

Page 219 of 2547

Return the cumulative sum of array elements treating NaNs as zero but change the type of result in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 203 Views

To return the cumulative sum of array elements over a given axis treating NaNs as zero, use the numpy.nancumsum() method. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. Zeros are returned for slices that are all-NaN or empty. Syntax numpy.nancumsum(a, axis=None, dtype=None, out=None) Parameters The function accepts the following parameters: a − Input array axis − Axis along which the cumulative sum is computed. Default is None (flattened array) dtype − ...

Read More

Return True if cast between data types can occur according to the casting rule in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 181 Views

The numpy.can_cast() method returns True if a cast between data types can occur according to NumPy's casting rules. It takes two parameters: the source data type and the target data type to cast to. Syntax numpy.can_cast(from_dtype, to_dtype, casting='safe') Parameters from_dtype: The data type or array to cast from to_dtype: The data type to cast to casting: Controls what kind of data casting may occur ('no', 'equiv', 'safe', 'same_kind', 'unsafe') Basic Usage Let's check if various data type conversions are possible ? import numpy as np print("Checking with can_cast() method ...

Read More

Convert an array of datetimes into an array of strings passing units in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 390 Views

To convert an array of datetimes into an array of strings, use the numpy.datetime_as_string() method in Python NumPy. The method returns an array of strings the same shape as the input array. The first parameter is the array of UTC timestamps to format. The "units" parameter sets the datetime unit to change the precision. Syntax numpy.datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind') Parameters The key parameters are ? arr − Array of datetime64 values to convert unit − Datetime unit for precision ('Y', 'M', 'D', 'h', 'm', 's', 'ms', etc.) timezone − Timezone handling ('naive', ...

Read More

Return an array with the number of nonoverlapping occurrences of substring in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 252 Views

To return an array with the number of non-overlapping occurrences of substring, use the numpy.char.count() method in Python NumPy. The numpy.char module provides vectorized string operations for arrays of type numpy.str_ or numpy.bytes_. Syntax numpy.char.count(a, sub, start=0, end=None) Parameters a − Input array of strings sub − Substring to search for start − Optional start position (default: 0) end − Optional end position (default: None) Example 1: Basic Character Count Count occurrences of a specific character in string array ? import numpy as np # Create a One-Dimensional array ...

Read More

Round to nearest integer towards zero in Python

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

To round to the nearest integer towards zero in Python, use the numpy.fix() method. It performs element-wise rounding of float arrays towards zero, which means it truncates the decimal part. For positive numbers, this rounds down, and for negative numbers, it rounds up towards zero. Syntax numpy.fix(x, out=None) Parameters The function accepts the following parameters: x − Array of floats to be rounded out − Optional output array where results are stored Basic Example Let's see how np.fix() rounds different types of numbers ? import numpy as ...

Read More

Multiply one polynomial to another in Python

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

To multiply one polynomial to another, use the numpy.polynomial.polynomial.polymul() method in Python. This function returns the multiplication of two polynomials represented as coefficient arrays. The arguments are sequences of coefficients from lowest order term to highest, i.e., [1, 2, 3] represents the polynomial 1 + 2*x + 3*x². The method returns the coefficient array representing their product. The parameters c1 and c2 are 1-D arrays of coefficients representing polynomials, ordered from lowest order term to highest. Syntax numpy.polynomial.polynomial.polymul(c1, c2) Parameters: c1 − 1-D array of polynomial coefficients (lowest to highest order) c2 − ...

Read More

Subtract one polynomial to another in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 509 Views

To subtract one polynomial from another in Python, use the numpy.polynomial.polynomial.polysub() method. This function returns the difference of two polynomials c1 - c2. The arguments are sequences of coefficients from lowest order term to highest, i.e., [1, 2, 3] represents the polynomial 1 + 2*x + 3*x². The method returns a coefficient array representing their difference. The parameters c1 and c2 are 1-D arrays of polynomial coefficients ordered from low to high. Syntax numpy.polynomial.polynomial.polysub(c1, c2) Parameters c1, c2: 1-D arrays of polynomial coefficients ordered from low to high degree. Example Let's ...

Read More

Add one polynomial to another in Python

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

To add one polynomial to another in Python, use the numpy.polynomial.polynomial.polyadd() method. This function returns the sum of two polynomials c1 + c2. The arguments are sequences of coefficients from lowest order term to highest, i.e., [1, 2, 3] represents the polynomial 1 + 2*x + 3*x**2. The numpy.polynomial.polynomial module provides a number of objects useful for dealing with polynomials, including a Polynomial class that encapsulates the usual arithmetic operations. Syntax numpy.polynomial.polynomial.polyadd(c1, c2) Parameters The method takes the following parameters ? c1, c2 ? 1-D arrays of polynomial coefficients ordered from ...

Read More

Compute the Moore-Penrose pseudoinverse of a stack of matrices in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 486 Views

The Moore-Penrose pseudoinverse is a generalization of the matrix inverse for non-square or singular matrices. In NumPy, you can compute the pseudoinverse of a stack of matrices using numpy.linalg.pinv(), which uses singular value decomposition (SVD) internally. Syntax numpy.linalg.pinv(a, rcond=1e-15, hermitian=False) Parameters The function accepts the following parameters: a − Matrix or stack of matrices to be pseudo-inverted rcond − Cutoff for small singular values. Values ≤ rcond × largest_singular_value are set to zero hermitian − If True, assumes the matrix is Hermitian for more efficient computation Example Let's compute ...

Read More

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
Showing 2181–2190 of 25,469 articles
« Prev 1 217 218 219 220 221 2547 Next »
Advertisements