def setUp(self): """Creates test data""" random_state = np.random.RandomState(0) modes_location = np.concatenate( (random_state.normal(-.3, .04, size=15), random_state.normal(.3, .04, size=15))) idx = np.arange(30) random_state.shuffle(idx) modes_location = modes_location[idx] self.modes_location = modes_location self.y = np.array(15 * [0] + 15 * [1])[idx] self.X = make_multimodal_samples(n_samples=30, modes_location=modes_location, noise=.05, random_state=random_state) self.X2 = make_multimodal_samples(n_samples=30, modes_location=modes_location, noise=.05, random_state=1) self.probs = np.array(15 * [[1., 0.]] + 15 * [[0., 1.]])[idx] # Dataset with outliers fd_clean = make_sinusoidal_process(n_samples=25, error_std=0, phase_std=0.1, random_state=0) fd_outliers = make_sinusoidal_process(n_samples=2, error_std=0, phase_mean=0.5, random_state=5) self.fd_lof = fd_outliers.concatenate(fd_clean)
def test_shift_registration(self): """Test wrapper of shift_registration_deltas""" fd = make_sinusoidal_process(n_samples=2, error_std=0, random_state=1) fd_reg = shift_registration(fd) deltas = shift_registration_deltas(fd) np.testing.assert_array_almost_equal(fd_reg.data_matrix, fd.shift(deltas).data_matrix)
def test_shift_registration_deltas(self): fd = make_sinusoidal_process(n_samples=2, error_std=0, random_state=1) deltas = shift_registration_deltas(fd).round(3) np.testing.assert_array_almost_equal(deltas, [-0.022, 0.03]) fd = fd.to_basis(Fourier()) deltas = shift_registration_deltas(fd).round(3) np.testing.assert_array_almost_equal(deltas, [-0.022, 0.03])
def test_fit_and_transform(self): """Test wrapper of shift_registration_deltas""" fd = make_sinusoidal_process(n_samples=2, error_std=0, random_state=10) reg = ShiftRegistration() response = reg.fit(self.fd) # Check attributes and returned value self.assertTrue(hasattr(reg, 'template_')) self.assertTrue(response is reg) fd_registered = reg.transform(fd) deltas = reg.deltas_.round(3) np.testing.assert_allclose(deltas, [0.071, -0.072])
import matplotlib.pyplot as plt from skfda.datasets import make_sinusoidal_process from skfda.preprocessing.registration import ShiftRegistration from skfda.representation.basis import Fourier ############################################################################## # In this example we will use a # :func:`sinusoidal process <skfda.datasets.make_sinusoidal_process>` # synthetically generated. This dataset consists in a sinusoidal wave with # fixed period which contanis phase and amplitude variation with gaussian # noise. # # In this example we want to register the curves using a translation # and remove the phase variation to perform further analysis. fd = make_sinusoidal_process(random_state=1) fd.plot() ############################################################################## # We will smooth the curves using a basis representation, which will help us # to remove the gaussian noise. Smoothing before registration # is essential due to the use of derivatives in the optimization process. # Because of their sinusoidal nature we will use a Fourier basis. fd_basis = fd.to_basis(Fourier(n_basis=11)) fd_basis.plot() ############################################################################## # We will use the # :func:`~skfda.preprocessing.registration.ShiftRegistration` transformer, # which is suitable due to the periodicity of the dataset and the small
import skfda import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d from skfda.datasets import make_sinusoidal_process from skfda.representation.interpolation import SplineInterpolator fd = make_sinusoidal_process(n_samples=1, n_features=6, random_state=3) + 1.2 fd.interpolator = SplineInterpolator(interpolation_order=3) plt.figure("original-function") fd.scatter(label='original values') fd.plot(label='interpolation', color='C0', linestyle="--") plt.legend() x = fd.sample_points[0] y = fd.data_matrix.squeeze() ymin, ymax = plt.ylim() ymin = 0 for i in range(len(x)): plt.plot([x[i], x[i]], [-1.2, y[i]], color='C0', linewidth=1) plt.ylim(ymin, ymax) plt.yticks([]) plt.tight_layout() plt.figure("function-resampled")
def setUp(self): """Initialization of samples""" self.X = make_sinusoidal_process(error_std=0, random_state=0) self.shift_registration = ShiftRegistration().fit(self.X)
def setUp(self): """Initialization of samples""" self.fd = make_sinusoidal_process(n_samples=2, error_std=0, random_state=1) self.fd.extrapolation = "periodic"
def setUp(self): self.grid = make_sinusoidal_process(n_samples=2, random_state=0) self.basis = self.grid.to_basis(Fourier()) self.dummy_data = [[1, 2, 3], [2, 3, 4]]
# a FDataGrid representing a function :math:`f : \mathbb{R}^2\longmapsto\mathbb{R}^2` is # constructed to show also the support of a multivariate dimensional image. The first # dimension of the image contains sinusoidal processes and the second dimension, # gaussian ones. # # First, the values are generated for each dimension with a function # :math:`f : \mathbb{R}\longmapsto\mathbb{R}` implemented in the # :func:`make_sinusoidal_process method <skfda.datasets.make_sinusoidal_process>` and in the # :func:`make_gaussian_process method <skfda.datasets.make_gaussian_process>`, respectively. # Those functions return FDataGrid objects whose 'data_matrix' store the values needed. n_samples = 10 n_features = 10 fd1 = make_sinusoidal_process(n_samples=n_samples, n_features=n_features, random_state=5) fd1.dataset_label = "Sinusoidal process" fd2 = make_gaussian_process(n_samples=n_samples, n_features=n_features, random_state=1) fd2.dataset_label = "Brownian process" ################################################################################## # After, those values generated for one dimension on the domain are propagated along # another dimension, obtaining a three-dimensional matrix or cube (two-dimensional domain # and one-dimensional image). This is done with both data matrices from the above FDataGrids. cube1 = np.repeat(fd1.data_matrix, n_features).reshape( (n_samples, n_features, n_features)) cube2 = np.repeat(fd2.data_matrix, n_features).reshape(