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


Python Nystroem.fit方法代码示例

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


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

示例1: test_nystroem_approximation

# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import fit [as 别名]
def test_nystroem_approximation():
    # some basic tests
    rnd = np.random.RandomState(0)
    X = rnd.uniform(size=(10, 4))

    # With n_components = n_samples this is exact
    X_transformed = Nystroem(n_components=X.shape[0]).fit_transform(X)
    K = rbf_kernel(X)
    assert_array_almost_equal(np.dot(X_transformed, X_transformed.T), K)

    trans = Nystroem(n_components=2, random_state=rnd)
    X_transformed = trans.fit(X).transform(X)
    assert_equal(X_transformed.shape, (X.shape[0], 2))

    # test callable kernel
    linear_kernel = lambda X, Y: np.dot(X, Y.T)
    trans = Nystroem(n_components=2, kernel=linear_kernel, random_state=rnd)
    X_transformed = trans.fit(X).transform(X)
    assert_equal(X_transformed.shape, (X.shape[0], 2))

    # test that available kernels fit and transform
    kernels_available = kernel_metrics()
    for kern in kernels_available:
        trans = Nystroem(n_components=2, kernel=kern, random_state=rnd)
        X_transformed = trans.fit(X).transform(X)
        assert_equal(X_transformed.shape, (X.shape[0], 2))
开发者ID:CheMcCandless,项目名称:scikit-learn,代码行数:28,代码来源:test_kernel_approximation.py

示例2: test_nystroem_callable

# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import fit [as 别名]
def test_nystroem_callable():
    # Test Nystroem on a callable.
    rnd = np.random.RandomState(42)
    n_samples = 10
    X = rnd.uniform(size=(n_samples, 4))

    def logging_histogram_kernel(x, y, log):
        """Histogram kernel that writes to a log."""
        log.append(1)
        return np.minimum(x, y).sum()

    kernel_log = []
    X = list(X)     # test input validation
    Nystroem(kernel=logging_histogram_kernel,
             n_components=(n_samples - 1),
             kernel_params={'log': kernel_log}).fit(X)
    assert_equal(len(kernel_log), n_samples * (n_samples - 1) / 2)

    def linear_kernel(X, Y):
        return np.dot(X, Y.T)

    # if degree, gamma or coef0 is passed, we raise a warning
    msg = "Don't pass gamma, coef0 or degree to Nystroem"
    params = ({'gamma': 1}, {'coef0': 1}, {'degree': 2})
    for param in params:
        ny = Nystroem(kernel=linear_kernel, **param)
        with pytest.raises(ValueError, match=msg):
            ny.fit(X)
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:30,代码来源:test_kernel_approximation.py

示例3: __init__

# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import fit [as 别名]
class NystromScikit:

    """
        Nystrom implementation form Scikit Learn wrapper.
        The main difference is in selection of inducing inputs.
    """

    def __init__(self, rank=10, random_state=42):
        """
        :param rank: (``int``) Maximal decomposition rank.

        :param random_state: (``int``) Random generator seed.
        """
        self.trained = False
        self.rank = rank
        self.random_state = random_state


    def fit(self, K, y):
        """
        Fit approximation to the kernel function / matrix.

        :param K: (``numpy.ndarray``) or of (``Kinterface``). The kernel to be approximated with G.

        :param y: (``numpy.ndarray``) Class labels :math:`y_i \in {-1, 1}` or regression targets.
        """
        assert isinstance(K, Kinterface)

        self.n           = K.shape[0]
        kernel           = lambda x, y: K.kernel(x, y, **K.kernel_args)
        self.model       = Nystroem(kernel=kernel,
                                    n_components=self.rank,
                                    random_state=self.random_state)

        self.model.fit(K.data, y)
        self.active_set_ = list(self.model.component_indices_[:self.rank])
        assert len(set(self.active_set_)) == len(self.active_set_) == self.rank
        R = self.model.normalization_
        self.G = K[:, self.active_set_].dot(R)
        self.trained = True
开发者ID:rahlk,项目名称:Bellwether,代码行数:42,代码来源:nystrom.py

示例4: __init__

# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import fit [as 别名]
from params import ts_depths,n_fea,gamma, np, sp
class Whitener:
    def __init__(self,X):
        self.Xmean = X.mean(0)
        self.Xstd = X.std(0)
    def whiten(self,Z):
        return (Z-self.Xmean)/self.Xstd
    def unwhiten(self,Zw):
        return Zw*self.Xstd + self.Xmean

def expkern(x,y):
    return np.exp(-gamma*la.norm(x-y))

wh = Whitener(ts_depths)
ts_depths_w = wh.whiten(ts_depths)
xx = np.linspace(ts_depths_w.min(),ts_depths_w.max(),n_fea)[:,np.newaxis]
rbf_tr = Nystroem(expkern,gamma,n_components=n_fea)
#rbf_tr = Nystroem(gamma=gamma,n_components=n_fea)
#class rbf_transformer:
#    def __init__(self,X,gamma):
#        self.X = X
#        self.gamma = gamma
#    def transform(self,xx):
#        return rbf_kernel(xx,self.X) 

#rbf_tr = rbf_transformer(xx,gamma)
#rbf_tr.fit(x)
rbf_tr.fit(xx)
ts_depths_tr = rbf_tr.transform(ts_depths_w)

开发者ID:mjhay,项目名称:neem_sonic_model,代码行数:31,代码来源:transformer.py

示例5: enumerate

# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import fit [as 别名]
 XtrainT = kcca.transform(ktrain)
 XtestT = kcca.transform(ktest)
 kccaScores = np.zeros((2,np.alen(nComponents)))
 for i,n in enumerate(nComponents):   
     kccaScores[:,i] = util.classify(XtrainT[:,0:n],XtestT[:,0:n],labelsTrain,labelsTest)
 
 #%% Subsampling methods
 kpls = PLSRegression(n_components=150)
 nComponents = np.arange(173,2173,100)
 
 # Nystroem method
 elapTimeNys = np.zeros(np.shape(nComponents))
 kplsScoresNys = np.zeros((2,3))
 for i,n in enumerate(nComponents):
     nys = Nystroem(n_components=n,gamma=gamma)
     nys.fit(Xtrain)
     ktrain = nys.transform(Xtrain)
     ktest = nys.transform(Xtest)
     startTime = timeit.default_timer()
     kpls.fit(ktrain,Ytrain)
     elapTimeNys[i] = timeit.default_timer() - startTime
     XtrainT = kpls.transform(ktrain)
     XtestT = kpls.transform(ktest)
     
     if n==573:
         kplsScoresNys[:,0] = util.classify(XtrainT,XtestT,labelsTrain,labelsTest)
     elif n==1073:
         kplsScoresNys[:,1] = util.classify(XtrainT,XtestT,labelsTrain,labelsTest)
     elif n==1573:
         kplsScoresNys[:,2] = util.classify(XtrainT,XtestT,labelsTrain,labelsTest)
 
开发者ID:manuwhs,项目名称:Trapyng,代码行数:32,代码来源:baseFeatureExtractionLib.py

示例6: ParametricModelApproximation

# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import fit [as 别名]
class ParametricModelApproximation(object):
    """Approximate a Gaussian Process by a parametric model.

    Approximating a Gaussian Process by a parametric model can be useful if
    one has to evaluate a sample function from the GP repeatedly or on many
    evaluation points as this would become computationally very expensive
    with a GP.

    Parameters
    ----------
    model : GaussianProcessRegressor
        The Gaussian Process which is to be approximated

    bounds: list of pair of floats
        The boundaries of the data space. This is used when determining the
        features of the parametric approximation (they are centered at random
        points in the data space)

    n_components: int
        The number of features/parameters of the parametric model

    seed: int
        The seed of the random number generator
    """
    def __init__(self, model, bounds, n_components, seed):
        self.gp = model
        self.bounds = bounds
        self.n_components = n_components
        self.rng = np.random.RandomState(seed)

        self.X_space = self.rng.uniform(self.bounds[:, 0], self.bounds[:, 1],
                                        (1000, self.bounds.shape[0]))

        assert self.gp.X_fit_.shape[1] == self.X_space.shape[1]

        self.kernel = self.gp.kernel_
        self.nystr = Nystroem(
            n_components=min(self.n_components, self.X_space.shape[0]),
            kernel='precomputed', random_state=self.rng)
        self.nystr.fit(self.kernel(self.X_space))

    def determine_coefs(self, X_query=None, y_query_samples=None, n_samples=1):
        """ Determine coefficients of parametric model.

        Simulate an evaluation at X_query with outcomes y_query_samples.
        Determine coefficients of parametric model the updated GP.

        Parameters
        ----------
        X_query : ndarray-like, default: None
            The query point at which an additional evaluation is simulated.
            If None, a parametric approximation of the unmodified GP is
            returned.

        y_query_samples: ndarray-like, default: None
            The possible outcomes of a query at X_query.

        n_samples: int
            The number of independent samples of model coefficients from the
            Bayesian posterior over model coefficients
        """
        if X_query is not None:
            X_query = np.asarray(X_query)
            X_queried = np.vstack((self.gp.X_fit_, X_query))
        else:
            X_queried = self.gp.X_fit_
            y_queried = self.gp.y_fit_

        Phi = self.nystr.transform(self.kernel(self.X_space, X_queried))
        A = Phi.T.dot(Phi) + self.gp.alpha * np.eye(Phi.shape[1])
        A_inv = np.linalg.inv(A)

        cov = self.gp.alpha * A_inv

        coefs = \
            np.empty((n_samples, self.n_components, y_query_samples.shape[0]))
        for i in range(y_query_samples.shape[0]): # XXX: Vectorize
            y_queried = np.hstack((self.gp.y_fit_, y_query_samples[i]))
            mean = A_inv.dot(Phi.T).dot(y_queried)
            coefs[:, :, i] = self.rng.multivariate_normal(mean, cov, n_samples)
        return np.array(coefs)

    def __call__(self, X, coefs):
        """ Evaluate parametric model at X for the given sampled coefficients.

        Parameters
        ----------
        X : ndarray-like
            The points at which the parametric model is to be evaluated

        coefs: ndarray-like
            The coefficients of the parametric model.
        """
        X = np.atleast_2d(X)

        Phi = self.nystr.transform(self.kernel(self.X_space, X))
        f = Phi.dot(coefs)
        return f
开发者ID:jmetzen,项目名称:bayesian_optimization,代码行数:100,代码来源:model.py


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