Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 223 of 2547
Convert an array of datetimes into an array of strings passing units in Python
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 MoreReturn an array with the number of nonoverlapping occurrences of substring in Python
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 MoreRound to nearest integer towards zero in Python
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 MoreMultiply one polynomial to another in Python
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 MoreSubtract one polynomial to another in Python
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 MoreAdd one polynomial to another in Python
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 MoreCompute the Moore-Penrose pseudoinverse of a stack of matrices in Python
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 MoreGet the Outer product of two arrays in Python
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 MoreSolve the tensor equation in Python
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 MoreReplace infinity with large finite numbers but fill NaN values in Python
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