def test_ICA(self): print ('Preprocessing -> Performing ICA test ...') sys.stdout.flush() fail = 0 for _ in range(100): data, mixMat = generate_2d_mixtures(10000) zca = ZCA(data.shape[1]) zca.train(data) data_zca = zca.project(data) mixMat_zca = zca.project(mixMat.T).T mixMat_zca /= get_norms(mixMat_zca, axis=None) model = ICA(2) model.train(data_zca, 10) assert numx.all(numx.abs(data_zca - model.unproject(model.project(data_zca))) < self.epsilon) # Check the angle between mixing matrix vectors and resulting vectors of the projection matrix # Theorectially the amari distnace could be used but it seems only to be rotation and scal invariant # and not invariant through permutations of the colums. res = model.projection_matrix / get_norms(model.projection_matrix, axis=None) v1 = res v2 = res * numx.array([[-1, 1], [-1, 1]]) v3 = res * numx.array([[1, -1], [1, -1]]) v4 = -res res = numx.array([res[:, 1], res[:, 0]]).T v5 = res v6 = res * numx.array([[-1, 1], [-1, 1]]) v7 = res * numx.array([[1, -1], [1, -1]]) v8 = -res v1d = numx.max([angle_between_vectors(v1[0], mixMat_zca[0]), angle_between_vectors(v1[1], mixMat_zca[1])]) v2d = numx.max([angle_between_vectors(v2[0], mixMat_zca[0]), angle_between_vectors(v2[1], mixMat_zca[1])]) v3d = numx.max([angle_between_vectors(v3[0], mixMat_zca[0]), angle_between_vectors(v3[1], mixMat_zca[1])]) v4d = numx.max([angle_between_vectors(v4[0], mixMat_zca[0]), angle_between_vectors(v4[1], mixMat_zca[1])]) v5d = numx.max([angle_between_vectors(v5[0], mixMat_zca[0]), angle_between_vectors(v5[1], mixMat_zca[1])]) v6d = numx.max([angle_between_vectors(v6[0], mixMat_zca[0]), angle_between_vectors(v6[1], mixMat_zca[1])]) v7d = numx.max([angle_between_vectors(v7[0], mixMat_zca[0]), angle_between_vectors(v7[1], mixMat_zca[1])]) v8d = numx.max([angle_between_vectors(v8[0], mixMat_zca[0]), angle_between_vectors(v8[1], mixMat_zca[1])]) dist = numx.min([v1d, v2d, v3d, v4d, v5d, v6d, v7d, v8d]) # biggest angle smaller than 5 degrees if dist > 5.0: fail += 1 assert fail < 5 print('successfully passed!') sys.stdout.flush()
along with this program. If not, see <http://www.gnu.org/licenses/>. """ # Import numpy, numpy extensions, PCA, 2D linear mixture, and visualization module import numpy as numx from pydeep.preprocessing import PCA from pydeep.misc.toyproblems import generate_2d_mixtures import pydeep.misc.visualization as vis # Set the random seed # (optional, if stochastic processes are involved we get the same results) numx.random.seed(42) # Create 2D linear mixture, 50000 samples, mean = 0, std = 3 data, _ = generate_2d_mixtures(num_samples=50000, mean=0.0, scale=3.0) # PCA pca = PCA(data.shape[1]) pca.train(data) data_pca = pca.project(data) # Display results # For better visualization the principal components are rescaled scale_factor = 3 # Figure 1 - Data with estimated principal components vis.figure(0, figsize=[7, 7]) vis.title("Data with estimated principal components")
""" # Import numpy, numpy extensions, ZCA, ICA, 2D linear mixture, and visualization import numpy as numx import pydeep.base.numpyextension as numxext from pydeep.preprocessing import ZCA, ICA from pydeep.misc.toyproblems import generate_2d_mixtures import pydeep.misc.visualization as vis # Set the random seed # (optional, if stochastic processes are involved we get the same results) numx.random.seed(42) # Create 2D linear mixture, 50000 samples, mean = 0, std = 3 data, mixing_matrix = generate_2d_mixtures(num_samples=50000, mean=0.0, scale=3.0) # Zero Phase Component Analysis (ZCA) - Whitening in original space zca = ZCA(data.shape[1]) zca.train(data) whitened_data = zca.project(data) # Independent Component Analysis (ICA) ica = ICA(whitened_data.shape[1]) ica.train(whitened_data, iterations=100, status=False) data_ica = ica.project(whitened_data) # print the ll on the data print("Log-likelihood on all data: " +
import pydeep.base.numpyextension as numxext # Import models, trainers and estimators import pydeep.rbm.model as model import pydeep.rbm.trainer as trainer import pydeep.rbm.estimator as estimator # Import linear mixture, preprocessing, and visualization from pydeep.misc.toyproblems import generate_2d_mixtures import pydeep.preprocessing as pre import pydeep.misc.visualization as vis numx.random.seed(42) # Create a 2D mxiture data, mixing_matrix = generate_2d_mixtures(100000, 1, 1.0) # Whiten data zca = pre.ZCA(data.shape[1]) zca.train(data) whitened_data = zca.project(data) # split training test data train_data = whitened_data[0:numx.int32(whitened_data.shape[0] / 2.0), :] test_data = whitened_data[numx.int32(whitened_data.shape[0] / 2.0):whitened_data.shape[0], :] # Input output dims h1 = 2 h2 = 2 v1 = whitened_data.shape[1]