SciPy - value() Method



The SciPy value() method is a part of physical_constants dictionary which is indexed by a key. The usage of this method referred to the context of optimization, interpolaration or, some other functions within of this library.

Syntax

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

value(key)

Parameters

This method accepts only a single parameter −

  • key: A string that defines the name of physical constant.

Return value

This method returns the numerical value like a float.

Example 1

Following is the basic SciPy value() method that illustrates the result of physical constants.

from scipy import constants
result = constants.value('elementary charge')
print("The result of physical constant(elementary charge): ", result)

Output

The above code produces the following result −

The result of physical constant(elementary charge):  1.602176634e-19

Example 2

Below the program illustrates the SciPy optimization that returns the minimum value through the customize function. After performing the optimization rule using minimize() it returns the result as an object associated with an attribute(fun).

from scipy.optimize import minimize
# define the custom function
def obj(x):
    return x**2 + 5*x + 4

# Perform the optimization
result = minimize(obj, 0)

# The optimized(minimum) value of custom function
opt_value = result.fun

print("The result of optimized Value:", opt_value)

Output

The above code produces the following result −

The result of optimized Value: -2.249999999999999

Example 3

This program determines the interpolaration object(interp1d) at variable f. Then it set the float value of 3.5 which evaluate the interpolarate function at specific point.

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

# Data points
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 1, 4, 9, 16, 25])

# interpolation function
f = interp1d(x, y, kind='quadratic')

# Evaluate the interpolated function at a specific point
x_new = 3.5
y_new = f(x_new)

print("Interpolated Value at x=3.5:", y_new)

# Plot the data and the interpolation
plt.plot(x, y, 'o', label='data points')
x_dense = np.linspace(0, 5, 100)
plt.plot(x_dense, f(x_dense), '-', label='Quadratic interpolation')
plt.legend()
plt.show()

Output

The above code produces the following result −

scipy_value_method_one
scipy_reference.htm
Advertisements