fit¶
- alpsplot.utils.fit.poly_fit(x, y, x_grid, degree=1)[source]¶
Use a polynomial of certain degree to fit (
x
,y
) series and returny_grid
wrtx_grid
.- Parameters:
x (numpy.ndarray) – The data to fit.
y (numpy.ndarray) – The data to fit.
x_grid (numpy.ndarray) – The x grid array.
degree (int) – The degree of polynomial to fit. Defaults to
1
.
- Returns:
numpy.ndarray – The
y_grid
array wrtx_grid
.- Example:
import numpy as np import matplotlib.pyplot as plt from alpsplot.utils.fit import poly_fit x = np.arange(10, step=0.5) x_grid = np.arange(10, step=0.1) y = x + 3 * np.sin(x) y1_grid = poly_fit(x, y, x_grid) y2_grid = poly_fit(x, y, x_grid, degree=4) plt.scatter(x, y, color='red') plt.plot(x_grid, y1_grid, color='green', label='degree = 1') plt.plot(x_grid, y2_grid, color='blue', label='degree = 4') plt.legend() plt.show()
- alpsplot.utils.fit.tanh_fit(x, y, x_grid, degree=1, mean_offset=0.0, eps=1e-05)[source]¶
Use a tanh(polynomial) of certain degree to fit (
x
,y
) series and returny_grid
wrtx_grid
.- Parameters:
x (numpy.ndarray) – The data to fit.
y (numpy.ndarray) – The data to fit.
x_grid (numpy.ndarray) – The x grid array.
degree (int) – The degree of polynomial to fit. Defaults to
1
.mean_offset (float) – The mean offset. Defaults to
0.0
.eps (float) – to control the scale. Defaults to
1e-5
.
- Returns:
numpy.ndarray – the
y_grid
array wrtx_grid
.- Example:
import numpy as np import matplotlib.pyplot as plt from alpsplot.utils.fit import tanh_fit x = np.arange(3, 4, step=0.1) x_grid = np.arange(3, 4, step=0.02) y = 3 * np.tanh(x**3 - 3 * x**2 - 7) + 4 y1_grid = tanh_fit(x, y, x_grid) y2_grid = tanh_fit(x, y, x_grid, mean_offset=0.5) y3_grid = tanh_fit(x, y, x_grid, degree=3) plt.scatter(x, y, color='red') plt.plot(x_grid, y1_grid, color='green', label='degree = 1') plt.plot(x_grid, y2_grid, color='orange', label='degree = 1 (offset)') plt.plot(x_grid, y3_grid, color='blue', label='degree = 3') plt.legend() plt.show()
- alpsplot.utils.fit.atan_fit(x, y, x_grid, degree=1, mean_offset=0.0, eps=1e-05)[source]¶
Use a tanh(polynomial) of certain degree to fit (
x
,y
) series and returny_grid
wrtx_grid
.- Parameters:
x (numpy.ndarray) – The data to fit.
y (numpy.ndarray) – The data to fit.
x_grid (numpy.ndarray) – The x grid array.
degree (int) – The degree of polynomial to fit. Defaults to
1
.mean_offset (float) – The mean offset. Defaults to
0.0
.eps (float) – to control the scale. Defaults to
1e-5
.
- Returns:
numpy.ndarray – The
y_grid
array wrtx_grid
.- Example:
import numpy as np import matplotlib.pyplot as plt from alpsplot.utils.fit import atan_fit x = np.arange(3, 4, step=0.1) x_grid = np.arange(3, 4, step=0.02) y = 3 * np.arctan(x**3 - 3 * x**2 - 7) + 4 y1_grid = atan_fit(x, y, x_grid) y2_grid = atan_fit(x, y, x_grid, mean_offset=0.5) y3_grid = atan_fit(x, y, x_grid, degree=3) plt.scatter(x, y, color='red') plt.plot(x_grid, y1_grid, color='green', label='degree = 1') plt.plot(x_grid, y2_grid, color='orange', label='degree = 1 (offset)') plt.plot(x_grid, y3_grid, color='blue', label='degree = 3') plt.legend() plt.show()
- alpsplot.utils.fit.inverse_fit(x, y, x_grid, degree=1, sign=None, eps=1e-05)[source]¶
Use a tanh(polynomial) of certain degree to fit (
x
,y
) series and returny_grid
wrtx_grid
.- Parameters:
x (numpy.ndarray) – The data to fit.
y (numpy.ndarray) – The data to fit.
x_grid (numpy.ndarray) – The x grid array.
degree (int) – The degree of polynomial to fit. Defaults to
1
.sign (bool) – Whether reciprocal is positive,
None
means auto pick. Defaults toNone
.eps (float) – . Defaults to
1e-5
.
- Returns:
numpy.ndarray – the
y_grid
array wrtx_grid
.- Example:
import numpy as np import matplotlib.pyplot as plt from alpsplot.utils.fit import inverse_fit fig, (ax1, ax2) = plt.subplots(1,2) x = np.arange(1, 2, step=0.1) x_grid = np.arange(1, 2, step=0.01) y = 6 / (x**3 - 2 * x**2 + 4 * x - 2) + 4 y1_grid = inverse_fit(x, y, x_grid, eps=1) y2_grid = inverse_fit(x, y, x_grid, degree=3, eps=1) ax1.scatter(x, y, color='red') ax1.plot(x_grid, y1_grid, color='green', label='degree = 1') ax1.plot(x_grid, y2_grid, color='blue', label='degree = 3') ax1.legend() y1_grid = inverse_fit(x, -y, x_grid, eps=1) y2_grid = inverse_fit(x, -y, x_grid, degree=3, eps=1) ax2.scatter(x, -y, color='red') ax2.plot(x_grid, y1_grid, color='green', label='degree = 1') ax2.plot(x_grid, y2_grid, color='blue', label='degree = 3') ax2.legend() plt.show()
- alpsplot.utils.fit.interp_fit(x, y, x_grid, **kwargs)[source]¶
Use
scipy.interpolate.UnivariateSpline()
to fit (x
,y
) series and returny_grid
wrtx_grid
.- Parameters:
x (numpy.ndarray) – The data to fit.
y (numpy.ndarray) – The data to fit.
x_grid (numpy.ndarray) – The x grid array.
**kwargs – The lower bound of y. Defaults to
0.0
.
- Returns:
numpy.ndarray – The
y_grid
array wrtx_grid
.- Example:
import numpy as np import matplotlib.pyplot as plt from alpsplot.utils.fit import interp_fit x = np.arange(10, step=0.5) x_grid = np.arange(10, step=0.1) y = x + 3 * np.sin(x) y_grid = interp_fit(x, y, x_grid) plt.scatter(x, y, color='red') plt.plot(x_grid, y_grid, color='green') plt.legend() plt.show()