scikit-learn / scikit-learn/scikit-learn
kernel_approximation.Nystroem with precomputed kernel
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 67.3k
- Forks
- 27.4k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 58
Description
Describe the bug
I am trying to get a Nystroem approximation of a pre computed kernel but it throws an error if I use n_components anything less than the number of datapoints. Unless my understanding is wrong, does this not defeat the point of the approximation? Please advise, code below:
I have come from this resolved issue https://github.com/scikit-learn/scikit-learn/issues/14641
Steps/Code to Reproduce
from sklearn.svm import SVC
from sklearn.kernel_approximation import Nystroem
# data shape (3000,50)
# kernel matrix shape (3000,3000)
clf = SVC()
feature_map_nystroem = Nystroem(
kernel = 'precomputed',
random_state=1,
n_components=300
)
kernel_transformed = feature_map_nystroem.fit_transform(kernel)
clf.fit(kernel_transformed, y)
Expected Results
I expect this to work.
Actual Results
Instead it gives error:
in check_pairwise_arrays(X, Y, precomputed, dtype, accept_sparse, force_all_finite, copy)
153 if precomputed:
154 if X.shape[1] != Y.shape[0]:
--> 155 raise ValueError("Precomputed metric requires shape "
156 "(n_queries, n_indexed). Got (%d, %d) "
157 "for %d indexed." %
158 (X.shape[0], X.shape[1], Y.shape[0]))
159 elif X.shape[1] != Y.shape[1]:
160 raise ValueError("Incompatible dimension for X and Y matrices: "
161 "X.shape[1] == %d while Y.shape[1] == %d" % (
162 X.shape[1], Y.shape[1]))
ValueError: Precomputed metric requires shape (n_queries, n_indexed). Got (300, 3000) for 300 indexed.
Versions
System:
python: 3.8.19 (default, Mar 20 2024, 19:58:24) [GCC 11.2.0]
executable: /opt/conda/miniconda3/envs/python3.8/bin/python
machine: Linux-6.1.0-21-cloud-amd64-x86_64-with-glibc2.17
Python dependencies:
pip: 24.0
setuptools: 63.1.0
sklearn: 0.24.2
numpy: 1.21.6
scipy: 1.10.1
Cython: 3.0.10
pandas: 1.2.5
matplotlib: 3.4.3
joblib: 1.4.2
threadpoolctl: 3.5.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with sklearn.kernel_approximation.Nystroem and trace its precomputed-kernel path into check_pairwise_arrays. Reproduce the provided 3000-by-3000 kernel example with n_components=300, then verify that the precomputed case supports the expected approximation shape without the shown validation error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100