Skip to content

(feat): add n_components parameter to tSNE #2803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/2803.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `n_components` parameter to {func}`~scanpy.tl.tsne` {smaller}`Kitsune`
5 changes: 5 additions & 0 deletions src/scanpy/tools/_tsne.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def tsne( # noqa: PLR0913
adata: AnnData,
n_pcs: int | None = None,
*,
n_components: int = 2,
use_rep: str | None = None,
perplexity: float = 30,
metric: str = "euclidean",
Expand Down Expand Up @@ -60,6 +61,8 @@ def tsne( # noqa: PLR0913
Annotated data matrix.
{doc_n_pcs}
{use_rep}
n_components
The number of dimensions of the embedding.
perplexity
The perplexity is related to the number of nearest neighbors that
is used in other manifold learning algorithms. Larger datasets
Expand Down Expand Up @@ -124,6 +127,7 @@ def tsne( # noqa: PLR0913
learning_rate=learning_rate,
n_jobs=n_jobs,
metric=metric,
n_components=n_components,
)
if metric != "euclidean" and (Version(sklearn.__version__) < Version("1.3.0rc1")):
params_sklearn["square_distances"] = True
Expand Down Expand Up @@ -179,6 +183,7 @@ def tsne( # noqa: PLR0913
n_jobs=n_jobs,
metric=metric,
use_rep=use_rep,
n_components=n_components,
)
key_uns, key_obsm = ("tsne", "X_tsne") if key_added is None else [key_added] * 2
adata.obsm[key_obsm] = X_tsne # annotate samples with tSNE coordinates
Expand Down
Loading