當前位置: 首頁>>代碼示例>>Python>>正文


Python kernel_approximation.RBFSampler方法代碼示例

本文整理匯總了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 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:19,代碼來源:test_kernel_approximation.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:40,代碼來源:test_kernel_approximation.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Mastering-Elasticsearch-7.0,代碼行數:12,代碼來源:test_kernel_approximation.py

示例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) 
開發者ID:IBM,項目名稱:lale,代碼行數:8,代碼來源:rbf_sampler.py

示例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) 
開發者ID:pandas-ml,項目名稱:pandas-ml,代碼行數:10,代碼來源:test_kernel_approximation.py

示例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) 
開發者ID:alvarobartt,項目名稱:twitter-stock-recommendation,代碼行數:40,代碼來源:test_kernel_approximation.py


注:本文中的sklearn.kernel_approximation.RBFSampler方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。