()
| 416 | |
| 417 | @staticmethod |
| 418 | def test_PCA(): |
| 419 | np.random.seed(1) |
| 420 | n = 1000 |
| 421 | p = 20 |
| 422 | s = 10 |
| 423 | group_size = 5 |
| 424 | group_num = 4 |
| 425 | support_size = np.zeros((p, 1)) |
| 426 | support_size[s - 1, 0] = 1 |
| 427 | |
| 428 | x1 = np.random.randn(n, 1) |
| 429 | x1 /= np.linalg.norm(x1) |
| 430 | X = x1.dot(np.random.randn(1, p)) + 0.01 * np.random.randn(n, p) |
| 431 | X = X - X.mean(axis=0) |
| 432 | g_index = np.arange(group_num) |
| 433 | g_index = g_index.repeat(group_size) |
| 434 | |
| 435 | # save_data(X, 'PCA') |
| 436 | X = load_data('PCA') |
| 437 | |
| 438 | # null |
| 439 | check_estimator(abess.SparsePCA()) |
| 440 | model1 = abess.SparsePCA(support_size=support_size) |
| 441 | model1.fit(X) |
| 442 | assert np.count_nonzero(model1.coef_) == s |
| 443 | |
| 444 | # ratio & transform |
| 445 | model1.ratio(X) |
| 446 | model1.transform(X) |
| 447 | model1.fit_transform(X) |
| 448 | |
| 449 | # sparse |
| 450 | model2 = abess.SparsePCA(support_size=s) |
| 451 | model2.fit(coo_matrix(X), sparse_matrix=True) |
| 452 | print("coef1: ", np.unique(np.nonzero(model1.coef_)[0])) |
| 453 | print("coef2: ", np.unique(np.nonzero(model2.coef_)[0])) |
| 454 | assert_value(model1.coef_, model2.coef_) |
| 455 | |
| 456 | model2 = abess.SparsePCA(support_size=s) |
| 457 | model2.fit(X, sparse_matrix=True) |
| 458 | assert_value(model1.coef_, model2.coef_) |
| 459 | |
| 460 | # sigma input |
| 461 | model3 = abess.SparsePCA(support_size=support_size) |
| 462 | model3.fit(Sigma=X.T.dot(X)) |
| 463 | model3.fit(Sigma=np.cov(X.T), n=n) |
| 464 | assert_fit(model1.coef_, model3.coef_) |
| 465 | |
| 466 | # KPCA |
| 467 | support_size_m = np.hstack((support_size, support_size, support_size)) |
| 468 | model4 = abess.SparsePCA(support_size=support_size_m) |
| 469 | model4.fit(X, number=3) |
| 470 | assert model4.coef_.shape[1] == 3 |
| 471 | |
| 472 | for i in range(3): |
| 473 | coef = np.nonzero(model4.coef_[:, i])[0] |
| 474 | assert len(coef) == s |
| 475 |
nothing calls this directly
no test coverage detected