Basic research is what I am doing when I don't know what I am doing.
-Werner von Braun
Like Numpy, we will use mostly use SciPy indirectly. For the most part, our interaction with SciPy will be via Pandas (for many statistical functions) and Scikit-Learn (for linear algebra and optimization functions). The only items that we may use directly are the stats functions in scipy.stats and the optimization functions in scipy.optimize.
This is overkill 95% of the time. You can just as easily run your function over a range and simply pick the local maximum value. That said, if you want to optimize, Scipy has functions for that.
In [ ]:
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
# Import minimize
from scipy.optimize import minimize
# Create a function to find the minimum of
def function_with_min(x):
# Add 1 to shift height and square
x = (x + 1) ** 2
# Add 3 to shift alignment
x = x + 3
# Return vlaue
return x
# Get minimum with minimize
result = minimize(function_with_min, 0)
print('Minimum input is {:.3}.'.format(result['x'][0]))
print('Minimum output is {:.3}.'.format(result['fun']))
# Create example points
x = np.arange(-10, 10, .1)
# Vectorize function
vec_function = np.vectorize(function_with_min)
y = vec_function(x)
# Less efficient for large datasets, but you can also
# y = [function_with_min(scalar) for scalar in x]
# Plot
plt.plot(x, y)
plt.show()
In [ ]:
# Create a function to find the maximum of
def function_with_max(x):
# Add 1 to shift height and square
x = (x - 1) ** 2
# Invert
x = x * -1
# Add 20
x = x + 20
return x
# Invert function result
result = minimize(lambda x: function_with_max(x) * -1, 0)
print('Maximum input is {:.3}.'.format(result['x'][0]))
print('Maximum output is {:.3}.'.format(result['fun']))
# Create example points
x = np.arange(-10, 10, .1)
# Vectorize function
vec_function = np.vectorize(function_with_max)
y = vec_function(x)
# Plot
plt.plot(x, y)
Also overkill 95% of the time. I cannot pronounce (let alone understand) most of the stuff in here. Pandas and numpy have functions that cover most of this, but if you need a general Kolmogorov-Smirnov one-sided test or an inverted Weibull continuous random variable, this is where you would look.