当前位置: 首页>>代码示例>>Python>>正文


Python PCA.set_params方法代码示例

本文整理汇总了Python中sklearn.decomposition.PCA.set_params方法的典型用法代码示例。如果您正苦于以下问题:Python PCA.set_params方法的具体用法?Python PCA.set_params怎么用?Python PCA.set_params使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.decomposition.PCA的用法示例。


在下文中一共展示了PCA.set_params方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: pca

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import set_params [as 别名]
def pca(x, variance_ratio=0.90):
    pca = PCA()
    pca.fit(x)
    acc = 0.0
    for ind, ele in enumerate(pca.explained_variance_ratio_):
        if acc > variance_ratio: break
        acc += ele
    ind += 1
    pca.set_params(n_components=ind)
    x = pca.fit_transform(x)
    return x, pca
开发者ID:PiscesDream,项目名称:HW_Plans,代码行数:13,代码来源:main.py

示例2: FeatureSel

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import set_params [as 别名]
class FeatureSel(BaseEstimator,TransformerMixin):
    def __init__(self,k_best=5,pca_comp=8):
        self.k_best=k_best
        self.pca_comp=pca_comp
        if pca_comp>0:
            self.pca=PCA(n_components=self.pca_comp)
        if k_best>0:
            self.skb=SelectKBest(k=self.k_best)


    def set_params(self, **parameters):

        for parameter, value in parameters.items():
            setattr(self,parameter, value)
        return self

        self.pca.set_params(n_components=self.pca_comp)

        self.skb.set_params(k=self.k_best)

        return self


    def transform(self,X):
        X1=self.pca.transform(X)
        X2=self.skb.transform(X)

        return np.hstack((X1,X2))


    def fit_transform(self,X,y):


        X1=self.pca.fit_transform(X,y)
        X2=self.skb.fit_transform(X,y)

        return np.hstack((X1,X2))

    def fit(self,X,y):
        if self.pca_comp>0:
            self.pca.fit(X,y)
        if self.k_best>0:
            self.skb.fit(X,y)
开发者ID:kanhua,项目名称:Enron-Email-Fraud,代码行数:45,代码来源:preprocess_data.py

示例3: pca

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import set_params [as 别名]
def pca(trainx, testx, p = 0.95, verbose=False, n_components=False, get_pca=False):
    pca = PCA(whiten=True)
    pca.fit(trainx)
    components, variance = 0, 0.0
    for components, ele in enumerate(pca.explained_variance_ratio_):
        variance += ele
        if variance > p: break
    components += 1
    if verbose:
        print 'n_components=%d'%components
    pca.set_params(n_components=components)
    pca.fit(trainx)

    trainx = pca.transform(trainx)
    testx = pca.transform(testx)

    ret = (trainx, testx)
    if n_components: ret += (components,)
    if get_pca: ret += (pca,)
    return ret
开发者ID:PiscesDream,项目名称:Lab_MMAPM,代码行数:22,代码来源:common.py

示例4: KNN_predict

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import set_params [as 别名]
def KNN_predict(x, y, predx, L=None, M=None, K=3):
#   if not M:
#       if L:
#           M = np.dot(L, L.T)
#       else:
#           M = np.eye(dim)
    

    pca = PCA(whiten=True)
    pca.fit(x)
#    print pca.explained_variance_ratio_
    components, variance = 0, 0.0
    for components, ele in enumerate(pca.explained_variance_ratio_):
        variance += ele
        if variance > 0.95: break
    components += 1
    print 'n_components=%d'%components
    pca.set_params(n_components=components)
    pca.fit(x)

    x = pca.transform(x)
    predx = pca.transform(predx)

    n, dim = x.shape
    if not L:
        L = np.eye(dim)

    Lx = np.dot(x, L)
    Lpredx = np.dot(predx, L)

    print 'Predicting ...'
    predict = []
    for ind, ele in enumerate(Lpredx):
        dist = ((ele - Lx)**2).sum(1)
        pred = y[dist.argsort()[1:K+1]]
        bincount = np.bincount(pred)
        maxcount = bincount.max()
        candidates = [predy for predy in pred if bincount[predy] == maxcount]
        predict.append( np.random.choice(candidates) )
    return np.array(predict)
开发者ID:PiscesDream,项目名称:Lab_MMAPM,代码行数:42,代码来源:knn.py

示例5: PCA

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import set_params [as 别名]
Created on Tue Jun 24 17:12:40 2014

@author: sean
"""

import numpy as np
from sklearn.decomposition import PCA
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
pca = PCA(n_components=2)
X_transform=pca.fit_transform(X)
X_inv=pca.inverse_transform(X_transform)
print 'below 2 matrices should be equal'
print X
print X_inv
print 'whiten'
pca.set_params(whiten=True)
X_transform=pca.fit_transform(X)
X_inv=pca.inverse_transform(X_transform)
print 'below 2 matrices should be equal'
print X
print X_inv

print 'whiten'
print 'pca 1 component'
pca.set_params(n_components=1)
X_transform=pca.fit_transform(X)
X_inv=pca.inverse_transform(X_transform)
print X

print 'below 2 matrices should be equal'
print X_inv
开发者ID:seanv507,项目名称:sklearn,代码行数:33,代码来源:testpca.py

示例6: load_iris

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import set_params [as 别名]
#   iris_data = load_iris()
#   X = iris_data['data']
#   Y = iris_data['target']
#    trainx, testx, trainy, testy = train_test_split(X, Y, test_size=0.66)

    (trainx, trainy), (testx, testy) = load_mnist(percentage=0.01, skip_valid=True)

    pca = PCA(whiten=True)
    pca.fit(trainx)
    components, variance = 0, 0.0
    for components, ele in enumerate(pca.explained_variance_ratio_):
        variance += ele
        if variance > 0.90: break
    components += 1
    print 'n_components=%d'%components
    pca.set_params(n_components=components)
    pca.fit(trainx)

    trainx = pca.transform(trainx)
    testx = pca.transform(testx)

    K = 3
    dim = components
    knn(trainx, testx, trainy, testy, np.eye(dim), K)
    knn(trainx, testx, trainy, testy, np.random.rand(dim, dim), K)

    lmnn = LMNN(K=K, mu=0.5)
    lmnn.fit(trainx, trainy, lr=2e-6, max_iter=1500, gradient_cycle=100, verbose=True)
    knn(trainx, testx, trainy, testy, lmnn.M, K)

    lmnn = _LMNN(k=K, learn_rate=2e-6, max_iter=1000)
开发者ID:PiscesDream,项目名称:Lab_MMAPM,代码行数:33,代码来源:LMNN.py

示例7: PcaWhitening

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import set_params [as 别名]
class PcaWhitening(object):
    """
    Whitens the data using principal component analysis.

    To speed up training the transformation, you can specify how many data
    points from the training data source to use.

    Parameters
    ----------
    n_train_vectors : int or None
        Number of data points to use when training the PCA. If `None`, use all
        values.
    n_components : int or None
        Number of components to use in PCA. If `None`, use all components,
        resulting in no data reduction.
    """

    def __init__(self, n_train_vectors=None, n_components=None):
        from sklearn.decomposition import PCA
        self.pca = PCA(whiten=True, n_components=self.n_components)
        self.fit = False
        self.n_train_vectors = n_train_vectors
        self.n_components = n_components

    def __call__(self, data):
        """Project the :param:data using the PCA projection."""
        if self.fit:
            # flatten features, pca only works on 1d arrays
            data_shape = data.shape
            data_flat = data.reshape((data_shape[0], -1))
            whitened_data = self.pca.transform(data_flat)
            # get back original shape
            return whitened_data.reshape(data_shape).astype(np.float32)
        else:
            return data

    def train(self, data_source, batch_size=4096):
        """
        Fit the PCA projection to data.

        Parameters
        ----------
        data_source : :class:DataSource
            Data to use for fitting the projection
        batch_size : int
            Not used here.
        """

        # select a random subset of the data if self.n_train_vectors is not
        # None
        if self.n_train_vectors is not None:
            sel_data = list(np.random.choice(data_source.n_data,
                                             size=self.n_train_vectors,
                                             replace=False))
        else:
            sel_data = slice(None)
        data = data_source[sel_data][0]  # ignore the labels
        data_flat = data.reshape((data.shape[0], -1))  # flatten features
        self.pca.fit(data_flat)
        self.fit = True

    def load(self, filename):
        """
        Load the PCA projection parameters from a pickle file.

        Parameters
        ----------
        filename : str
            Pickle file containing the projection parameters

        """
        with open(filename, 'r') as f:
            self.pca.set_params(pickle.load(f))
            self.fit = True

    def save(self, filename):
        """
        Save the PCA projection parameters to a pickle file.

        Parameters
        ----------
        filename : str
            Pickle file to store the parameters to
        """
        with open(filename, 'w') as f:
            pickle.dump(self.pca.get_params(deep=True), f)
开发者ID:fdlm,项目名称:dmgr,代码行数:88,代码来源:preprocessing.py


注:本文中的sklearn.decomposition.PCA.set_params方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。