Skip to content

Commit 850a805

Browse files
committed
Updated tsne.py
1 parent 70ba4cd commit 850a805

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

machine_learning/tsne.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
t-SNE is a nonlinear dimensionality reduction algorithm for visualizing
66
high-dimensional data in a low-dimensional space (2D or 3D).
77

8-
It computes pairwise similarities in both spaces and minimizes the
8+
It computes pairwise similarities in both spaces and minimizes the
99
Kullback-Leibler divergence using gradient descent.
1010

1111
References:
@@ -149,35 +149,33 @@ def apply_tsne(
149149
return y
150150

151151

152-
def main() -> None:
152+
def main() -> ndarray:
153153
"""
154-
Run t-SNE on Iris dataset and display the first 5 embeddings.
154+
Run t-SNE on Iris dataset and return the embeddings.
155+
156+
Returns:
157+
ndarray: t-SNE embedding of the Iris dataset
155158

156159
Example:
157-
>>> main() # doctest: +ELLIPSIS
158-
t-SNE embedding (first 5 points):
159-
[[-...
160+
>>> result = main()
161+
>>> result.shape
162+
(150, 2)
163+
>>> isinstance(result, np.ndarray)
164+
True
160165
"""
161166
data_x, _ = collect_dataset()
162167
y_emb = apply_tsne(data_x, n_components=2, n_iter=300)
163168

164169
if not isinstance(y_emb, np.ndarray):
165170
raise TypeError("t-SNE embedding must be an ndarray")
166171

167-
print("t-SNE embedding (first 5 points):")
168-
print(y_emb[:5])
169-
170-
# Optional visualization (commented, Ruff/mypy compliant)
171-
# import matplotlib.pyplot as plt
172-
# plt.scatter(
173-
# y_emb[:, 0],
174-
# y_emb[:, 1],
175-
# c=_labels,
176-
# cmap="viridis"
177-
# )
178-
# plt.show()
172+
return y_emb
179173

180174

181175
if __name__ == "__main__":
182176
doctest.testmod()
183-
main()
177+
178+
# Demonstration of the algorithm
179+
result = main()
180+
print("t-SNE embedding (first 5 points):")
181+
print(result[:5])

0 commit comments

Comments
 (0)