本文整理汇总了Python中sklearn.kernel_approximation.RBFSampler方法的典型用法代码示例。如果您正苦于以下问题:Python kernel_approximation.RBFSampler方法的具体用法?Python kernel_approximation.RBFSampler怎么用?Python kernel_approximation.RBFSampler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.kernel_approximation
的用法示例。
在下文中一共展示了kernel_approximation.RBFSampler方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rbf_sampler
# 需要导入模块: from sklearn import kernel_approximation [as 别名]
# 或者: from sklearn.kernel_approximation import RBFSampler [as 别名]
def test_rbf_sampler():
# test that RBFSampler approximates kernel on random data
# compute exact kernel
gamma = 10.
kernel = rbf_kernel(X, Y, gamma=gamma)
# approximate kernel mapping
rbf_transform = RBFSampler(gamma=gamma, n_components=1000, random_state=42)
X_trans = rbf_transform.fit_transform(X)
Y_trans = rbf_transform.transform(Y)
kernel_approx = np.dot(X_trans, Y_trans.T)
error = kernel - kernel_approx
assert_less_equal(np.abs(np.mean(error)), 0.01) # close to unbiased
np.abs(error, out=error)
assert_less_equal(np.max(error), 0.1) # nothing too far off
assert_less_equal(np.mean(error), 0.05) # mean is fairly close
示例2: test_skewed_chi2_sampler
# 需要导入模块: from sklearn import kernel_approximation [as 别名]
# 或者: from sklearn.kernel_approximation import RBFSampler [as 别名]
def test_skewed_chi2_sampler():
# test that RBFSampler approximates kernel on random data
# compute exact kernel
c = 0.03
# set on negative component but greater than c to ensure that the kernel
# approximation is valid on the group (-c; +\infty) endowed with the skewed
# multiplication.
Y[0, 0] = -c / 2.
# abbreviations for easier formula
X_c = (X + c)[:, np.newaxis, :]
Y_c = (Y + c)[np.newaxis, :, :]
# we do it in log-space in the hope that it's more stable
# this array is n_samples_x x n_samples_y big x n_features
log_kernel = ((np.log(X_c) / 2.) + (np.log(Y_c) / 2.) + np.log(2.) -
np.log(X_c + Y_c))
# reduce to n_samples_x x n_samples_y by summing over features in log-space
kernel = np.exp(log_kernel.sum(axis=2))
# approximate kernel mapping
transform = SkewedChi2Sampler(skewedness=c, n_components=1000,
random_state=42)
X_trans = transform.fit_transform(X)
Y_trans = transform.transform(Y)
kernel_approx = np.dot(X_trans, Y_trans.T)
assert_array_almost_equal(kernel, kernel_approx, 1)
assert np.isfinite(kernel).all(), \
'NaNs found in the Gram matrix'
assert np.isfinite(kernel_approx).all(), \
'NaNs found in the approximate Gram matrix'
# test error is raised on when inputs contains values smaller than -c
Y_neg = Y.copy()
Y_neg[0, 0] = -c * 2.
assert_raises(ValueError, transform.transform, Y_neg)
示例3: test_input_validation
# 需要导入模块: from sklearn import kernel_approximation [as 别名]
# 或者: from sklearn.kernel_approximation import RBFSampler [as 别名]
def test_input_validation():
# Regression test: kernel approx. transformers should work on lists
# No assertions; the old versions would simply crash
X = [[1, 2], [3, 4], [5, 6]]
AdditiveChi2Sampler().fit(X).transform(X)
SkewedChi2Sampler().fit(X).transform(X)
RBFSampler().fit(X).transform(X)
X = csr_matrix(X)
RBFSampler().fit(X).transform(X)
示例4: __init__
# 需要导入模块: from sklearn import kernel_approximation [as 别名]
# 或者: from sklearn.kernel_approximation import RBFSampler [as 别名]
def __init__(self, gamma=1.0, n_components=100, random_state=None):
self._hyperparams = {
'gamma': gamma,
'n_components': n_components,
'random_state': random_state}
self._wrapped_model = Op(**self._hyperparams)
示例5: test_objectmapper
# 需要导入模块: from sklearn import kernel_approximation [as 别名]
# 或者: from sklearn.kernel_approximation import RBFSampler [as 别名]
def test_objectmapper(self):
df = pdml.ModelFrame([])
self.assertIs(df.kernel_approximation.AdditiveChi2Sampler,
ka.AdditiveChi2Sampler)
self.assertIs(df.kernel_approximation.Nystroem, ka.Nystroem)
self.assertIs(df.kernel_approximation.RBFSampler, ka.RBFSampler)
self.assertIs(df.kernel_approximation.SkewedChi2Sampler,
ka.SkewedChi2Sampler)
示例6: test_skewed_chi2_sampler
# 需要导入模块: from sklearn import kernel_approximation [as 别名]
# 或者: from sklearn.kernel_approximation import RBFSampler [as 别名]
def test_skewed_chi2_sampler():
# test that RBFSampler approximates kernel on random data
# compute exact kernel
c = 0.03
# set on negative component but greater than c to ensure that the kernel
# approximation is valid on the group (-c; +\infty) endowed with the skewed
# multiplication.
Y[0, 0] = -c / 2.
# abbreviations for easier formula
X_c = (X + c)[:, np.newaxis, :]
Y_c = (Y + c)[np.newaxis, :, :]
# we do it in log-space in the hope that it's more stable
# this array is n_samples_x x n_samples_y big x n_features
log_kernel = ((np.log(X_c) / 2.) + (np.log(Y_c) / 2.) + np.log(2.) -
np.log(X_c + Y_c))
# reduce to n_samples_x x n_samples_y by summing over features in log-space
kernel = np.exp(log_kernel.sum(axis=2))
# approximate kernel mapping
transform = SkewedChi2Sampler(skewedness=c, n_components=1000,
random_state=42)
X_trans = transform.fit_transform(X)
Y_trans = transform.transform(Y)
kernel_approx = np.dot(X_trans, Y_trans.T)
assert_array_almost_equal(kernel, kernel_approx, 1)
assert_true(np.isfinite(kernel).all(),
'NaNs found in the Gram matrix')
assert_true(np.isfinite(kernel_approx).all(),
'NaNs found in the approximate Gram matrix')
# test error is raised on when inputs contains values smaller than -c
Y_neg = Y.copy()
Y_neg[0, 0] = -c * 2.
assert_raises(ValueError, transform.transform, Y_neg)