In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pwlf
%matplotlib inline
In [2]:
np.random.seed(123)
n = 100
n_data_sets = 100
n_segments = 6
# generate sine data
x = np.linspace(0, 10, n)
y = np.zeros((n_data_sets, n))
for i in range(n_data_sets):
y[i] = np.sin(x * np.pi / 2)
# add noise to the data
y[i] = np.random.normal(0, 0.05, 100) + y[i]
X = np.tile(x, n_data_sets)
In [3]:
# plot all of the data
plt.figure()
plt.plot(X.flatten(), y.flatten(), '.')
plt.show()
In [4]:
# perform a single fit to all of the data
my_pwlf_global = pwlf.PiecewiseLinFit(X.flatten(), y.flatten())
my_pwlf_global.fit(n_segments)
Out[4]:
In [5]:
# plot all of the data with global fit
yhat = my_pwlf_global.predict(x)
plt.figure()
plt.plot(X.flatten(), y.flatten(), '.')
plt.plot(x, yhat, '-')
plt.plot()
plt.show()
In [6]:
# for each data set, fit a pwlf model using the global breaks
my_pwlf_models = []
slopes = []
for i in range(n_data_sets):
temp_pwlf = pwlf.PiecewiseLinFit(x, y[i])
temp_pwlf.fit_with_breaks(my_pwlf_global.fit_breaks)
my_pwlf_models.append(temp_pwlf)
slopes.append(temp_pwlf.slopes)
In [7]:
slopes = np.array(slopes)
print(my_pwlf_global.slopes)
print(slopes.mean(axis=0))
print(slopes.std(axis=0))
In [ ]: