TheAlgorithms/Python · error · ValueError
n_components and n_iter must be >= 1
Error message
n_components and n_iter must be >= 1
What it means
Raised by apply_tsne in t_stochastic_neighbour_embedding.py when n_components or n_iter is less than 1. The t-SNE implementation initializes a random embedding of shape (n_samples, n_components) and iterates n_iter times, so zero or negative values for either parameter make the gradient-descent loop meaningless and are rejected up front.
Source
Thrown at machine_learning/t_stochastic_neighbour_embedding.py:114
"""
Apply t-SNE for dimensionality reduction.
Args:
data_matrix: Original dataset (features).
n_components: Target dimension (2D or 3D).
learning_rate: Step size for gradient descent.
n_iter: Number of iterations.
Returns:
ndarray: Low-dimensional embedding of the data.
>>> features, _ = collect_dataset()
>>> embedding = apply_tsne(features, n_components=2, n_iter=50)
>>> embedding.shape
(150, 2)
"""
if n_components < 1 or n_iter < 1:
raise ValueError("n_components and n_iter must be >= 1")
n_samples = data_matrix.shape[0]
rng = np.random.default_rng()
embedding = rng.standard_normal((n_samples, n_components)) * 1e-4
high_dim_affinities = compute_pairwise_affinities(data_matrix)
high_dim_affinities = np.maximum(high_dim_affinities, 1e-12)
embedding_increment = np.zeros_like(embedding)
momentum = 0.5
for iteration in range(n_iter):
low_dim_affinities, numerator_matrix = compute_low_dim_affinities(embedding)
low_dim_affinities = np.maximum(low_dim_affinities, 1e-12)
affinity_diff = high_dim_affinities - low_dim_affinities
gradient = 4 * (View on GitHub (pinned to f5988cc097)
Solutions
- Pass n_components >= 1 (2 or 3 are the usual choices for visualization).
- Pass n_iter >= 1; typical values are 250-1000 for this implementation.
- Guard computed parameter values before the call: max(1, requested) or explicit validation.
Example fix
# before embedding = apply_tsne(features, n_components=0, n_iter=50) # after embedding = apply_tsne(features, n_components=2, n_iter=50)
Defensive patterns
Strategy: validation
Validate before calling
n_components = max(1, int(n_components)) n_iter = max(1, int(n_iter)) embedding = apply_tsne(data_matrix, n_components=n_components, n_iter=n_iter)
Prevention
- Bound config sweeps so n_components/n_iter start at 1.
- When computing n_components from column selections, assert the result >= 1.
When it happens
Trigger: Calling apply_tsne(data_matrix, n_components=0) or with n_iter=0 (or negative values for either), often from a config where an unset parameter defaults to 0.
Common situations: CLI/config-driven dimensionality reduction where n_components is computed as len(selected_columns)-something and underflows to 0, or a hyperparameter sweep boundary that includes 0.
Related errors
- x and y have different lengths
- Input arrays must have the same length.
- Test samples' feature length does not equal to that of train
- Expected a_coeffs to have {self.order + 1} elements for {sel
- Expected b_coeffs to have {self.order + 1} elements for {sel
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/b5ac8127dac456d1.
Report an issue: GitHub.