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 221 of 2547
Compute 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 MoreReturn the data type with the smallest size and scalar kind to which both the given types be safely cast in Python
The numpy.promote_types() method returns the data type with the smallest size and scalar kind to which both given types can be safely cast. This is useful when you need to determine the appropriate data type for operations involving mixed types. Syntax numpy.promote_types(type1, type2) Parameters type1: First data type (string or numpy dtype) type2: Second data type (string or numpy dtype) Return Value Returns the promoted data type that can safely hold values from both input types. The returned data type is always in native byte order. Basic Examples Let's start ...
Read MoreReturn the imaginary part of the complex argument in Python
To return the imaginary part of a complex number or array, use numpy.imag(). This method extracts the imaginary component from complex numbers. If the input is real, it returns the same type; if complex, it returns float values representing the imaginary parts. Syntax numpy.imag(val) Parameters: val − Input array or scalar with complex numbers Returns: Array of imaginary parts as float values Basic Example with Single Complex Number import numpy as np # Single complex number z = 5 + 3j print("Complex number:", z) print("Imaginary part:", np.imag(z)) ...
Read MoreReturn the cumulative sum of array elements over given axis treating NaNs as zero in Python
To return the cumulative sum of array elements over a given axis treating NaNs as zero, use the nancumsum() method. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. The method returns a new array with cumulative sums computed along the specified axis. Zeros are returned for slices that are all-NaN or empty. Cumulative sum works progressively: 5, 5+10, 5+10+15, 5+10+15+20. Syntax numpy.nancumsum(a, axis=None, dtype=None, out=None) Parameters a − Input array axis − Axis along which the cumulative sum is computed. Default (None) computes ...
Read MoreGet the Trigonometric tangent of an array of angles given in degrees with Python
The trigonometric tangent function returns the ratio of sine to cosine for each angle. To calculate the tangent of angles given in degrees, we use NumPy's tan() function combined with degree-to-radian conversion. Syntax numpy.tan(x, out=None, where=True) Parameters x − Input array of angles in radians out − Optional output array where results are stored where − Optional condition to control where calculation is applied Converting Degrees to Radians Since numpy.tan() expects angles in radians, we multiply degrees by π/180 ? import numpy as np # Array of ...
Read MoreReturn the gradient of an N-dimensional array over given axis in Python
The gradient function in NumPy computes the gradient of an N-dimensional array using numerical differentiation. It calculates derivatives along specified axes using central differences for interior points and forward/backward differences at boundaries. Syntax numpy.gradient(f, *varargs, axis=None, edge_order=1) Parameters The function accepts the following parameters: f − N-dimensional array containing samples of a scalar function varargs − Spacing between f values (default: unitary spacing for all dimensions) axis − Axis or axes along which the gradient is calculated (default: None for all axes) edge_order − Order of accuracy for boundary differences (1 or ...
Read MoreTest whether similar float type of different sizes are subdtypes of floating class in Python
To test whether similar float types of different sizes are subtypes of the floating class, use the numpy.issubdtype() method in Python NumPy. The method accepts a dtype or object coercible to one as parameters. Syntax numpy.issubdtype(arg1, arg2) Parameters: arg1 − First data type to check arg2 − Second data type to check against Returns: Boolean value indicating if arg1 is a subtype of arg2 Testing Float Types First, import the required library − import numpy as np Now, use the issubdtype() method to check if different ...
Read MoreGet the Trigonometric cosine of an array of angles given in degrees with Python
To find the trigonometric cosine of an array of angles given in degrees, use the numpy.cos() method in Python NumPy. Since numpy.cos() expects angles in radians, you need to convert degrees to radians by multiplying by π/180. Syntax numpy.cos(x, out=None, where=True) Parameters The parameters of numpy.cos() are: x - Input array of angles in radians out - Optional output array where results are stored where - Optional condition to apply the function selectively Converting Degrees to Radians Since cosine function expects radians, multiply degrees by π/180 ? ...
Read MoreIntegrate along the given axis using the composite trapezoidal rule in Python
To integrate along the given axis using the composite trapezoidal rule, use the numpy.trapz() method. The trapezoidal rule approximates the definite integral by dividing the area under a curve into trapezoids and summing their areas. If x is provided, the integration happens in sequence along its elements. The method returns the definite integral of y as an n-dimensional array approximated along a single axis. If y is 1-dimensional, the result is a float. If n is greater than 1, the result is an (n-1) dimensional array. Syntax numpy.trapz(y, x=None, dx=1.0, axis=-1) Parameters ...
Read More