Show Menu
Cheatography

Python - K-Means_Clustering Cheat Sheet (DRAFT) by

K-means clustering algorithm

This is a draft cheat sheet. It is a work in progress and is not finished yet.

TO START

# IMPORT VIS LIBRARIES
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

# IMPORT MODELLING LIBRARIES
from sklearn.cluster import KMeans

PRELIM­INARY OPERATIONS

df = pd.rea­d_c­sv(­'da­ta.c­sv')
import data
df.head()
check head df
df.info()
check info df
df.des­cribe()
check stats df
 

TRAIN MODEL

df.dro­p('­col­',a­xis=1)
take everything but 1 col
FIT THE MODEL
kmeans = Kmeans­(n_­clu­ste­rs=n)
instatiate model
kmeans.fi­t(data)
train/fit the model
kmeans.cl­ust­er_­cen­ters_
find centers
kmeans.la­bels_
find labels