def test_tga_inverse(): # Test that the projection of data can be inverted rng = np.random.RandomState(0) n, p = 50, 3 X = rng.randn(n, p) # spherical data X[:, 1] *= .00001 # make middle component relatively small X += [5, 4, 3] # make a large mean # same check that we can find the original data from the transformed # signal (since the data is almost of rank n_components) tga = TGA(n_components=2).fit(X) Y = tga.transform(X) Y_inverse = tga.inverse_transform(Y) assert_almost_equal(X, Y_inverse, decimal=3)
def test_tga_median(): # TGA on dense arrays tga = TGA(n_components=2, random_state=1, centering='median') X = iris.data X_r = tga.fit(X).transform(X) assert_equal(X_r.shape[1], 2) tga = TGA(n_components=2, random_state=1, centering='median') X_r2 = tga.fit_transform(X) assert_array_almost_equal(X_r, X_r2) tga = TGA(random_state=1, centering='median') tga.fit(X) X_r = tga.transform(X) tga = TGA(random_state=1, centering='median') X_r2 = tga.fit_transform(X) assert_array_almost_equal(X_r, X_r2)
if __name__ == "__main__": import sys import glob import matplotlib.pyplot as pl use_data = str(sys.argv[1]) M, shape = bitmap_to_mat(glob.glob(use_data + "/*.bmp")[:2000:2]) print(M.shape) tga = TGA(n_components=5, random_state=1) start_time = time.time() tga.fit(M) print("fitted, time taken {0}s".format(time.time() - start_time)) start_time = time.time() transformed = tga.transform(M) L = tga.inverse_transform(transformed) print('calculated L, time taken {0}s'.format(time.time() - start_time)) S = M - L if not os.path.exists('results_tga'): os.makedirs('results_tga') directory = "results_tga/" + use_data if not os.path.exists(directory): os.makedirs(directory) fig, axes = pl.subplots(1, 3, figsize=(10, 4)) fig.subplots_adjust(left=0, right=1, hspace=0, wspace=0.01) for i in range(min(len(M), 500)): do_plot(axes[0], M[i], shape)