-1

我正在使用 scikit learn 进行 Nystroem 近似。主要代码是:

feature_map_fourier = RBFSampler(gamma=0.5, random_state=1)
feature_map_nystroem = Nystroem(gamma=0.5, random_state=1)
fourier_approx_svm = pipeline.Pipeline([("feature_map", feature_map_fourier),
                                        ("svm", svm.LinearSVC(C=4))])
nystroem_approx_svm = pipeline.Pipeline([("feature_map", feature_map_nystroem),
                                        ("svm", svm.LinearSVC(C=4))])
# fit and predict using linear and kernel svm:
sample_sizes = np.arange(1,20)
print sample_sizes
fourier_scores = []
nystroem_scores = []
fourier_times = []
nystroem_times = []
for D in sample_sizes:
    avgtime = 0.0
    avgscore = 0.0
    avgftime = 0.0
    avgfscore = 0.0
    ns = []
    fs = []
    for i in range(0, 10):
    feature_map_fourier = RBFSampler(gamma=0.5, random_state=i) 
        feature_map_nystroem = Nystroem(gamma=0.5, random_state=i)
        fourier_approx_svm = pipeline.Pipeline([("feature_map", feature_map_fourier),
                                        ("svm", svm.LinearSVC(C=1))])
        nystroem_approx_svm = pipeline.Pipeline([("feature_map", feature_map_nystroem),("svm", svm.LinearSVC(C=1))])
    nystroem_approx_svm.set_params(feature_map__n_components=D)
        nystroem_approx_svm.fit(data_train, targets_train)
        fourier_approx_svm.set_params(feature_map__n_components=D)
        fourier_approx_svm.fit(data_train, targets_train)
        start = time()
        fourier_score = fourier_approx_svm.score(data_test, targets_test)
        t = time() - start

        avgftime += t
        avgfscore += fourier_score     
        start = time()
        nystroem_score = nystroem_approx_svm.score(data_test, targets_test)
        t = time() - start
        avgtime +=  t
        avgscore += nystroem_score
        ns.append(avgscore)
        fs.append(avgfscore)
    print 'Nstrrom '+str(np.std(ns))
    print 'fs '+str(np.std(ns))    
    nystroem_times.append(avgtime/10.0)
    nystroem_scores.append(avgscore/10.0)
    fourier_times.append(avgftime/10.0)
    fourier_scores.append(avgfscore/10.0)

尝试运行此代码时出现以下错误。

C:\Users\t-sujain\Documents\LDKL BaseLine\Nystreom>forestNormalized_kernel_appro
x.py
522910
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
Traceback (most recent call last):
  File "C:\Users\t-sujain\Documents\LDKL BaseLine\Nystreom\forestNormalized_kern
el_approx.py", line 70, in <module>
    nystroem_approx_svm.fit(data_train, targets_train)
  File "F:\Python27\lib\site-packages\sklearn\pipeline.py", line 126, in fit
    Xt, fit_params = self._pre_transform(X, y, **fit_params)
  File "F:\Python27\lib\site-packages\sklearn\pipeline.py", line 116, in _pre_tr
ansform
    Xt = transform.fit_transform(Xt, y, **fit_params_steps[name])
  File "F:\Python27\lib\site-packages\sklearn\base.py", line 364, in fit_transfo
rm
    return self.fit(X, y, **fit_params).transform(X)
  File "F:\Python27\lib\site-packages\sklearn\kernel_approximation.py", line 470
, in transform
    gamma=self.gamma)
  File "F:\Python27\lib\site-packages\sklearn\metrics\pairwise.py", line 808, in
 pairwise_kernels
    return func(X, Y, **kwds)
  File "F:\Python27\lib\site-packages\sklearn\metrics\pairwise.py", line 345, in
 rbf_kernel
    K = euclidean_distances(X, Y, squared=True)
  File "F:\Python27\lib\site-packages\sklearn\metrics\pairwise.py", line 148, in
 euclidean_distances
    XX = X.multiply(X).sum(axis=1)
  File "F:\Python27\lib\site-packages\scipy\sparse\compressed.py", line 251, in
multiply
    return self._binopt(other,'_elmul_')
  File "F:\Python27\lib\site-packages\scipy\sparse\compressed.py", line 676, in
_binopt
    data    = np.empty(maxnnz, dtype=upcast(self.dtype,other.dtype))
MemoryError

我正在使用 cygbin 和具有 100GB RAM 的系统,因此系统不可能内存不足。有人可以帮我吗?

4

1 回答 1

2

根据评论中的讨论:此崩溃是由于在传递稀疏数据作为输入时在变换方法中发生的二次过度分配造成的。在 0.14.1 版本之后,它已在 master 分支中修复。

另请注意:在高维稀疏输入上使用 RBF 内核可能不是很有用。通常稀疏矩阵表示用于稀疏的高维数据,例如文本文档的词袋特征。对于此类数据,线性内核通常与非线性内核一样好或更好,因此该Nystroem方法在这种情况下可能无用。

于 2013-10-08T08:06:02.913 回答