A t-SNE map of the stock market
t-SNE provides great visualizations when the individual samples can be labeled. In this exercise, you'll apply t-SNE to the company stock price data. A scatter plot of the resulting t-SNE features, labeled by the company names, gives you a map of the stock market! The stock price movements for each company are available as the array normalized_movements
(these have already been normalized for you). The list companies
gives the name of each company. PyPlot (plt
) has been imported for you.
This exercise is part of the course
Unsupervised Learning in Python
Exercise instructions
- Import
TSNE
fromsklearn.manifold
. - Create a TSNE instance called
model
withlearning_rate=50
. - Apply the
.fit_transform()
method ofmodel
tonormalized_movements
. Assign the result totsne_features
. - Select column
0
and column1
oftsne_features
. - Make a scatter plot of the t-SNE features
xs
andys
. Specify the additional keyword argumentalpha=0.5
. - Code to label each point with its company name has been written for you using
plt.annotate()
, so just hit submit to see the visualization!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import TSNE
____
# Create a TSNE instance: model
model = ____
# Apply fit_transform to normalized_movements: tsne_features
tsne_features = ____
# Select the 0th feature: xs
xs = ____
# Select the 1th feature: ys
ys = tsne_features[:,1]
# Scatter plot
____
# Annotate the points
for x, y, company in zip(xs, ys, companies):
plt.annotate(company, (x, y), fontsize=5, alpha=0.75)
plt.show()