def test_basis_fpca_transform_result(self): n_basis = 9 n_components = 3 fd_data = fetch_weather()['data'].coordinates[0] fd_data = FDataGrid(np.squeeze(fd_data.data_matrix), np.arange(0.5, 365, 1)) # initialize basis data basis = Fourier(n_basis=n_basis, domain_range=(0, 365)) fd_basis = fd_data.to_basis(basis) fpca = FPCA(n_components=n_components, regularization=TikhonovRegularization( LinearDifferentialOperator(2), regularization_parameter=1e5)) fpca.fit(fd_basis) scores = fpca.transform(fd_basis) # results obtained using Ramsay's R package results = [[-7.68307641e+01, 5.69034443e+01, -1.22440149e+01], [-9.02873996e+01, 1.46262257e+01, -1.78574536e+01], [-8.21155683e+01, 3.19159491e+01, -2.56212328e+01], [-1.14163637e+02, 3.66425562e+01, -1.00810836e+01], [-6.97263223e+01, 1.22817168e+01, -2.39417618e+01], [-6.41886364e+01, -1.07261045e+01, -1.10587407e+01], [1.35824412e+02, 2.03484658e+01, -9.04815324e+00], [-1.46816399e+01, -2.66867491e+01, -1.20233465e+01], [1.02507511e+00, -2.29840736e+01, -9.06081296e+00], [-3.62936903e+01, -2.09520442e+01, -1.14799951e+01], [-4.20649313e+01, -1.13618094e+01, -6.24909009e+00], [-7.38115985e+01, -3.18423866e+01, -1.50298626e+01], [-6.69822456e+01, -3.35518632e+01, -1.25167352e+01], [-1.03534763e+02, -1.29513941e+01, -1.49103879e+01], [-1.04542036e+02, -1.36794907e+01, -1.41555965e+01], [-7.35863347e+00, -1.41171956e+01, -2.97562788e+00], [7.28804530e+00, -5.34421830e+01, -3.39823418e+00], [5.59974094e+01, -4.02154080e+01, 3.78800103e-01], [1.80778702e+02, 1.87798201e+01, -1.99043247e+01], [-3.69700617e+00, -4.19441020e+01, 6.45820740e+00], [3.76527216e+01, -4.23056953e+01, 1.04221757e+01], [1.23850646e+02, -4.24648130e+01, -2.22336786e-01], [-7.23588457e+00, -1.20579536e+01, 2.07502089e+01], [-4.96871011e+01, 8.88483448e+00, 2.02882768e+01], [-1.36726355e+02, -1.86472599e+01, 1.89076217e+01], [-1.83878661e+02, 4.12118550e+01, 1.78960356e+01], [-1.81568820e+02, 5.20817910e+01, 2.01078870e+01], [-5.08775852e+01, 1.34600555e+01, 3.18602712e+01], [-1.37633866e+02, 7.50809631e+01, 2.42320782e+01], [4.98276375e+01, 1.33401270e+00, 3.50611066e+01], [1.51149934e+02, -5.47417776e+01, 3.97592325e+01], [1.58366096e+02, -3.80762686e+01, -5.62415023e+00], [2.17139548e+02, 6.34055987e+01, -1.98853635e+01], [2.33615480e+02, -7.90787574e-02, 2.69069525e+00], [3.45371437e+02, 9.58703622e+01, 8.47570770e+00]] results = np.array(results) # compare results np.testing.assert_allclose(scores, results, atol=1e-7)
def _get_group_fd(X, group_mask, basis, grid_points=None): _check_skfda() group_X = np.copy(X[:, group_mask]) if grid_points is None: group_grid_points = np.arange(0, group_X.shape[1]) else: group_grid_points = np.copy(grid_points[group_mask]) fd = FDataGrid(group_X, group_grid_points) if basis is not None: fd = fd.to_basis(basis) return fd
def test_basis_fpca_fit_result(self): n_basis = 9 n_components = 3 fd_data = fetch_weather()['data'].coordinates[0] fd_data = FDataGrid(np.squeeze(fd_data.data_matrix), np.arange(0.5, 365, 1)) # initialize basis data basis = Fourier(n_basis=n_basis, domain_range=(0, 365)) fd_basis = fd_data.to_basis(basis) fpca = FPCA(n_components=n_components, regularization=TikhonovRegularization( LinearDifferentialOperator(2), regularization_parameter=1e5)) fpca.fit(fd_basis) # results obtained using Ramsay's R package results = [[ 0.92407552, 0.13544888, 0.35399023, 0.00805966, -0.02148108, -0.01709549, -0.00208469, -0.00297439, -0.00308224 ], [ -0.33314436, -0.05116842, 0.89443418, 0.14673902, 0.21559073, 0.02046924, 0.02203431, -0.00787185, 0.00247492 ], [ -0.14241092, 0.92131899, 0.00514715, 0.23391411, -0.19497613, 0.09800817, 0.01754439, -0.00205874, 0.01438185 ]] results = np.array(results) # compare results obtained using this library. There are slight # variations due to the fact that we are in two different packages for i in range(n_components): if np.sign(fpca.components_.coefficients[i][0]) != np.sign( results[i][0]): results[i, :] *= -1 np.testing.assert_allclose(fpca.components_.coefficients, results, atol=1e-7)
def test_basis_fpca_regularization_fit_result(self): n_basis = 9 n_components = 3 fd_data = fetch_weather()['data'].coordinates[0] fd_data = FDataGrid(np.squeeze(fd_data.data_matrix), np.arange(0.5, 365, 1)) # initialize basis data basis = Fourier(n_basis=n_basis, domain_range=(0, 365)) fd_basis = fd_data.to_basis(basis) fpca = FPCA(n_components=n_components) fpca.fit(fd_basis) # results obtained using Ramsay's R package results = [[ 0.9231551, 0.1364966, 0.3569451, 0.0092012, -0.0244525, -0.02923873, -0.003566887, -0.009654571, -0.0100063 ], [ -0.3315211, -0.0508643, 0.89218521, 0.1669182, 0.2453900, 0.03548997, 0.037938051, -0.025777507, 0.008416904 ], [ -0.1379108, 0.9125089, 0.00142045, 0.2657423, -0.2146497, 0.16833314, 0.031509179, -0.006768189, 0.047306718 ]] results = np.array(results) # compare results obtained using this library. There are slight # variations due to the fact that we are in two different packages for i in range(n_components): if np.sign(fpca.components_.coefficients[i][0]) != np.sign( results[i][0]): results[i, :] *= -1 np.testing.assert_allclose(fpca.components_.coefficients, results, atol=1e-7)