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 224 of 2547
Replace NaN with zero and infinity with large finite numbers in Python
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 MoreReturn the lowest index in the string where substring is found in a range using Python index()
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 MoreReturn the square of the complex-value input in Python
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 MoreReturn the lowest index in the string where substring is found using Python index()
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 MoreCompute log-determinants for a stack of matrices in Python
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 MoreReturn matrix rank of array using Singular Value Decomposition method in Python
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 MoreCompute element-wise arc tangent of x1/x2 choosing the quadrant correctly in Python
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 MoreGet the Trigonometric inverse cosine in Python
The inverse cosine (arccos) is a multivalued function that returns the angle whose cosine equals a given value. In NumPy, the arccos() function returns angles in the range [0, π] radians. For real-valued inputs, it always returns real output, while invalid values (outside [-1, 1]) return nan. To find the trigonometric inverse cosine, use the numpy.arccos() method. The method returns the angle of the array intersecting the unit circle at the given x-coordinate in radians [0, π]. Syntax numpy.arccos(x, out=None, where=True) Parameters The function accepts the following parameters ? x − ...
Read MoreCompute the determinant of an array in linear algebra in Python
The determinant is a scalar value that provides important information about a square matrix in linear algebra. In Python NumPy, we use np.linalg.det() to compute the determinant of an array. Syntax numpy.linalg.det(a) Parameters: a − Input array (must be square matrix) Returns: The determinant of the input array as a scalar value. Basic Example Let's compute the determinant of a 2x2 matrix − import numpy as np # Create a 2x2 array arr = np.array([[5, 10], [12, 18]]) print("Array:") print(arr) # Compute the determinant det ...
Read MoreReturn True if first argument is a typecode lower/equal in type hierarchy in Python
The numpy.issubdtype() method returns True if the first argument is a data type that is lower or equal in the NumPy type hierarchy compared to the second argument. This is useful for checking type compatibility and inheritance relationships in NumPy arrays. Syntax numpy.issubdtype(arg1, arg2) Parameters The method accepts two parameters ? arg1 ? The data type or object coercible to a data type to be tested arg2 ? The data type to compare against in the hierarchy Understanding NumPy Type Hierarchy NumPy has a type hierarchy where specific types ...
Read More