FAQ: K-Means Clustering - Try It On Your Own

This community-built FAQ covers the “Try It On Your Own” exercise from the lesson “K-Means Clustering”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

Machine Learning

FAQs on the exercise Try It On Your Own

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I imported a dataset from UCI but when I executed print() with the dataframe it was displayed in the console but not in the browser on the right. How do we make it get displayed in the browser?

you need to place the ff line on top of the code editor: import codecademylib3_seaborn

2 Likes

Thank you! My pyplot wasn’t working as well and this solved the issue.

Well since no one has shared any examples on the Try It On Your Own of the K-Means Clustering lesson.
Here is what I’ve done.

Any feedback will be highly appreciated.
Regards!

Hello everyone!
Thats my solution.
Any feedback will be appreciated.
Thanks!

from sklearn.preprocessing import scale
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
import pandas as pd
from sklearn.cluster import KMeans

digits_tra = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tra", header=None)
digits_tes = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/optdigits/optdigits.tes", header=None)
#print(digits_tra.info)
#print(digits_tes.info)
digits_tra = scale(digits_tra)
digits_tes = scale(digits_tes)
#print(digits_tra)
num_clusters = list(range(1,100,10))
inertias = []
for k in num_clusters:
  model = KMeans(n_clusters=k)
  model.fit(digits_tra)
  inertias.append(model.inertia_)
#print(inertias)
plt.plot(num_clusters, inertias, '-o')
plt.xlabel('Number of Clusters (k)')
plt.ylabel('Inertia')
plt.show()