Calculate the N-th Discrete Difference in Python

AmitDiwan
Updated on 28-Feb-2022 06:53:23

161 Views

To calculate the n-th discrete difference, use the numpy.diff() method. The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively. The diff() method returns the n-th differences. The shape of the output is the same as a except along axis where the dimension is smaller by n. The type of the output is the same as the type of the difference between any two elements of a. This is the same as the type of a in most cases. A notable exception is datetime64, which results in ... Read More

Return Cumulative Sum of Array Elements in Python

AmitDiwan
Updated on 28-Feb-2022 06:49:37

155 Views

To return the cumulative sum of array elements over a given axis treating NaNs as zero, use the nancumprod() 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.The 1st parameter is the input array. The 2nd parameter is the axis along which the cumulative sum is computed. The default (None) is to compute the cumsum over the flattened array. The 3rd parameter is the type of the returned array and of the accumulator in which the elements are summed. If dtype ... Read More

Return True If Cast Between Scalar and Data Type Can Occur in Python

AmitDiwan
Updated on 28-Feb-2022 06:46:30

151 Views

The numpy.can_cast() method returns True if scalar and data type can occur according to the casting rule. The 1st parameter is the scalar or data type or array to cast from. The 2nd parameter is the data type to cast to.StepsAt first, import the required library −import numpy as npChecking if scalar and data type can occur according to the casting rule. −print("Checking with can_cast() method in Numpy") print("Result...", np.can_cast(20, 'i1')) print("Result...", np.can_cast(280, 'i1')) print("Result...", np.can_cast(80, 'u1')) print("Result...", np.can_cast(300.7, np.float32)) print("Result...", np.can_cast(120.6, np.float64)) print("Result...", np.can_cast(7.2e100, np.float32)) print("Result...", np.can_cast(6.5e100, np.float64))Exampleimport numpy as np # The numpy.can_cast() method returns True if ... Read More

Return True If Cast Between Data Types Can Occur According to the Casting Rule in Python

AmitDiwan
Updated on 28-Feb-2022 06:44:30

118 Views

The numpy.can_cast() method returns True if cast between data types can occur according to the casting rule. The 1st parameter is the data type or array to cast from. The 2nd parameter is the data type to cast to.StepsAt first, import the required library −import numpy as npUsing the can_cast() to check if cast between data types can occur according to the casting rule −print("Checking with can_cast() method in Numpy") print("Result...", np.can_cast(np.int32, np.int64)) print("Result...", np.can_cast(np.float64, complex)) print("Result...", np.can_cast(complex, float)) print("Result...", np.can_cast('i8', 'f8')) print("Result...", np.can_cast('i8', 'f4')) print("Result...", np.can_cast('i4', 'S4'))Exampleimport numpy as np # The numpy.can_cast() method returns True if cast ... Read More

Convert Array of Datetimes to Strings in Python

AmitDiwan
Updated on 28-Feb-2022 06:41:26

3K+ 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.StepsAt first, import the required library −import numpy as npCreate an array of datetime. The 'M' type specifies datetime −arr = np.arange('2022-02-20T02:10', 6*60, 60, dtype='M8[m]')To convert an array of datetimes into an array of strings, use the numpy.datetime_as_string() method in Python Numpy −print("Result...", np.datetime_as_string(arr, unit ='m'))Exampleimport numpy ... Read More

Calculate Hypotenuse of a Right Triangle in Python

AmitDiwan
Updated on 28-Feb-2022 06:38:48

469 Views

To get the hypotenuse, use the numpy.hypot() method in Python Numpy. The method returns the hypotenuse of the triangle(s). This is a scalar if both x1 and x2 are scalars. This method is equivalent to sqrt(x1**2 + x2**2), element-wise. If x1 or x2 is scalar_like, it is broadcast for use with each element of the other argument. The parameters are the leg of the triangle(s). If x1.shape != x2.shape, they must be broadcastable to a common shape.StepsAt first, import the required library −import numpy as npCreating an array with integer elements −arr = np.ones((3, 3), dtype=int)Displaying our array −print("Array...", arr)Get ... Read More

Get Trigonometric Inverse Tangent of Array Elements in Python

AmitDiwan
Updated on 28-Feb-2022 06:33:20

1K+ Views

The arctan is a multi-valued function: for each x there are infinitely many numbers z such that tan(z) = x. # The inverse tangent is also known as atan or tan^{-1}.The convention is to return the angle z whose real part lies in [-pi/2, pi/2]. For real-valued input data types, arctan always returns real output. For each value that cannot be expressed as a real number or infinity, it yields nan and sets the invalid floating point error flag. For complex-valued input, arctan is a complex analytic function that has [1j, infj] and [-1j, -infj] as branch cuts, and is ... Read More

Get the Trigonometric Inverse Tangent in Python

AmitDiwan
Updated on 28-Feb-2022 06:30:52

4K+ Views

The arctan is a multi-valued function: for each x there are infinitely many numbers z such that tan(z) = x. The convention is to return the angle z whose real part lies in [-pi/2, pi/2]. The inverse tangent is also known as atan or tan^{-1}.For real-valued input data types, arctan always returns real output. For each value that cannot be expressed as a real number or infinity, it yields nan and sets the invalid floating point error flag. For complex-valued input, arctan is a complex analytic function that has [1j, infj] and [-1j, -infj] as branch cuts, and is continuous ... Read More

Convert Array of Datetimes to Array of Strings in Python

AmitDiwan
Updated on 28-Feb-2022 06:27:11

256 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. We have passed the hours unitStepsAt first, import the required library −import numpy as npCreate an array of datetime. The 'M' type specifies datetime −arr = np.arange('2022-02-20T02:10', 6*60, 60, dtype='M8[m]')Displaying our array −print("Array...", arr)Get the datatype: −print("Array datatype...", arr.dtype) Get the dimensions of the Array −print("Array ... Read More

Compute the Hyperbolic Sine in Python

AmitDiwan
Updated on 28-Feb-2022 06:24:37

582 Views

To compute the Hyperbolic sine, use the numpy.sinh() method in Python Numpy. The method is equivalent to 1/2 * (np.exp(x) - np.exp(-x)) or -1j * np.sin(1j*x). Returns the corresponding hyperbolic sine values. This is a scalar if x is a scalar. The 1st parameter, x is input array. The 2nd and 3rd parameters are optional.The 2nd parameter is an ndarray, A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. The 3rd parameter is the condition is broadcast over the ... Read More

Advertisements