示例#1
0
def align(movie_data, options, args, lrh):
  print 'pICA(scikit-learn)'
  nvoxel = movie_data.shape[0]
  nTR    = movie_data.shape[1]
  nsubjs = movie_data.shape[2]

  align_algo = args.align_algo
  nfeature   = args.nfeature
  randseed    = args.randseed
  if not os.path.exists(options['working_path']):
    os.makedirs(options['working_path'])

  # zscore the data
  bX = np.zeros((nsubjs*nTR,nvoxel))
  for m in range(nsubjs):
    for t in range(nTR):
      bX[nTR*m+t,:] = stats.zscore(movie_data[:,t,m].T ,axis=0, ddof=1)
  del movie_data
 
  np.random.seed(randseed)
  A = np.mat(np.random.random((nfeature,nfeature)))

  ica = FastICA(n_components= nfeature, max_iter=500,w_init=A,random_state=randseed)
  ica.fit(bX.T)
  R = ica.transform(bX.T)

  niter = 10  
  # initialization when first time run the algorithm
  np.savez_compressed(options['working_path']+align_algo+'_'+lrh+'_'+str(niter)+'.npz',\
                                R = R,  niter=niter)
  return niter
示例#2
0
def ica_components(l, dataset, n=5):
    # l = Learn()
    # l.get_data(name)

    ica = FastICA(n_components=n)
    ica.fit(l.X)

    title = '{0} Components distribution for ICA with {1} components'.format(
        dataset.upper(), str(n))

    bins = np.linspace(-.0001, .0001, 100)
    plt.figure()
    plt.title(title)
    plt.xlabel('value')
    plt.ylabel('frequency')
    a = []
    for count, i in enumerate(ica.components_):
        a.extend(i)
        kurt = stats.kurtosis(i)
        plt.hist(i, bins, alpha=0.5, label=str(count + 1) + ": " + str(kurt))

    plt.legend(loc='best')
    print('{0} ica components kurtosis: {1}'.format(dataset,
                                                    stats.kurtosis(a)))
    plt.savefig('{0}.png'.format(title.replace(' ', '_')), bbox_inches='tight')
示例#3
0
 def wrapper_fastica(data, random_state=None):
     """Call FastICA implementation from scikit-learn."""
     ica = FastICA(random_state=random_state)
     ica.fit(cat_trials(data).T)
     u = ica.components_.T
     m = ica.mixing_.T
     return m, u
示例#4
0
def feat_map_ica(layer_name,
                 subset_file='subset_cutoff_200_images.txt',
                 map_index=0,
                 n_plots=25):
    files = get_feature_files(layer_name, subset_file)
    maps = []
    for idx, file in enumerate(files):
        mat = np.load(file)
        map_shape = mat[:, :, map_index].shape
        maps.append(mat[:, :, map_index].flatten())
    maps = np.stack(maps, axis=0)
    ica = FastICA()
    ica.fit(maps)

    cols = int(np.ceil(np.sqrt(n_plots)))
    rows = int(np.ceil(n_plots // cols))
    fig, ax_list = plt.subplots(ncols=cols, nrows=rows)
    ax_list = ax_list.flatten()
    for idx, ax in enumerate(ax_list):
        if idx >= n_plots:
            ax.axis('off')
        else:
            ax.matshow(np.reshape(ica.components_[idx, :], map_shape),
                       interpolation='none')
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)
    plt.savefig('./plots/ica_' + layer_name + '_map_' + str(map_index) +
                '.png',
                format='png',
                dpi=1500)
    plt.close()
示例#5
0
def perform_ica(data, num_comps=5, dims={'x': 2, 'y': 1, 't': 0}):
    """Performs an ica on the timedomain
    
    Arguments:
        data {3-d array}        -- Input of xyt, can be any order as long as it is accompanied with appropriate dims argument
    
    Keyword Arguments:
        num_comps {int}         -- Number of ICs to extract (default: {5})
        dims {dict}             -- The dimensions corresponding to x, y, and t (default: {{'x':2, 'y':1, 't':0}})
    
    Returns:
        comps_out {3-d array} -- Array of num_comps x X x Y
    """
    '''Performs ica on data input that is of the for (t,y,x)'''

    ica = FastICA(n_components=num_comps)

    data_reshaped = np.transpose(data, [dims['t'], dims['y'], dims['x']])
    # Transpose to t, y, x

    sz = data_reshaped.shape
    data_re = data_reshaped.reshape([sz[0], sz[1] * sz[2]])
    # Make it zero mean and common std
    for ii in range(sz[1] * sz[2]):
        data_re[:, ii] = (data_re[:, ii] - np.mean(data_re[:, ii])) / np.std(
            data_re[:, ii])
    # Now perform ica
    print('fitting model...')
    ica.fit(data_re)
    print('done')
    comps_out = ica.components_.reshape([num_comps, sz[1], sz[2]])

    return comps_out
def ica(X, y, components, max_cluster, num_classes, run_nn=False):
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        train_size=0.7,
                                                        shuffle=True)
    ica_compress = FastICA(n_components=components, whiten=True)
    ica_compress.fit(X_train, y=y_train)
    X_train_new = ica_compress.transform(X_train)
    X_test_new = ica_compress.transform(X_test)
    print(kurtosis(X_test_new))
    print(ica_compress.components_)
    if run_nn:
        mlp_classifier(X_train_new,
                       y_train,
                       0.3,
                       plot=True,
                       X_test=X_test_new,
                       y_test=y_test)
    X_new = np.concatenate((X_train_new, X_test_new), axis=0)
    y = np.concatenate((y_train, y_test), axis=0)
    kmeans(X_new,
           y,
           max_cluster,
           num_classes,
           run_nn=run_nn,
           plot_cluster=True,
           reduction_algo='ICA')
    expectation_max(X_new,
                    y,
                    max_cluster,
                    num_classes,
                    run_nn=run_nn,
                    plot_cluster=True,
                    reduction_algo='ICA')
示例#7
0
class IcaProcessing(Processing):
    """
    IcaProcessing is the procesisng used when performing Independant Component Analysis on Side-Channel traces.
    (based on sklearn.decomposition.ICA)
    """
    def __init__(self,
                 container,
                 number_of_components,
                 random_state=0,
                 post_section=None,
                 filename=None):
        """
        :param container: the container on which to perform ICA
        :param number_of_components: number of component used for the dimensionality reduction
        :param random_state: optional, for the sklearn object
        """
        self._ica = FastICA(n_components=number_of_components,
                            random_state=random_state)

        # compute ica:
        batch = container[:container.number_of_traces]
        self._ica.fit(batch.leakages)

        self.post_section = post_section
        Processing.__init__(self, filename)

    def __call__(self, leakage):
        if self.post_section is None:
            return self._ica.transform([leakage])[0]
        else:
            return self._ica.transform([leakage])[0, self.post_section]
示例#8
0
def myICA(X,y):
    t1 = clock()
    clf =  FastICA()
    clf.fit(X)
    newRep = clf.transform(X)
    t2 = clock()
    return t2-t1
示例#9
0
def fast_ica(image, components):
    """Reconstruct an image from Fast ICA compression using specific number of components to use

    Args:
        image: PIL Image, Numpy array or path of 3D image
        components: Number of components used for reconstruction

    Returns:
        Reconstructed image

    Example:

    >>> from PIL import Image
    >>> import numpy as np
    >>> from ipfml.processing import reconstruction
    >>> image_values = Image.open('./images/test_img.png')
    >>> reconstructed_image = reconstruction.fast_ica(image_values, 25)
    >>> reconstructed_image.shape
    (200, 200)
    """

    lab_img = transform.get_LAB_L(image)
    lab_img = np.array(lab_img, 'uint8')

    ica = FastICA(n_components=50)
    # run ICA on image
    ica.fit(lab_img)
    # reconstruct image with independent components
    image_ica = ica.fit_transform(lab_img)
    restored_image = ica.inverse_transform(image_ica)

    return restored_image
def main(mode):
    path = "/local/attale00/extracted_pascal__4__Multi-PIE"
    path_ea = path + "/color128/"

    allLabelFiles = utils.getAllFiles("/local/attale00/a_labels")

    labeledImages = [i[0:16] + ".png" for i in allLabelFiles]

    # labs=utils.parseLabelFiles(path+'/Multi-PIE/labels','mouth',labeledImages,cutoffSeq='.png',suffix='_face0.labels')
    labs = utils.parseLabelFiles(
        "/local/attale00/a_labels", "mouth", labeledImages, cutoffSeq=".png", suffix="_face0.labels"
    )

    testSet = fg.dataContainer(labs)
    roi = (50, 74, 96, 160)
    X = fg.getAllImagesFlat(path_ea, testSet.fileNames, (128, 256), roi=roi)

    # perform ICA
    if mode not in ["s", "v"]:
        ica = FastICA(n_components=100, whiten=True)
        ica.fit(X)
        meanI = np.mean(X, axis=0)
        X1 = X - meanI
        data = ica.transform(X1)
        filters = ica.components_

    elif mode in ["s", "v"]:
        W = np.load("/home/attale00/Desktop/classifiers/ica/filter1.npy")
        m = np.load("/home/attale00/Desktop/classifiers/ica/meanI1.npy")
        X1 = X - m
        data = np.dot(X1, W.T)

    for i in range(len(testSet.data)):
        testSet.data[i].extend(data[i, :])

    strel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))

    # fg.getHogFeature(testSet,roi,path=path_ea,ending='.png',extraMask = None,orientations = 3, cells_per_block=(6,2),maskFromAlpha=False)
    # fg.getColorHistogram(testSet,roi,path=path_ea,ending='.png',colorspace='lab',bins=10)
    testSet.targetNum = map(utils.mapMouthLabels2Two, testSet.target)

    rf = classifierUtils.standardRF(max_features=np.sqrt(len(testSet.data[0])), min_split=5, max_depth=40)
    if mode in ["s", "v"]:
        print "Classifying with loaded classifier"
        classifierUtils.classifyWithOld(
            path, testSet, mode, clfPath="/home/attale00/Desktop/classifiers/ica/rf128ICA_1"
        )
    elif mode in ["c"]:
        print "cross validation of data"
        print "Scores"
        # print classifierUtils.standardCrossvalidation(rf,testSet,n_jobs=5)
        # _cvDissect(testSet,rf)
        classifierUtils.dissectedCV(rf, testSet)
        print "----"

    elif mode in ["save"]:
        print "saving new classifier"
        _saveRF(testSet)
    else:
        print "not doing anything"
def PerformIca(X,Y,num_components,random_state):
    result = {}
    algo = FastICA(random_state=random_state,max_iter=800)
    algo.fit(X)
    full_mixing_matrix = algo.mixing_
    full_unmixing_matrix = algo.components_
    _x = algo.transform(X)
    kt_value = np.abs(kt(_x))
    largest_kt_values_idx = np.argsort(kt_value)[::-1]
    result["ica_kt_all"] = kt_value

    for n in num_components:
        prefix = "ica_" + str(n) + "_"
        component_idx_to_select = largest_kt_values_idx[0:n]
        mixing_matrix = full_mixing_matrix.T[component_idx_to_select,:].T
        unmixing_matrix = full_unmixing_matrix[component_idx_to_select,:]
        algo.components_ = unmixing_matrix
        algo.mixing_ = mixing_matrix

        result[prefix+"mm"] = mixing_matrix
        result[prefix+"umm"] = unmixing_matrix

        _x = algo.transform(X)
        result[prefix+"data"] = _x
        X_recons = algo.inverse_transform(_x)
        result[prefix+"reconstruction_error"] = ComputeReconstructionSSE(X,X_recons)
        n_kt_value = kt_value[component_idx_to_select]
        avg_kt = n_kt_value.mean()
        #print("ICA num dim {0} : reconstruction error {1} avg kt {2}".format(str(n),str(result[prefix+"reconstruction_error"]),str(avg_kt)))
        #print(np.sort(n_kt_value))
    return result
示例#12
0
def fast_ica_from_sklearn():
    df = pd.read_csv()

    data = df.drop()

    # 独立成分の数=2
    decomposer = FastICA(n_components=2)

    # データの平均を計算
    M = np.mean(data, axis=1)[:, np.newaxis]

    # 各データから平均を引く
    data2 = data - M

    # 平均0としたデータに対して、独立成分分析を実施
    decomposer.fit(data2)

    # 独立成分ベクトルを取得(D次元 x 独立成分数)
    S = decomposer.transform(data2)

    # 混合行列の計算(データ数 x 独立性分数)
    W = decomposer.mixing_

    # 混合行列と独立成分から元の信号dataを復元
    X = np.dot(S, W.T)
    X += M

    # 混合行列の擬似逆行列を取得
    W_inv = decomposer.components_

    plt.plot(W_inv)
示例#13
0
def test_fastica_convergence_fail():
    # Test the FastICA algorithm on very simple data
    # (see test_non_square_fastica).
    # Ensure a ConvergenceWarning raised if the tolerance is sufficiently low.
    rng = np.random.RandomState(0)

    n_samples = 1000
    # Generate two sources:
    t = np.linspace(0, 100, n_samples)
    s1 = np.sin(t)
    s2 = np.ceil(np.sin(np.pi * t))
    s = np.c_[s1, s2].T
    center_and_norm(s)

    # Mixing matrix
    mixing = rng.randn(6, 2)
    m = np.dot(mixing, s)

    # Do fastICA with tolerance 0. to ensure failing convergence
    warn_msg = ("FastICA did not converge. Consider increasing tolerance "
                "or the maximum number of iterations.")
    with pytest.warns(ConvergenceWarning, match=warn_msg):
        ica = FastICA(algorithm="parallel",
                      n_components=2,
                      random_state=rng,
                      max_iter=2,
                      tol=0.0)
        ica.fit(m.T)
def myICA(bigdf, ncomp, whiten=True):
    ica = FastICA(n_components=ncomp, whiten=True)
    ica.fit(bigdf)
    icafittrans = ica.transform(bigdf)
    icafittrans = pd.DataFrame(icafittrans)
    icafittrans.index = dfDateIndex

    icacomponents = ica.components_
    icacomponents = pd.DataFrame(icacomponents)
    icacomponents.columns = dfSearchTermIndex

    # ICA results are arbitrarily scaled.
    # For easy human comprehension we'll rectify the peaks
    peakdirs = icafittrans.apply(peakDirection, axis=0)
    icafittrans = icafittrans.multiply(peakdirs, axis='columns')
    # That's enough to make it look right, but we also need to rectify components
    icacomponents = icacomponents.multiply(peakdirs, axis='rows')

    # While we are at it, let's reorder the components from most peaky to least
    peakinesses = icafittrans.apply(peakiness, axis=0)
    peakinessSort = np.argsort(-peakinesses)
    # now sort the columns in the icafittrans and the A_
    icafittrans = icafittrans.iloc[:, peakinessSort]
    icacomponents = icacomponents.iloc[peakinessSort, :]

    return icafittrans, icacomponents
示例#15
0
def calculate_encoder(image,
                      algorithm,
                      patches_size=(DEFAULT_PATCHE_SIZE, DEFAULT_PATCHE_SIZE),
                      projection_dimensios=DEFAULT_PATCHE_SIZE,
                      previous_components=None):
    """
    Gets a higher dimension sparse dictionary.algorithm can be
    ica, kmeans or colors.

    """
    patches = extract_patches_2d(
        image, patches_size,
        calculate_max_number_of_patches(image, patches_size))
    patches = numpy.reshape(patches, (patches.shape[0], -1)).astype(float)
    encoder = None
    if algorithm == 'ica':
        encoder = FastICA(n_components=projection_dimensios,
                          whiten=True,
                          max_iter=10)
    elif algorithm == 'kmeans':
        encoder = MiniBatchKMeans(projection_dimensios, compute_labels=False)
    else:
        raise Exception('Unknow algorithm %s' % algorithm)
    encoder.fit(patches)
    return encoder
def get_best_dimensionality_reductions(x1, x2, best_features):
    dim_reds = {}
    for d, x in {'wine': x1, 'pima': x2}.items():
        pca = PCA(n_components=0.95, whiten=True, random_state=42)
        pca.fit(x)

        k = dim_reds.setdefault('pca', {})
        k[d] = pca

        k = dim_reds.setdefault('rfc', {})
        k[d] = best_features[d]

    k = dim_reds.setdefault('ica', {})
    ica = FastICA(n_components=8, whiten=True, random_state=42)
    ica.fit(x1)
    k['wine'] = ica
    ica = FastICA(n_components=6, whiten=True, random_state=42)
    ica.fit(x2)
    k['pima'] = ica

    k = dim_reds.setdefault('rp', {})
    rp = SparseRandomProjection(random_state=42, n_components=8)
    rp.fit(x1)
    k['wine'] = rp

    rp = SparseRandomProjection(random_state=42, n_components=6)
    rp.fit(x2)
    k['pima'] = rp

    return dim_reds
def extractPatterns(actmat,significance,method):
        nassemblies = significance.nassemblies
    
        if method == 'pca':
                idxs = np.argsort(-significance.explained_variance_)[0:nassemblies]
                patterns = significance.components_[idxs,:]
        elif method == 'ica':
                from sklearn.decomposition import FastICA
                ica = FastICA(n_components=nassemblies)
                ica.fit(actmat.T)
                patterns = ica.components_
        else:
                print('ERROR !')
                print('    assembly extraction method '+str(method)+' not understood')
                patterns = np.nan
                
        
                
        if patterns is not np.nan:
            
                patterns = patterns.reshape(nassemblies,-1)
                
                # sets norm of assembly vectors to 1
                norms = np.linalg.norm(patterns,axis=1)
                patterns /= np.matlib.repmat(norms,np.size(patterns,1),1).T
        
        return patterns
示例#18
0
def get_ICA_features(X_tr, X_te, n_components=None):
	ica = FastICA(n_components=n_components)
	ica.fit(X_tr)
	t_x_tr = ica.transform(X_tr)
	t_x_te = ica.transform(X_te)

	return t_x_tr, t_x_te
示例#19
0
    def fit(self, data):
        max_sir = None
        self.ica = None
        for _ in range(self.n_iter):
            fca = FastICA(n_components=self.n_components,
                          max_iter=self.max_iter,
                          algorithm='deflation')
            fca.fit(data)
#            try:

            new_components = sorted(zip(fca.components_, np.argmax(np.abs(fca.components_), axis=1)), key= lambda x: x[1])
            new_components = np.stack(new_components)
            new_components = np.array([x[0] for x in new_components])
            fca.components_ = new_components

            m = new_components/(abs(norm(new_components, axis=1)).reshape(-1,1)) - np.identity(data.shape[1])
            sir = norm(m, axis=1)
#             except:
#                continue
            sir = -10*np.log10(sir).sum()
            # in the paper its >
            if max_sir is None or sir > max_sir:
                self.max_sir = sir
                max_sir = sir
                self.sir_log.append(sir)
                self.ica = fca
        return self.ica
示例#20
0
def red_dim(metodo, n):
	trainX = 	np.load('trainX.npy')
	testX = 	np.load('testX.npy')
	if metodo == "RFE":
		clf = DecisionTreeClassifier()
		rfe = RFECV(clf, step=1, cv=10, verbose=2)
		trainy = np.load('trainy.npy')
		rfe = rfe.fit(trainX, trainy)
		l = zip(rfe.ranking_,dnames)
		for i in sorted(l, key=lambda x: x[0]):
			print i[1]
	else:
		if metodo == "PCA":
			pca = PCA(n_components=n)
			pca.fit(trainX)
			trainX = pca.transform(trainX)
			testX = pca.transform(testX)
		elif metodo == "iPCA":
			pca = IncrementalPCA(n_components=n)
			pca.fit(trainX)
			trainX = pca.transform(trainX)
			testX = pca.transform(testX)
		elif metodo == "ICA":
			ica = FastICA(n_components=n)
			ica.fit(trainX)
			trainX = ica.transform(trainX)
			testX = ica.transform(testX)
		else:
			print u'Dimensión inválida'
			exit()
		np.save('trainX_' + metodo + str(n) , trainX)
		np.save('testX_' + metodo + str(n), testX)
示例#21
0
 def wrapper_fastica(data, random_state=None):
     """Call FastICA implementation from scikit-learn."""
     ica = FastICA(random_state=random_state)
     ica.fit(cat_trials(data).T)
     u = ica.components_.T
     m = ica.mixing_.T
     return m, u
def ica(n_components, X, y, algorithm='parallel', whiten=True, fun='logcosh', fun_args=None, max_iter=2000, tol=0.000001, w_init=None, random_state=3, plot=1, dataset='german'):
    ica_model = FastICA(n_components=n_components, algorithm=algorithm, whiten=whiten, fun=fun, fun_args=fun_args, max_iter=max_iter, tol=tol, w_init=w_init, random_state=random_state)
    ica_model.fit(X)
    X_new = ica_model.transform(X)
    if plot:
        if dataset == 'german':
            plt.scatter(X_new[y == 1, 0], X_new[y == 1, 1], c='red', label='Samples with label 1')
            plt.scatter(X_new[y == 0, 0], X_new[y == 0, 1], c='green', label='Samples with label 0')
            plt.title("German dataset after ICA")
            plt.legend()
            plt.xlabel("Component 1")
            plt.ylabel("Component 2")
            plt.savefig("german-after-ICA.png")
            plt.close()

        elif dataset == 'australian':
            plt.scatter(X_new[y == 1, 0], X_new[y == 1, 1], c='red', label='Samples with label 1')
            plt.scatter(X_new[y == 0, 0], X_new[y == 0, 1], c='green', label='Samples with label 0')
            plt.title("Australian dataset after ICA")
            plt.legend()
            plt.xlabel("Component 1")
            plt.ylabel("Component 2")
            plt.savefig("australian-after-ICA.png")
            plt.close()
    return X_new
def plot_component_kurtosis(datasets, targets):
    """Plot component kurtosis."""

    plt.figure(figsize=(8, 4))

    subindex = 0
    for dataset, target in zip(datasets, targets):
        subindex += 1

        logging.info(f"Visualizing ICA kurtosis for {dataset}...")
        data = helpers.load_dataset_df(dataset)

        train, test = helpers.split_test_train(data, target)
        train, test = helpers.scale_test_train(train, test)

        dim = DimRedux(train.X.shape[1], max_iter=5000, whiten=True, random_state=42)
        dim.fit(train.X)

        kurt = scipy.stats.kurtosis(dim.transform(train.X))

        plt.subplot(1, len(datasets), subindex)
        plt.bar([n for n, c in enumerate(kurt)], kurt, align="center", alpha=0.5)
        plt.xlabel("Components")
        plt.ylabel("Kurtosis")
        plt.title(f"{dataset}", fontsize=10)

        plt.tight_layout()

    outpath = os.path.join(helpers.BASEDIR, "img", f"dim-ica-both-kurtosis.png")
    plt.savefig(outpath)

    return None
示例#24
0
文件: ica.py 项目: kuntzer/sclas
class ICA(method.Method):
	
	def __init__(self, params):
		self.params = params
		self.ica = FastICA(**params)
	
	def __str__(self):
		return "FastICA"
		
	def train(self, data):
		"""
		Train the FastICA on the withened data
		
		:param data: whitened data, ready to use
		"""
		self.ica.fit(data)
	
	def encode(self, data):
		"""
		Encodes the ready to use data
		
		:returns: encoded data with dimension n_components
		"""
		return self.ica.transform(data)
	
	def decode(self, components):
		"""
		Decode the data to return whitened reconstructed data
		
		:returns: reconstructed data
		"""
		return self.ica.inverse_transform(components)
示例#25
0
def iCA(x_all, y_all):
    # 独立成分の数=24
    decomposer = FastICA(n_components=2)
    # データの平均を計算
    M = np.mean(x_all, axis=1)[:, np.newaxis]
    # 各データから平均を引く
    data2 = x_all - M
    # 平均0としたデータに対して、独立成分分析を実施
    decomposer.fit(data2)

    # 独立成分ベクトルを取得(D次元 x 独立成分数)
    S = decomposer.transform(data2)
    #プロットする
    for label in np.unique(y_all):
        plt.scatter(S[y_all == label, 0],
                    S[y_all == label, 1], )
    plt.legend(loc='upper right',
               bbox_to_anchor=(1,1),
               borderaxespad=0.5,fontsize = 10)
    plt.title('principal component')
    plt.xlabel('Ic1')
    plt.ylabel('Ic2')

    # 主成分の寄与率を出力する
    #print('各次元の寄与率: {0}'.format(decomposer.explained_variance_ratio_))
    #print('累積寄与率: {0}'.format(sum(decomposer.explained_variance_ratio_)))

    # グラフを表示する
    plt.show()


    return S, y_all
def ica_varing_components(data, type):
    # Run ICA demensionality reduction on original data
    n_components_max = 56
    n_components = np.arange(2, n_components_max, 1)
    kurtosis_matrix = np.array([])
    for n in n_components:
        print n
        ica = FastICA(n_components=n,whiten=True, algorithm='deflation', max_iter=100)
        ica.fit(data)
        transformed_data = ica.transform(data)
        kurtosis = sta.kurtosis(transformed_data, axis=0) # for Normal distribution, kurtosis = 0 by this algorithm
        # shape = n_components when axis = 0, meaning kurtosis is calculated for each column
        kurtosis_matrix = np.append(kurtosis_matrix, np.average(kurtosis))
    
    # Plot n_components vs. kurtosis
    plt.figure(figsize=(16, 9))
    plt.plot(n_components, kurtosis_matrix, "o-", label="kurtosis")
    plt.grid(True)
    plt.xlim(np.amin(n_components), np.amax(n_components))
    plt.xticks(range(np.amin(n_components), n_components_max+3, 3))
    plt.ylabel('kurtosis')
    plt.xlabel('# of components')
    plt.legend()
    plt.title(("NBA Player Stats %s ICA\n(Kurtosis of Normal Distribution is 0 in this algorithm)") % (type))
    plt.savefig(("original_ICA_kurtosis_2_to_%d_components_axis_1.png") % n_components_max)
    plt.close()
示例#27
0
def _ICA_decomposition(X, n_components, max_iter):
    """ Apply FastICA algorithm from sklearn.decompostion to the matrix X
        
        Note: FastICA in sklearn works with a matrix of shape (n_features , n_samples)
              that is why we fit FastICA with X.T
              
    Parameters
    ----------
    X : 2D array, shape (n_samples , n_features)

    n_components : int
        number of ICA components
        
    max_iter : int
        see https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.FastICA.html

    Returns
    -------
    2D array , shape (n_components , n_features)
        components obtained from the ICA decomposition of X

    """
    ica = FastICA(n_components=n_components, max_iter=max_iter)
    ica.fit(X.T)
    return ica.transform(X.T).T
示例#28
0
def img2_coord(img, init=None):

    assert np.max(img) <= 1.0

    if init is None:
        init = np.zeros((img.shape[0] + 200, img.shape[1] + 200))

    init[100:-100, 100:-100] = img

    img = init

    img_size = img.shape[0]

    tile_x = np.tile(np.arange(img_size), (img_size, 1))
    tile_y = tile_x.T

    mean_x = np.sum(img * tile_x) / np.sum(img)
    mean_y = np.sum(img * tile_y) / np.sum(img)

    dist_mean_x = np.abs(mean_x - tile_x) * img
    dist_mean_y = np.abs(mean_y - tile_y) * img

    hypo = np.max(((dist_mean_x * dist_mean_x) + (dist_mean_y * dist_mean_y)))

    diff_mean_x = tile_x[img > 0].flatten() - mean_x
    diff_mean_y = tile_y[img > 0].flatten() - mean_y

    m = np.stack([diff_mean_x, diff_mean_y])

    decomposer = FastICA(2)
    decomposer.fit(m.T)
    Uica = decomposer.mixing_

    # print('ICA vectors')
    norms = np.sqrt((Uica**2).sum(axis=0))
    Uica = Uica / np.sqrt((Uica**2).sum(axis=0))
    if norms[0] > norms[1]:
        rotate = -np.arctan2(Uica[0, 0], Uica[1, 0])
    else:
        rotate = -np.arctan2(Uica[0, 1], Uica[1, 1])

    # represent between [-math.pi, math.pi]
    if rotate < -math.pi / 2:
        rotate += math.pi
    elif rotate > math.pi / 2:
        rotate -= math.pi

    # print('rotate: {:.2f} [deg]'.format(rotate * 360 / 2 / 3.14))

    aspect_ratio = max(norms) / min(norms)
    # print('aspect ratio: {:.2f}'.format(aspect_ratio))

    width = np.sqrt(hypo / (1 + aspect_ratio**2)) * 2 + 0.25
    # height = np.sqrt(hypo * aspect_ratio**2 / (1 + aspect_ratio**2)) * 2
    height = width * aspect_ratio

    # print('width: {} height: {}'.format(width, height))

    return mean_x, mean_y, height, aspect_ratio, rotate, img_size
示例#29
0
def wrapper_fastica(data):
    """ Call FastICA implementation from scikit-learn.
    """
    ica = FastICA()
    ica.fit(datatools.cat_trials(data))
    u = ica.components_.T
    m = ica.mixing_.T
    return m, u
示例#30
0
def ica_algorithm(dataSet, d=0.95):
    print('Doing FastICA......')
    print('Origin data size:' + str(dataSet.shape))
    ica = FastICA(n_components=d)
    ica.fit(dataSet)
    new_data = ica.transform(dataSet)
    print('After ICA data size:' + str(new_data.shape))
    return new_data
示例#31
0
def ica(tx, ty, rx, ry):
    compressor = ICA(whiten=True)  # for some people, whiten needs to be off
    compressor.fit(tx, y=ty)
    newtx = compressor.transform(tx)
    newrx = compressor.transform(rx)
    em(newtx, ty, newrx, ry, add="wICAtr", times=10)
    km(newtx, ty, newrx, ry, add="wICAtr", times=10)
    nn(newtx, ty, newrx, ry, add="wICAtr")
def independent_components_decomposition(W, n_components):
    fast_ica = FastICA(n_components=n_components)
    fast_ica.fit(W)
    W_ = fast_ica.components_
    norm = np.linalg.norm(W_, axis=1).reshape(-1, n_components)
    W_nomralize = W_ / norm.T
    independent_components = torch.from_numpy(W_nomralize.T).float()
    return independent_components
示例#33
0
文件: analysis2.py 项目: jj4192/MLp3
def ica(tx, ty, rx, ry):
    compressor = ICA(whiten=False)  # for some people, whiten needs to be off
    compressor.fit(tx, y=ty)
    newtx = compressor.transform(tx)
    newrx = compressor.transform(rx)
    em(newtx, ty, newrx, ry, add="wICAtr", times=10)
    km(newtx, ty, newrx, ry, add="wICAtr", times=10)
    nn(newtx, ty, newrx, ry, add="wICAtr")
示例#34
0
def fastica(eeg_data):
    """
    Sample function to apply `FastICA`_ to the EEG data.

    Parameters
    ----------
    eeg_data : array
        EEG data in a CxTxE array. With C the number of channels, T the number
        of time samples and E the number of events.

    Returns
    -------
    ica : ICA object
        Trained `FastICA`_ object.
    ica_data : array
        EEG projected data in a CxTxE array. With C the number of components, T
        the number of time samples and E the number of events.
    """

    # Dimension shapes
    ch_len = eeg_data.shape[ch_dim]
    t_len = eeg_data.shape[t_dim]
    ev_len = eeg_data.shape[ev_dim]

    # -------------------------------------------------------------------------
    # 1. Fit the FastICA model

    # We need to collapse time and events dimensions
    coll_data = eeg_data.transpose([t_dim, ev_dim, ch_dim])\
        .reshape([t_len*ev_len, ch_len])

    # Fit model
    ica = FastICA()
    ica.fit(coll_data)

    # Normalize ICs to unit norm
    k = np.linalg.norm(ica.mixing_, axis=0)  # Frobenius norm
    ica.mixing_ /= k
    ica.components_[:] = (ica.components_.T * k).T

    # -------------------------------------------------------------------------
    # 2. Transform data

    # Project data
    bss_data = ica.transform(coll_data)

    # Adjust shape and dimensions back to "eeg_data" shape
    ic_len = bss_data.shape[1]
    bss_data = np.reshape(bss_data, [ev_len, t_len, ic_len])
    new_order = [0, 0, 0]
    # TODO: Check the following order
    new_order[ev_dim] = 0
    new_order[ch_dim] = 2
    new_order[t_dim] = 1
    bss_data = bss_data.transpose(new_order)

    # End
    return ica, bss_data
示例#35
0
 def ica(self):
     '''
     Perform ICA on the data
         source_matrix -- rows are sources, columns are time points, values are ?
         mixing_matrix -- rows are electrodes, columns are source, values are contributions of the electrode to the source
     '''
     ica = FastICA(self.number_of_sources)
     ica.fit(self.data)
     self.mixing_matrix = ica.mixing_  # estimated mixing matrix
示例#36
0
def test_fastica_nowhiten():
    m = [[0, 1], [1, 0]]

    # test for issue #697
    ica = FastICA(n_components=1, whiten=False, random_state=0)
    warn_msg = "Ignoring n_components with whiten=False."
    with pytest.warns(UserWarning, match=warn_msg):
        ica.fit(m)
    assert hasattr(ica, "mixing_")
示例#37
0
def wrapper_fastica(data):
    """ Call FastICA implementation from scikit-learn.
    """
    from sklearn.decomposition import FastICA
    ica = FastICA()
    ica.fit(datatools.cat_trials(data))
    u = ica.components_.T
    m = ica.mixing_.T
    return m, u
def independent_component(x, y):
    clf = FastICA(random_state=1)
    clf.fit(x.reshape(-1, 1), y)
    comp = clf.components_[0][0]
    mm = clf.get_mixing_matrix()[0][0]
    sources = clf.sources_.flatten()
    src_max = max(sources)
    src_min = min(sources)
    return [comp, mm, src_max, src_min]
示例#39
0
def ICA(model_data, components = None, transform_data = None):
    t0 = time()
    ica = FastICA(n_components=components)
    if transform_data == None:
        projection = ica.fit_transform(model_data)
    else:
        ica.fit(model_data)
        projection = ica.transform(transform_data)
    print "ICA Time: %0.3f" % (time() - t0)
    return projection
示例#40
0
def var_test_ica(flux_arr_orig, exposure_list, wavelengths, low_n=3, hi_n=100, n_step=1, show_plots=False,
                    show_summary_plot=False, save_summary_plot=True, test_ind=7, real_time_progress=False,
                    idstr=None):
    start_ind = np.min(np.nonzero(flux_arr_orig[test_ind]))
    end_ind = np.max(np.nonzero(flux_arr_orig[test_ind]))

    perf_table = Table(names=["n", "avg_diff2", "max_diff_scaled"], dtype=["i4", "f4", "f4"])
    if hi_n > flux_arr_orig.shape[0]-1:
        hi_n = flux_arr_orig.shape[0]-1

    for n in range(low_n, hi_n, n_step):
        ica = FastICA(n_components = n, whiten=True, max_iter=750, random_state=1234975)
        test_arr = flux_arr_orig[test_ind].copy()

        flux_arr = np.vstack([flux_arr_orig[:test_ind], flux_arr_orig[test_ind+1:]])
        ica_flux_arr = flux_arr.copy()  #keep back one for testing
        ica.fit(ica_flux_arr)

        ica_trans = ica.transform(test_arr.copy(), copy=True)
        ica_rev = ica.inverse_transform(ica_trans.copy(), copy=True)

        avg_diff2 = np.ma.sum(np.ma.power(test_arr-ica_rev[0],2)) / (end_ind-start_ind)
        max_diff_scaled = np.ma.max(np.ma.abs(test_arr-ica_rev[0])) / (end_ind-start_ind)
        perf_table.add_row([n, avg_diff2, max_diff_scaled])

        if real_time_progress:
            print "n: {:4d}, avg (diff^2): {:0.5f}, scaled (max diff): {:0.5f}".format(n, avg_diff2, max_diff_scaled)

        if show_plots:
            plt.plot(wavelengths, test_arr)
            plt.plot(wavelengths, ica_rev[0])
            plt.plot(wavelengths, test_arr-ica_rev[0])

            plt.legend(['orig', 'ica', 'orig-ica'])
            plt.xlim((wavelengths[start_ind], wavelengths[end_ind]))

            plt.title("n={}, avg (diff^2)={}".format(n, avg_diff2))
            plt.tight_layout()
            plt.show()
            plt.close()

    if show_summary_plot or save_summary_plot:
        plt.plot(perf_table['n'], perf_table['avg_diff2'])
        plt.plot(perf_table['n'], perf_table['max_diff_scaled'])
        plt.title("performance")
        plt.tight_layout()
        if show_summary_plot:
            plt.show()
        if save_summary_plot:
            if idstr is None:
                idstr = random.randint(1000000, 9999999)
            plt.savefig("ica_performance_{}.png".format(idstr))
        plt.close()

    return perf_table
def independent_component( (x, y) ):
    lenX = len(x)
    newX = np.array(x).reshape(lenX, 1)
    g = FastICA()
    g.fit(newX, y)
    ret = [0.0, 0.0, 0.0, 0.0]
    ret[0] = g.components_[0][0]
    ret[1] = g.get_mixing_matrix()[0][0]
    sources = g.sources_.flatten()
    ret[2] = max(sources)
    ret[3] = min(sources)
    return ret
示例#42
0
def test_fastica_nowhiten():
    m = [[0, 1], [1, 0]]
    ica = FastICA(whiten=False, random_state=0)
    ica.fit(m)
    ica.mixing_

    # test for issue #697
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        ica = FastICA(n_components=1, whiten=False, random_state=0)
        ica.fit(m)  # should raise warning
        assert_true(len(w) == 1)  # 1 warning should be raised
示例#43
0
def learnICADict(features, components=25):
	

	icaHOG = FastICA(n_components=components)
	icaHOF = FastICA(n_components=components)

	icaHOG.fit(np.array([x['hog'] for x in features]).T)
	icaHOF.fit(np.array([x['hof'] for x in features]).T)

	hogComponents = icaHOG.components_.T
	hofComponents = icaHOF.components_.T

	return hogComponents, hofComponents
def main(mode):
    path = '/local/attale00/AFLW_ALL/'
    path_ea = '/local/attale00/AFLW_cropped/mouth_img_error/'
#    
    fileNames = utils.getAllFiles(path_ea);

    
    labs=utils.parseLabelFiles(path+'/labels/labels','mouth_opening',fileNames,cutoffSeq='.png',suffix='_face0.labels')
    
    testSet = fg.dataContainer(labs)
    components = 150
    roi=None
    X=fg.getAllImagesFlat(path_ea,testSet.fileNames,(40,120),roi=roi)
#    X=fg.getAllImagesFlat(path_ea,testSet.fileNames,(120,40),roi=roi,resizeFactor = .5)
# 
# perform ICA
    if mode not in ['s','v']:
        ica = FastICA(n_components=components,whiten=True)
        ica.fit(X)
        meanI=np.mean(X,axis=0)
        X1=X-meanI
        data=ica.transform(X1)
        filters=ica.components_
        
    elif mode in ['s','v']:
        W=np.load('/home/attale00/Desktop/classifiers/patches/filterMP1.npy')
        m=np.load('/home/attale00/Desktop/classifiers/patches/meanIMP1.npy')
        X1=X-m
        data=np.dot(X1,W.T)    
    
    for i in range(len(fileNames)):
            testSet.data[i].extend(data[i,:])
            
    print 'feature vector length: {}'.format(len(testSet.data[0]))

    testSet.targetNum=map(utils.mapMouthLabels2Two,testSet.target)
    rf=classifierUtils.standardRF(max_features = np.sqrt(len(testSet.data[0])),min_split=13,max_depth=40)
    #rf = svm.NuSVC()
    #rf = linear_model.SGDClassifier(loss='perceptron', eta0=1, learning_rate='constant', penalty=None)    
    if mode in ['s','v']:
        print 'Classifying with loaded classifier'
        _classifyWithOld(path,testSet,mode)
    elif mode in ['c']:
        print 'cross validation of data'
        rValues = classifierUtils.dissectedCV(rf,testSet)
        pickle.dump(rValues,open('errorpatch_ica','w'))
    elif mode in ['save']:
        print 'saving new classifier'
        _saveRF(testSet,rf,filters=filters,meanI=meanI)
    else:
        print 'not doing anything'
示例#45
0
    def fit(self, x, y, i=0):
        # if gaussian processes are being used, data dimensionality needs to be reduced before fitting
        if self.method[i] == 'GP':
            if self.reduce_dim == 'FastICA':
                print('Reducing dimensionality with ICA')
                do_ica = FastICA(n_components=self.n_components)
                self.do_reduce_dim = do_ica.fit(x)
            if self.reduce_dim == 'PCA':
                print('Reducing dimensionality with PCA')
                do_pca = PCA(n_components=self.n_components)
                self.do_reduce_dim = do_pca.fit(x)

            x = self.do_reduce_dim.transform(x)
        #try:
            print('Training model...')
        try:
            self.model.fit(x, y)
            self.goodfit = True
            print(self.model)
        except:
            self.goodfit = False
            if self.method[i] == 'GP':
                print('Model failed to train! (For GP this does not always indicate a problem, especially for low numbers of components.)')
                pass
            else:
                print('Model failed to train!')
                traceback.print_stack()

        if self.ransac:
            self.outliers = np.logical_not(self.model.inlier_mask_)
            print(str(np.sum(self.outliers)) + ' outliers removed with RANSAC')
示例#46
0
文件: ica.py 项目: rcurtin/benchmarks
    def RunICAScikit():
      totalTimer = Timer()

      # Load input dataset.
      data = np.genfromtxt(self.dataset, delimiter=',')

      opts = {}
      if "num_components" in options:
        opts["n_components"] = int(options.pop("num_components"))

      if "algorithm" in options:
        opts["algorithm"] = str(options.pop("algorithm"))
        if opts["algorithm"] not in ['parallel', 'deflation']:
          Log.Fatal("Invalid value for algorithm: "+ str(algorithm.group(1))+" .Must be either parallel or deflation")
          return -1

      if "function" in options:
        opts["fun"] = str(options.pop("function"))
        if opts["fun"] not in ['logcosh', 'exp', 'cube']:
          Log.Fatal("Invalid value for fun: "+ str(fun.group(1))+" .Must be either logcosh,exp or cube")
          return -1

      if "tolerance" in options:
        opts["tol"] = float(options.pop("tolerance"))

      try:
        # Perform ICA.
        with totalTimer:
          model = FastICA(**opts)
          ic = model.fit(data).transform(data)
      except Exception as e:
        return -1

      return totalTimer.ElapsedTime()
def test_fit_transform():
    """Test FastICA.fit_transform"""
    rng = np.random.RandomState(0)
    X = rng.random_sample((100, 10))
    for whiten, n_components in [[True, 5], [False, 10]]:

        ica = FastICA(n_components=5, whiten=whiten, random_state=0)
        Xt = ica.fit_transform(X)
        assert_equal(ica.components_.shape, (n_components, 10))
        assert_equal(Xt.shape, (100, n_components))

        ica = FastICA(n_components=5, whiten=whiten, random_state=0)
        ica.fit(X)
        assert_equal(ica.components_.shape, (n_components, 10))
        Xt2 = ica.transform(X)

        assert_array_almost_equal(Xt, Xt2)
示例#48
0
def fastICA(X):

    from sklearn.decomposition import FastICA  # FastICAのライブラリ
    n, p = X.shape

    M = np.mean(X, axis=0)
    M_est = M

    X2 = X - M

    decomposer = FastICA(n_components=p)
    decomposer.fit(X2)

    A_est = decomposer.mixing_
    W_est = np.linalg.inv(A_est)
    S_est = decomposer.transform(X2)

    return S_est, W_est, M_est
def test_fit_transform():
    # Test FastICA.fit_transform
    rng = np.random.RandomState(0)
    X = rng.random_sample((100, 10))
    for whiten, n_components in [[True, 5], [False, None]]:
        n_components_ = (n_components if n_components is not None else
                         X.shape[1])

        ica = FastICA(n_components=n_components, whiten=whiten, random_state=0)
        Xt = ica.fit_transform(X)
        assert_equal(ica.components_.shape, (n_components_, 10))
        assert_equal(Xt.shape, (100, n_components_))

        ica = FastICA(n_components=n_components, whiten=whiten, random_state=0)
        ica.fit(X)
        assert_equal(ica.components_.shape, (n_components_, 10))
        Xt2 = ica.transform(X)

        assert_array_almost_equal(Xt, Xt2)
 def GenFeatures( self ):
     """Main feature generation method"""
     
     if 'Word2Vec' in self.FeatureGenerationMethod:
         if self.ReTrainW2V:
             W2V = gensim.models.Word2Vec(min_count = 8, size = 256, workers = 1, window = 5)
             W2V.build_vocab(self.Summaries)
             W2V.train(self.Summaries)
             joblib.dump(W2V, 'data' +os.sep+ 'W2VModel' +os.sep+ 'modelW2V.pkl') 
         else:
             W2V = joblib.load('data' +os.sep+ 'W2VModel' +os.sep+ 'modelW2V.pkl')
 
         modelSet = set(W2V.index2word)
         icaFeatures = numpy.zeros( (len(self.Summaries), W2V.vector_size) )
         fastICA = FastICA(n_components = 32, whiten = True, max_iter=2048, algorithm='deflation')
     
         
         for i in xrange(0, len(self.Summaries)):
             textVec = self.Summaries[i]
             features = []
             for word in textVec:
                 if word in modelSet: 
                     features.append(W2V[word])
         
             features = numpy.asarray(features, dtype = numpy.float32)
             
             if features.size > 0:
                 U, s, V = numpy.linalg.svd(features, full_matrices=False)
                 SVDComponents = U
                 SVDVar = numpy.cumsum((s))
     #             pyplot.subplot(1,3,1), pyplot.imshow(U, interpolation = 'none', vmin = 0.0, vmax = 1.0), pyplot.colorbar(), pyplot.subplot(1,3,2), pyplot.imshow(V, interpolation = 'none', vmin = 0.0, vmax = 1.0), pyplot.colorbar(), pyplot.subplot(1,3,3), pyplot.plot(SVDVar), pyplot.show()
     #             print U.shape, V.shape, s.shape
                 try:
                     sourceICA = fastICA.fit(features).transform(features)
                     fastICAComponents = fastICA.components_
                     if self.ICAKurt:
                         kurtS = scipy.stats.kurtosis(fastICAComponents, axis = 1)
                         kurtIdx = numpy.argmax(kurtS)
                         icaFeatures[i, :] = fastICAComponents[kurtIdx, :]
                         print i, 'Kurtosis: ' +str(kurtS[kurtIdx]), 'S Shape: ' +str(sourceICA.shape), 'Kurt Shape: ' +str(kurtS.shape), 'ICA Shape: ' +str(fastICAComponents.shape)
                     else:
                         print 'Loop ' +str(i)+ ' of ' +str(len(self.Summaries)) 
                         icaFeatures[i, :] = numpy.mean(fastICAComponents, axis = 0)
                         
                 except Exception, e:
                     print i, e
                 
             else:
                 print i, type(features), features.shape, features.size
                 icaFeatures[i, :] = numpy.zeros( (W2V.vector_size,) )
             
             
         pyplot.figure(), pyplot.imshow(icaFeatures, interpolation = 'none', vmin = 0.0, vmax = 1.0), pyplot.colorbar(), pyplot.show()
         return icaFeatures
def ICA_reduction(posture, trainblock, componenet):
    currentdirectory = os.getcwd()  # get the directory.
    parentdirectory = os.path.abspath(currentdirectory + "/../..")  # Get the parent directory(2 levels up)
    path = parentdirectory + '\Output Files\E5-Dimensionality Reduction/posture-'+str(posture)+'/TrainBlock-'+str(trainblock)+''
    if not os.path.exists(path):
        os.makedirs(path)
    i_user = 1
    block = 1
    AUC = []
    while i_user <= 31:
        while block <= 6:
            train_data = np.genfromtxt("../../Output Files/E3-Genuine Impostor data per user per posture/posture-"+str(posture)+"/User-"+str(i_user)+"/1-"+str(i_user)+"-"+str(posture)+"-"+str(trainblock)+"-GI.csv", dtype=float, delimiter=",")
            test_data = np.genfromtxt("../../Output Files/E3-Genuine Impostor data per user per posture/posture-"+str(posture)+"/User-"+str(i_user)+"/1-"+str(i_user)+"-"+str(posture)+"-"+str(block)+"-GI.csv", dtype=float, delimiter=",")

            target_train = np.ones(len(train_data))
            row = 0
            while row < len(train_data):
                if np.any(train_data[row, 0:3] != [1, i_user, posture]):
                    target_train[row] = 0
                row += 1

            row = 0
            target_test = np.ones(len(test_data))
            while row < len(test_data):
                if np.any(test_data[row, 0:3] != [1, i_user, posture]):
                    target_test[row] = 0
                row += 1

            sample_train = train_data[:, [3,4,5,6,7,9,11,12,13,14,15,16,17]]
            sample_test = test_data[:, [3,4,5,6,7,9,11,12,13,14,15,16,17]]
            scaler = preprocessing.MinMaxScaler().fit(sample_train)
            sample_train_scaled = scaler.transform(sample_train)
            sample_test_scaled = scaler.transform(sample_test)

            ica = FastICA(n_components=componenet, max_iter=150)
            sample_train_ica = ica.fit(sample_train_scaled).transform(sample_train_scaled)
            sample_test_ica = ica.transform(sample_test_scaled)

            clf = ExtraTreesClassifier(n_estimators=100)
            clf.fit(sample_train_ica, target_train)

            prediction = clf.predict(sample_test_ica)
            auc = metrics.roc_auc_score(target_test, prediction)
            AUC.append(auc)

            block += 1

        block = 1
        i_user += 1
    print(AUC)
    AUC = np.array(AUC)
    AUC = AUC.reshape(31, 6)
    np.savetxt("../../Output Files/E5-Dimensionality Reduction/posture-"+str(posture)+"/TrainBlock-"+str(trainblock)+"/ICA-"+str(componenet)+"-Component.csv", AUC, delimiter=",")
示例#52
0
def calculate_encoder(image,
                       algorithm,
                       patches_size=(DEFAULT_PATCHE_SIZE,DEFAULT_PATCHE_SIZE),
                       projection_dimensios=DEFAULT_PATCHE_SIZE,
                       previous_components=None):
    """
    Gets a higher dimension sparse dictionary.algorithm can be
    ica, kmeans or colors.

    """
    patches = extract_patches_2d(image, patches_size,
            calculate_max_number_of_patches(image, patches_size) )
    patches = numpy.reshape(patches, (patches.shape[0],-1)).astype(float)
    encoder = None
    if algorithm == 'ica':
        encoder = FastICA(n_components=projection_dimensios, whiten=True, max_iter=10)
    elif algorithm == 'kmeans':
        encoder  = MiniBatchKMeans(projection_dimensios,compute_labels=False)
    else:
        raise Exception('Unknow algorithm %s' % algorithm)
    encoder.fit(patches)
    return encoder
示例#53
0
def compute_PCA_ICA_NMF(n_components=5):
    spec_mean = spectra.mean(0)

    # PCA: use randomized PCA for speed
    pca = RandomizedPCA(n_components - 1)
    pca.fit(spectra)
    pca_comp = np.vstack([spec_mean,
                          pca.components_])

    # ICA treats sequential observations as related.  Because of this, we need
    # to fit with the transpose of the spectra
    ica = FastICA(n_components - 1)
    ica.fit(spectra.T)
    ica_comp = np.vstack([spec_mean,
                          ica.transform(spectra.T).T])

    # NMF requires all elements of the input to be greater than zero
    spectra[spectra < 0] = 0
    nmf = NMF(n_components)
    nmf.fit(spectra)
    nmf_comp = nmf.components_

    return pca_comp, ica_comp, nmf_comp
def genW2VFeatures(model, text):
    modelSet = set(model.index2word)
    icaFeatures = numpy.zeros( (len(text), model.vector_size) )
    fastICA = FastICA(n_components = 32, whiten = True, max_iter=2048, algorithm='deflation')

    
    for i in xrange(0, len(text)):
        textVec = text[i]
        features = []
        for word in textVec:
            if word in modelSet: 
                features.append(model[word])
    
        features = numpy.asarray(features, dtype = numpy.float32)
#         featuresNaN = numpy.isnan(features)
#         featuresInf = numpy.isinf(features)
#         print (featuresNaN == True), (featuresInf == True)
        
        if features.size > 0:
            U, s, V = numpy.linalg.svd(features, full_matrices=False)
            SVDComponents = U
            SVDVar = numpy.cumsum((s))
#             pyplot.subplot(1,3,1), pyplot.imshow(U, interpolation = 'none', vmin = 0.0, vmax = 1.0), pyplot.colorbar(), pyplot.subplot(1,3,2), pyplot.imshow(V, interpolation = 'none', vmin = 0.0, vmax = 1.0), pyplot.colorbar(), pyplot.subplot(1,3,3), pyplot.plot(SVDVar), pyplot.show()
#             print U.shape, V.shape, s.shape
            try:
                sourceICA = fastICA.fit(features).transform(features)
                fastICAComponents = fastICA.components_
                if ICAKurt:
                    kurtS = scipy.stats.kurtosis(fastICAComponents, axis = 1)
                    kurtIdx = numpy.argmax(kurtS)
                    icaFeatures[i, :] = fastICAComponents[kurtIdx, :]
                    print i, 'Kurtosis: ' +str(kurtS[kurtIdx]), 'S Shape: ' +str(sourceICA.shape), 'Kurt Shape: ' +str(kurtS.shape), 'ICA Shape: ' +str(fastICAComponents.shape)
                else:
                    print 'Loop ' +str(i)+ ' of ' +str(len(text)) 
                    icaFeatures[i, :] = numpy.mean(fastICAComponents, axis = 0)
                    
            except Exception, e:
                print i, e
                
#                 PCAIdx = numpy.argmax(PCAVar)
#                 icaFeatures[i, :] = PCAComponents[PCAIdx, :]

            
#             pyplot.clf(), pyplot.plot(icaFeatures[i, :]), pyplot.show()
            
        else:
            print i, type(features), features.shape, features.size
            icaFeatures[i, :] = numpy.zeros( (model.vector_size,) )
示例#55
0
def do_ica(data, class_label):

    # ica
    ica = ICA()
    result = ica.fit(data).transform(data)

    plt.plot(result[:, 0], result[:, 1], "o", markersize=7, color="blue", alpha=0.5, label=class_label)

    plt.xlabel("x_values")
    plt.ylabel("y_values")
    plt.xlim([-0.5, 0.5])
    plt.ylim([-0.5, 0.5])
    plt.legend()
    plt.title("Transformed samples versus original data")

    plt.show()
示例#56
0
def split_ica(combined_data, label_1, label_2):

    ica = ICA()
    result = ica.fit(combined_data).transform(combined_data)

    plt.plot(result[0:100, 0], result[0:100, 1], "o", markersize=7, color="blue", alpha=0.5, label=label_1)
    plt.plot(result[100:200, 0], result[100:200, 1], "^", markersize=7, color="red", alpha=0.5, label=label_2)

    plt.xlabel("x_values")
    plt.ylabel("y_values")
    plt.xlim([-0.3, 0.3])
    plt.ylim([-0.3, 0.3])
    plt.legend()
    # plt.title('Transformed samples with class labels from matplotlib.mlab.PCA()')

    plt.show()

    return result
示例#57
0
文件: pcaocr.py 项目: Taohong01/OCR
    def myICA(self):
        # Now we prepare train_data and test_data.
        train = np.array(self.train_cells)[:,:50].reshape(-1,400).astype(np.float32) # Size = (2500,400)
        test = np.array(self.test_cells)[:,50:100].reshape(-1,400).astype(np.float32) # Size = (2500,400)
        print 'looks good'
        nn =1000
        n_samples = 250
        X = np.array(train[(nn):(nn+n_samples),:])
        print 'data X shape is ', X.shape
        # global centering
        X_centered = X - X.mean(axis=0)
        self.plotGallery('original images', X[0:6,:], image_shape=(20,20), n_row=2,n_col=3)
        self.plotGallery('first centered images', X_centered[0:6,:], image_shape=(20,20), n_row=2,n_col=3)        
        # local centering
        X_centered -= X.mean(axis=1).reshape(n_samples,-1)
        self.plotGallery('2nd centered images', X_centered[0:6,:], image_shape=(20,20), n_row=2,n_col=3)    

        num_componets = 200
        ica = FastICA(n_components = num_componets, whiten = True)
        newX_centered = ica.fit(X_centered).transform(X_centered)
        print 'new X centered shape is ', newX_centered.shape

      
        img0 = cv2.resize(ica.mean_.reshape(20,20), (0,0), fx =5, fy=5)
        
        self.plotGallery('ica original test image set', X[0:6,:], image_shape=(20,20), n_row=2,n_col=3)
        self.plotGallery('ica components', ica.components_[0:6,:], image_shape=(20,20), n_row=2,n_col=3)
        
        #sumImage = np.dot(newX_centered, ica.components_) \
        #+ (X.mean(axis=1).reshape(n_samples,-1) +X.mean(axis=0))/10
        print ica.mixing_.shape
        print newX_centered.shape
        print ica.mean_.shape
        print 'Is components matrix equivalent to mixing matrix after transpose? Based on this testing, the answer is', np.allclose(ica.mixing_.T, ica.components_)
        sumImage = np.dot(newX_centered, ica.mixing_.T) +ica.mean_
        print newX_centered.shape
        print ica.components_.shape
        print sumImage.shape
        
        self.plotGallery('ica sum images', sumImage[0:6,:], image_shape=(20,20), n_row=2,n_col=3)
        print 'the conclusion of this ICA test is that to reconstruct the image, we should use the dot product of the image transformation and the mixing matrix instead of components, also we should add the mean back'
        
        self.printALine()
示例#58
0
    def RunICAScikit(q):
      totalTimer = Timer()

      # Load input dataset.
      data = np.genfromtxt(self.dataset, delimiter=',')

      s = re.search('-s (\d+)', options)
      s = 0 if not s else int(s.group(1))

      try:
        # Perform ICA.
        with totalTimer:
          model = FastICA(random_state=s)
          ic = model.fit(data).transform(data)
          mixing = model.get_mixing_matrix()
      except Exception as e:
        q.put(-1)
        return -1

      time = totalTimer.ElapsedTime()
      q.put(time)
      return time
示例#59
0
文件: get_ca1.py 项目: cyanut/ca1-3d
def plot_3d(res, label, ca1_label, cell_colors, cell_labels):
    pca = PCA()
    X = res
    S_pca = pca.fit(X).transform(X)
    ica = FastICA()
    S_ica = ica.fit(X).transform(X)
    S_ica /= S_ica.std(axis=0)

    fig = plt.figure()
    #ax = fig.add_subplot(111, projection='3d')
    ax = Axes3D(fig)

    plot_data = []

    plot_data.append(res[~label[:,0] & ~label[:,1] & ca1_label])
    plot_data.append(res[label[:,0] & ~label[:,1] & ca1_label])
    plot_data.append(res[~label[:,0] & label[:,1] & ca1_label])
    plot_data.append(res[label[:,0] & label[:,1] & ca1_label])
    plot_data.append(res[~ca1_label])

    for plot, color, label in zip(plot_data, cell_colors, cell_labels):
        ax.scatter(plot[:,0], plot[:,1], plot[:,2], c=color, s=args.cell_diameter / 2, label=label, marker=".", edgecolors=color)

    axis_list = [pca.components_.T, ica.mixing_]
    colors = ['orange', 'red']
    source = [0,0,0]
    res_mean = res.mean(axis=0)
    source_list = []
    for i in range(3):
        source_list.append(np.array([res_mean[i]] * 3))
    
    for color, axis in zip(colors, axis_list):
        axis /= np.sqrt(np.sum(axis ** 2, axis=0))
        #axis *= res.std()
        x,y,z = axis
        l = 200
        #pca_axis = ax.quiver(source_list[0] + l*x, source_list[1] + l*y, source_list[2]+l*z,x,y,z, color=color, length = l, arrow_length_ratio=0.1)
    plt.show()