本文整理汇总了Python中sklearn.kernel_approximation.Nystroem.transform方法的典型用法代码示例。如果您正苦于以下问题:Python Nystroem.transform方法的具体用法?Python Nystroem.transform怎么用?Python Nystroem.transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.kernel_approximation.Nystroem
的用法示例。
在下文中一共展示了Nystroem.transform方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SparseKernelClassifier
# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import transform [as 别名]
class SparseKernelClassifier(CDClassifier):
def __init__(self, mode='exact', kernel='rbf', gamma=1e-3, C=1, alpha=1,
n_components=500, n_jobs=1, verbose=False):
self.mode = mode
self.kernel = kernel
self.gamma = gamma
self.C = C
self.alpha = alpha
self.n_components = n_components
self.n_jobs = n_jobs
self.verbose = verbose
super(SparseKernelClassifier, self).__init__(
C=C,
alpha=alpha,
loss='squared_hinge',
penalty='l1',
multiclass=False,
debiasing=True,
Cd=C,
warm_debiasing=True,
n_jobs=n_jobs,
verbose=False,
)
def fit(self, X, y):
if self.mode == 'exact':
K = pairwise_kernels(
X,
metric=self.kernel,
filter_params=True,
gamma=self.gamma
)
self.X_train_ = X
else:
self.kernel_sampler_ = Nystroem(
kernel=self.kernel,
gamma=self.gamma,
n_components=self.n_components
)
K = self.kernel_sampler_.fit_transform(X)
super(SparseKernelClassifier, self).fit(K, y)
return self
def decision_function(self, X):
if self.mode == 'exact':
K = pairwise_kernels(
X, self.X_train_,
metric=self.kernel,
filter_params=True,
gamma=self.gamma
)
else:
K = self.kernel_sampler_.transform(X)
return super(SparseKernelClassifier, self).decision_function(K)
示例2: WeightedSparseKernelClassifier
# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import transform [as 别名]
class WeightedSparseKernelClassifier(LinearSVC):
def __init__(
self, mode='exact', kernel='rbf', gamma=1e-3, C=1,
multi_class='ovr', class_weight='auto', n_components=5000,
verbose=False
):
self.mode = mode
self.kernel = kernel
self.gamma = gamma
self.C = C
self.multi_class = multi_class
self.class_weight = class_weight
self.n_components = n_components
self.verbose = verbose
super(WeightedSparseKernelClassifier, self).__init__(
C=C,
loss='squared_hinge',
penalty='l1',
dual=False,
verbose=verbose
)
def fit(self, X, y):
if self.mode == 'exact':
K = pairwise_kernels(
X,
metric=self.kernel,
filter_params=True,
gamma=self.gamma
)
self.X_train_ = X
else:
self.kernel_sampler_ = Nystroem(
kernel=self.kernel,
gamma=self.gamma,
n_components=self.n_components
)
K = self.kernel_sampler_.fit_transform(X)
return super(WeightedSparseKernelClassifier, self).fit(K, y)
def decision_function(self, X):
if self.mode == 'exact':
K = pairwise_kernels(
X, self.X_train_,
metric=self.kernel,
filter_params=True,
gamma=self.gamma
)
else:
K = self.kernel_sampler_.transform(X)
return super(WeightedSparseKernelClassifier, self).decision_function(K)
示例3: test_nystroem_singular_kernel
# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import transform [as 别名]
def test_nystroem_singular_kernel():
# test that nystroem works with singular kernel matrix
rng = np.random.RandomState(0)
X = rng.rand(10, 20)
X = np.vstack([X] * 2) # duplicate samples
gamma = 100
N = Nystroem(gamma=gamma, n_components=X.shape[0]).fit(X)
X_transformed = N.transform(X)
K = rbf_kernel(X, gamma=gamma)
assert_array_almost_equal(K, np.dot(X_transformed, X_transformed.T))
assert_true(np.all(np.isfinite(Y)))
示例4: __init__
# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import transform [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)
示例5: enumerate
# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import transform [as 别名]
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)
# RBF sampler method
示例6: ParametricModelApproximation
# 需要导入模块: from sklearn.kernel_approximation import Nystroem [as 别名]
# 或者: from sklearn.kernel_approximation.Nystroem import transform [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