def test_sfa_parameter_computation(dimension, n_samples): current_data = mixed_trigonometric_functions(dimension, n_samples) sfa = SFA() slow_features = sfa.fit_transform(current_data) W, b = sfa.affine_parameters() affine_transformed = np.dot(current_data, W.T) + b assert np.allclose(slow_features, affine_transformed)
def test_sfa_parameter_computation_rank_deficit_nonzero_fill( dimension, rank_deficit): current_data = mixed_trigonometric_functions(dimension, rank_deficit=rank_deficit) sfa = SFA(fill_mode="noise") slow_features = sfa.fit_transform(current_data) with pytest.raises(RuntimeError): W, b = sfa.affine_parameters()
def test_sfa_parameter_computation_rank_deficit_zero_fill( dimension, rank_deficit): current_data = mixed_trigonometric_functions(dimension, rank_deficit=rank_deficit) sfa = SFA(fill_mode="zero") slow_features = sfa.fit_transform(current_data) W, b = sfa.affine_parameters() affine_transformed = np.dot(current_data, W.T) + b assert np.allclose(slow_features, affine_transformed)
def test_sfa_feature_order(dimension, n_samples): current_data = mixed_trigonometric_functions(dimension, n_samples) sfa = SFA() slow_features = sfa.fit_transform(current_data) explicit_delta_values = compute_delta(slow_features) assert np.allclose(explicit_delta_values, np.sort(explicit_delta_values))
def test_sfa_constraints(dimension, n_samples): current_data = mixed_trigonometric_functions(dimension, n_samples) sfa = SFA() slow_features = sfa.fit_transform(current_data) covariance_matrix = np.cov(slow_features.T) assert np.allclose(covariance_matrix, np.eye(dimension))
# Generate latent cosine signals x = np.hstack([np.cos(t), 0.5 * np.cos(t), np.cos(2 * t), 1.5 * np.cos(t)]) # Compute random affine mapping of cosines (observed) A = np.random.normal(0, 1, (dim, dim)) b = np.random.normal(0, 2, (1, dim)) data = np.dot(x, A) + b # Extract slow features from observed data # Plot cosines, mapped data, and extracted features fig, ax = plt.subplots(2 + len(fill_modes), 1, sharex=True) fig.set_size_inches(8, 18) fig.subplots_adjust(hspace=0.5) for d in range(n_slow_features): ax[0].plot(x[:, d]) ax[1].plot(data) for idx, fill_mode in enumerate(fill_modes): sfa = SFA(n_slow_features, fill_mode=fill_mode) slow_features = sfa.fit_transform(data) ax[2 + idx].plot(slow_features[:, :-1]) ax[2 + idx].plot(slow_features[:, -1], linestyle=":", color="purple") ax[2 + idx].set_title(f"Extracted features, fill_mode='{fill_mode}'") ax[2 + idx].set_xlabel("Time t") ax[0].set_title("x(t)") ax[1].set_title("A⋅x(t) + b") for idx in range(2 + len(fill_modes)): ax[idx].set_ylabel("Features") plt.show()
embedded = lem.fit_transform(data) A = lem.affinity_matrix_ restart_rate = 500 n_random_samples = 1500 trajectory = randomWalkFromCSC(sp.sparse.csc_matrix(A), n_random_samples, restart_rate=restart_rate) walk_data = data[trajectory] visited = np.unique(trajectory) non_visited = np.setdiff1d(np.arange(0, n_points), visited) pf = PolynomialFeatures(1) sfa = SFA(2, batch_size=restart_rate if restart_rate > 0 else None) sf = sfa.fit_transform(pf.fit_transform(walk_data)) oos = sfa.transform(pf.transform(data[non_visited])) pca = PCA(2) pc = pca.fit_transform(data) fig = plt.figure() fig.set_size_inches(8, 12) fig.subplots_adjust(hspace=0.5) ax_3d = fig.add_subplot(321, projection='3d') ax_3d.set_title("Wavy circle data") ax_rw = fig.add_subplot(323, projection='3d') ax_rw.set_title("Random walk samples") ax_lem = fig.add_subplot(322) ax_lem.set_title("Spectral embedding")