Found 10784 Articles for Python

Get the Outer product of two arrays in Python

AmitDiwan
Updated on 25-Feb-2022 07:08:29

2K+ Views

To get the Outer product of two arrays, use the numpy.outer() method in Python. The 1st parameter a is the first input vector. Input is flattened if not already 1-dimensional. The 2nd parameter b is the second input vector. Input is flattened if not already 1-dimensional. The 3rd parameter out is a location where the result is stored.Given two vectors, a = [a0, a1, ..., aM] and b = [b0, b1, ..., bN], the outer product [1] is −[[a0*b0 a0*b1 ... a0*bN ] [a1*b0 . [ ... . [aM*b0 aM*bN ]]StepsAt first, import the required libraries-import numpy as npCreating two ... Read More

Get the Inner product of an array and a scalar in Python

AmitDiwan
Updated on 25-Feb-2022 07:06:22

163 Views

To get the Inner product of an array and a scalar, use the numpy.inner() method in Python. Ordinary inner product of vectors for 1-D arrays, in higher dimensions a sum product over the last axes. The parameters are 1 and b, two vectors. If a and b are nonscalar, their last dimensions must match.StepsAt first, import the required libraries-import numpy as npCreate an array using numpy.eye(). This method returns a 2-D array with ones on the diagonal and zeros elsewhere −arr = np.eye(5)The val is the scalar −val = 2Check the datatype −print("Datatype of Array...", arr.dtype) Check the Dimension −print("Dimensions ... Read More

Compute the multiplicative inverse of a matrix in Python

AmitDiwan
Updated on 25-Feb-2022 07:02:16

1K+ Views

To compute the (multiplicative) inverse of a matrix, use the numpy.linalg.inv() method in Python. Given a square matrix a, return the matrix ainv satisfying dot(a, ainv) = dot(ainv, a) = eye(a.shape[0]). The method returns (Multiplicative) inverse of the matrix a. The 1st parameter, a is a Matrix to be inverted.StepsAt first, import the required libraries-import numpy as np from numpy.linalg import invCreate an array −arr = np.array([[ 5, 10], [ 15, 20 ]])Display the array −print("Our Array...", arr)Check the Dimensions −print("Dimensions of our Array...", arr.ndim)Get the Datatype −print("Datatype of our Array object...", arr.dtype)Get the Shape −print("Shape of our Array object...", ... Read More

Solve the tensor equation in Python

AmitDiwan
Updated on 25-Feb-2022 06:55:20

534 Views

To solve the tensor equation, use the numpy.linalg.tensorsolve() method in Python. It is assumed that all indices of x are summed over in the product, together with the rightmost indices of a, as is done in, for example, tensordot(a, x, axes=b.ndim).The 1st parameter, a is a coefficient tensor, of shape b.shape + Q. Q, a tuple, equals the shape of that sub-tensor of a consisting of the appropriate number of its rightmost indices, and must be such that prod(Q) == prod(b.shape). The 2nd parameter, b is a right-hand tensor, which can be of any shape. The 3rd parameter, axis is ... Read More

Replace infinity with large finite numbers but fill NaN values in Python

AmitDiwan
Updated on 25-Feb-2022 06:53:13

934 Views

To replace NaN with zero and infinity with large finite numbers, use the numpy.nan_to_num() method in Python. The method returns, x, with the non-finite values replaced. If copy is False, this may be x itself. The 1st parameter is the input data. The 2nd parameter is copy, whether to create a copy of x (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True.The 3rd parameter is nan, the value to be used to fill NaN values. If no value is passed then NaN values ... Read More

Replace NaN with zero and infinity with large finite numbers in Python

AmitDiwan
Updated on 25-Feb-2022 06:51:14

293 Views

To replace NaN with zero and infinity with large finite numbers, use the numpy.nan_to_num() method in Python. The method returns, x, with the non-finite values replaced. If copy is False, this may be x itself. The 1st parameter is the input data. The 2nd parameter is copy, whether to create a copy of x (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True.The 3rd parameter is nan, the value to be used to fill NaN values. If no value is passed then NaN values ... Read More

Return the lowest index in the string where substring is found in a range using Python index()

AmitDiwan
Updated on 25-Feb-2022 06:42:28

98 Views

Return the lowest index in the string where substring sub is found using the numpy.char.index() method in Python Numpy. The method returns the output array of ints. Raises ValueError if sub is not found. The first parameter is the input array. The second parameter is the substring to be searched. The third and fourth parameter are optional arguments, wherein start and end are interpreted as in slice notation.StepsAt first, import the required library −import numpy as npCreate a One-Dimensional array of strings −arr = np.array(['KATIE', 'KATE', 'CRATE']) Displaying our array −print("Array...", arr)Get the datatype −print("Array datatype...", arr.dtype) Get the dimensions ... Read More

Return the square of the complex-value input in Python

AmitDiwan
Updated on 25-Feb-2022 06:43:41

141 Views

To return the element-wise square of the array input, use the numpy.square() method in Python. The method returns the element-wise x*x, of the same shape and dtype as x. This is a scalar if x is a scalar. The 1st parameter, x is the input data. The 2nd parameter, out is 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. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.The 3rd parameter, ... Read More

Return the lowest index in the string where substring is found using Python index()

AmitDiwan
Updated on 25-Feb-2022 06:40:31

225 Views

Return the lowest index in the string where substring sub is found using the numpy.char.index() method in Python Numpy. The method returns the output array of ints. Raises ValueError if sub is not found. The first parameter is the input array. The second parameter is the substring to be searched.StepsAt first, import the required library −import numpy as npCreate a One-Dimensional array of strings −arr = np.array(['KATIE', 'KATE']) Displaying our array −print("Array...", arr)Get the datatype −print("Array datatype...", arr.dtype) Get the dimensions of the Array −print("Array Dimensions...", arr.ndim)Get the shape of the Array −print("Our Array Shape...", arr.shape) Get the number of ... Read More

Solve a linear matrix equation or system of linear scalar equations in Python

AmitDiwan
Updated on 25-Feb-2022 06:40:12

9K+ Views

To solve a linear matrix equation, use the numpy.linalg.solve() method in Python. The method computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b. Returns a solution to the system a x = b. Returned shape is identical to b. The 1st parameter a is the Coefficient matrix. The 2nd parameter b is the Ordinate or “dependent variable” values.StepsAt first, import the required libraries -import numpy as npCreating two 2D numpy arrays using the array() method. Consider the system of equations x0 + 2 * x1 = 1 and 3 * x0 + ... Read More

Advertisements