1
我想通過一個稀疏的預先計算的克矩陣sklearn.svm.SVC.fit。下面是一些工作代碼:sklearn svm中的稀疏預計算Gram矩陣?
import numpy as np
from sklearn import svm
X = np.array([[0, 0], [1, 1]])
y = [0, 1]
clf = svm.SVC(kernel='precomputed')
gram = np.dot(X, X.T)
clf.fit(gram, y)
但是,如果我有:
from scipy.sparse import csr_matrix
sparse_gram = csr_matrix(gram)
clf.fit(sparse_gram, y)
我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 191, in fit
fit(X, y, sample_weight, solver_type, kernel)
File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 235, in _dense_fit
max_iter=self.max_iter)
TypeError: Argument 'X' has incorrect type (expected numpy.ndarray, got csr_matrix)
我在_dense_fit功能最終的事實(見的地方說行上面的235)讓我覺得我需要做一些特別的事情來說明如何使用稀疏矩陣。但我不知道該怎麼做。
更新:我剛剛檢查了代號爲擬合函數(https://sourcegraph.com/github.com/scikit-learn/scikit-learn/symbols/python/sklearn/svm/base/BaseLibSVM/fit),現在我更糊塗了:
self._sparse = sp.isspmatrix(X) and not self._pairwise
if self._sparse and self._pairwise:
raise ValueError("Sparse precomputed kernels are not supported. "
"Using sparse data and dense kernels is possible "
"by not using the ``sparse`` parameter")
所以我想,因爲它說,「疏影預計算的內核不支持」和這確實是我想要做的,所以我可能不幸運。 (這是一個錯誤,雖然我沒有真正看到這個錯誤?)