Beispiel #1
0
    def surface_normals(S, number_of_neighbors):

        nbrs = _NearestNeighbors(n_neighbors=number_of_neighbors,
                                 algorithm='kd_tree').fit(S)
        distances, indices = nbrs.kneighbors(S)

        pca = _PCA()
        normals_S = []  #np.zeros(2)
        principle_axes = []  #np.zeros([2,2])
        surface_region = _np.zeros([1, S.shape[1]])

        for i in range(indices.shape[0]):

            for j in indices[i]:
                surface_region = _np.append(surface_region, S[j])

            surface_region = _np.reshape(surface_region, (-1, S.shape[1]))
            surface_region = _np.delete(surface_region, 0, 0)

            pca_region = pca.fit(surface_region)

            principle_axes.append(pca_region.components_)
            #principle_axes = _np.append(principle_axes,pca_region.components_)
            #normals_S = np.append(normals_S,pca_region.components_[1])
            normals_S.append(pca_region.components_[1])

        #normals_S = np.reshape(normals_S,(-1,2))
        #principle_axes = np.reshape(principle_axes,(-1,2,2))

        #return [np.delete(normals_S,0,0),principle_axes,distances]
        return normals_S, principle_axes, distances
Beispiel #2
0
def with_pca(features, n_pcs=20, thresh=3):
    """PCA-based outlier removal function
    
    Args:
        features - the input feature data for finding outliers
        n_pcs    - the number of principal components to look for outliers in
        thresh   - the standard-deviation multiplier for outlier id 
                   (e.g. thresh = 3 means values > 3 stdv from the mean will 
                   be flagged as outliers)
    
    Returns:
        mask     - a boolean array with True for good values, False for outliers
    """
    # create the bool mask where outlier samples will be flagged as False
    mask = _np.repeat(True, features.shape[0])

    # set up the pca reducer, then transform the data
    reducer = _PCA(n_components=n_pcs, whiten=True)
    transformed = reducer.fit_transform(features)

    # loop through the number of pcs set and flag values outside the threshold
    for i in range(n_pcs):
        outliers = abs(transformed[:, i]) > thresh
        mask[outliers] = False

    return mask
Beispiel #3
0
    def __init__(self, options):
        self.handle_options(options)
        out_params = convert_params(options.get('params', {}),
                                    ints=['k'],
                                    aliases={'k': 'n_components'})

        self.estimator = _PCA(**out_params)
Beispiel #4
0
 def get_pca(self):  #gets a PCA of the data the object was initialized with
     """
     Principal component analysis of data 
     """
     pca = _PCA()
     self.pca = pca.fit_transform(self._data)
     self.pca_explained_var = pca.explained_variance_ratio_ * 100  #output a percent of the variance explained within the object defined here
     return
Beispiel #5
0
 def get_pca(self):
     """
     Principal component analysis of data 
     """
     pca = _PCA()
     self.pca = pca.fit_transform(self._data)
     self.pca_explained_var = pca.explained_variance_ratio_ * 100
     return
    def _fit(self, X):
        self.estimator_ = _PCA(iterated_power=self.iterated_power,
                               n_components=self.n_components,
                               random_state=self.random_state,
                               svd_solver=self.svd_solver,
                               tol=self.tol,
                               whiten=self.whiten).fit(X)

        return self
Beispiel #7
0
def pca(features, n_pcs=100):
    """PCA transformation function

    Args:
        features - the input feature data to transform
        n_pcs    - the number of components to keep after transformation

    Returns:
        an array of PCA-transformed features
    """
    reducer = _PCA(n_components=n_pcs, whiten=True)
    return reducer.fit_transform(features)
Beispiel #8
0
def main():
    print("Tesing the performance of PCA...")
    # Generate data
    seed(100)
    X = randint(0, 10, (6, 3))
    X_train, X_test = train_test_split(X)
    # Fit and transform data.
    pca = PCA()
    pca.fit(X, n_components=2)
    print("Imylu PCA:\n", pca.transform(X))
    # Compare result with scikit-learn.
    _pca = _PCA(n_components=2)
    print("Sklearn PCA:\n", _pca.fit(X).transform(X))
Beispiel #9
0
def myPca(features, n_pcs=100):
    reducer = _PCA(whiten=True)
    #features = StandardScaler().fit_transform(features)
    transformed = reducer.fit_transform(features)

    # plt.figure()
    # plt.plot(np.cumsum(reducer.explained_variance_ratio_))
    # plt.xlabel('Number of Components')
    # plt.ylabel('Variance (%)') #for each component
    # plt.title('Modified Stanford-CCB Explained Variance')
    # plt.grid()
    # plt.show()

    return reducer, transformed[:, 0:n_pcs]
Beispiel #10
0
    def __init__(self, *args, **params):
        try:
            self.variables = args[:]
            assert len(self.variables) > 0
        except:
            raise Exception('Syntax error: Expected "<field> ..."')

        out_params = self.convert_params(
            params,
            ints=['k'],
            aliases = {
                'k': 'n_components'
            }
        )

        self.estimator = _PCA(**out_params)
Beispiel #11
0
 def __init__(self, **kwargs):
     super().__init__()
     self._method = _PCA(**kwargs)
     self.hyperparams = self._method.get_params