示例#1
1
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)
示例#2
0
文件: demo_tga.py 项目: vyraun/tga
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)
        axes[0].set_title("raw")