{"record":{"id":"b5ac8127dac456d1","repo":"TheAlgorithms/Python","slug":"n-components-and-n-iter-must-be-1","errorCode":null,"errorMessage":"n_components and n_iter must be >= 1","messagePattern":"n_components and n_iter must be >= 1","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/t_stochastic_neighbour_embedding.py","lineNumber":114,"sourceCode":"    \"\"\"\n    Apply t-SNE for dimensionality reduction.\n\n    Args:\n        data_matrix: Original dataset (features).\n        n_components: Target dimension (2D or 3D).\n        learning_rate: Step size for gradient descent.\n        n_iter: Number of iterations.\n\n    Returns:\n        ndarray: Low-dimensional embedding of the data.\n\n    >>> features, _ = collect_dataset()\n    >>> embedding = apply_tsne(features, n_components=2, n_iter=50)\n    >>> embedding.shape\n    (150, 2)\n    \"\"\"\n    if n_components < 1 or n_iter < 1:\n        raise ValueError(\"n_components and n_iter must be >= 1\")\n\n    n_samples = data_matrix.shape[0]\n    rng = np.random.default_rng()\n    embedding = rng.standard_normal((n_samples, n_components)) * 1e-4\n\n    high_dim_affinities = compute_pairwise_affinities(data_matrix)\n    high_dim_affinities = np.maximum(high_dim_affinities, 1e-12)\n\n    embedding_increment = np.zeros_like(embedding)\n    momentum = 0.5\n\n    for iteration in range(n_iter):\n        low_dim_affinities, numerator_matrix = compute_low_dim_affinities(embedding)\n        low_dim_affinities = np.maximum(low_dim_affinities, 1e-12)\n\n        affinity_diff = high_dim_affinities - low_dim_affinities\n\n        gradient = 4 * (","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/t_stochastic_neighbour_embedding.py#L96-L132","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nembedding = apply_tsne(features, n_components=0, n_iter=50)\n\n# after\nembedding = apply_tsne(features, n_components=2, n_iter=50)","handlingStrategy":"validation","validationCode":"n_components = max(1, int(n_components))\nn_iter = max(1, int(n_iter))\nembedding = apply_tsne(data_matrix, n_components=n_components, n_iter=n_iter)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bound config sweeps so n_components/n_iter start at 1.","When computing n_components from column selections, assert the result >= 1."],"tags":["machine-learning","dimensionality-reduction","tsne","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}