Selected Reading

SciPy - find() Method



The SciPy find() method is used to find an array of elements indices that satisfy the given condition. Also, we can say this method returns the list of physical_constant keys, including a given string.

In Numpy, the find() method is similar with respect to nonzero() and where(). Below is the description of these method −

  • non-zero(): It returns the array elements indices which are non-zero.
  • where(): This method satisfy the given condition based on input array elements indices.

Syntax

Following is the syntax of the SciPy find() method −

find(key)

Parameters

This function accepts only a single parameter −

  • key: The key is a physical_constant which acts a string.

Return value

It has two cases −

  • return the result based on specific module.
  • return the result of physical constant.

Example 1

Following is the example that illustrate the usage of SciPy find() method.

from scipy.constants import find, physical_constants
result = find('boltzmann')
print(result)

Output

The above code produces the following result −

['Boltzmann constant', 'Boltzmann constant in Hz/K', 'Boltzmann constant in eV/K', 'Boltzmann constant in inverse meter per kelvin', 'Stefan-Boltzmann constant']

Example 2

Here, we use the another pysical_constants as a string parameter to display the result.

from scipy.constants import find, physical_constants
result = find('radius')
print(result)

Output

The above code produces the following result −

['Bohr radius', 'classical electron radius', 'deuteron rms charge radius', 'proton rms charge radius']

Example 3

Here, we create a space matrix to fill the data of rows and columns and using find() to obtain the row indices, column indices and values of non-zero element.

The sparse matrix is a type of matrix that contains a maximum 0th value. Thus, this matrix is generally used in the field of machine learning and it saves computing time and storage.
import numpy as np
from scipy.sparse import csr_matrix, find

# Create a sparse matrix
A = csr_matrix([[0, 0, 1], [1, 0, 0], [0, 2, 0]])

row, col, data = find(A)

print("The row indices of non-zero elements:", row)
print("The column indices of non-zero elements:", col)
print("The values of non-zero elements:", data)

Output

The above code produces the following result −

The row indices of non-zero elements: [0 1 2]
The column indices of non-zero elements: [2 0 1]
The values of non-zero elements: [1 1 2
scipy_reference.htm
Advertisements