We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
1010912024, 08:16
4 tnport necessary Libraries
Anport natplotlib.pyplot as pit
frm skiearn.cluster inport Kans
Innport Necessary Libraries Inthe fist code cell import the necessary libraries like numpy, pandas, matplatb, and sciktlearn forthe kMeans
algorithm.
keMeansipynb ~ Colab
‘ep 2: Create a Dataset For demonstration, lets create a simple 20 dataset using numpy.
4 creating 2 sample 20 cataset with 2 clusters
np. randon.seec(@)
Xe npovstack((np.randon.cornal (loe=[2, 2], scale
np.randon.nornal oe+(8, 8], 531
af = pacoatarrane(Xy cotumase['X°, °Y"])
1 visualize the asta
pit.scatter(af{'x"], dV], c='blue',
piestitle('Sanple 20 bate!)
pit xlabelx")
pitiylabelcy")
Bitsshont)
a
Sample 2D Data
‘Step 4: Apply the keMeans Algorithm Now, apply the k-Means clustering algorithm using scik-earn We wil set k=2 since we want two clusters
Y canerte 01am qumbers using nl
# poply keteans clustering
keans’ = oheans(n_elusterse2)
leans. F480)
1 Get the cluster centers (centroies)
prine(Centrotds:", centroids)
1 Get the cluster Labels (to which cluster each point belongs)
Aabels = areons labels,
Drine("Lavels:", labels)
centroids: [{7.99963924 6.68137373]
(2-bowao3oe 2951408831]
ntpsifolab research google. comidrve/1WaStGINcOULatig6X_ZZIXK20QwcKarR-#sorolTo=JFP40EOxOpAnkpriniMode=true
5, sizen(50, 209.
5) sizee(58, 2009)
a
Chose
181010912024, 08:16 keMeansipynt - Colao
20eccecccseececseses0ecss)
1/4er/oeai/ Lib /python}.10/cist-packages/skleatn/clusver/_kreans.py:2416: Futureaarning: The defSult value of “r_inst” will change érom 3
Stper(). creck parano_vs_input(h, derault a tnita20)
‘Stop 5: Visualize the Clustering Results Next, visualize the clusters along with the centroids
een(ef"X"], dF['Y'], e=labels, $258, cxape"vieidis")
feentrotds[, 8), centrotés(, 1,
pisvtitle(k-neans Clustering with ke2")
oclabel(X")
peeylebelW)
pit. shont)
ea, 9200, alghan8.75, narker='x')
k-Means Clustering with k=2
‘Step 6: Evaluate the Model (Optional) You can evaluate how wel the clusters are formed using metrics like Inertia (witin-luster sum of
‘squares), Lower inertia means better clustering
python
‘calculate inertia (aun of squared distances to the nearest cluster center)
print("Thertio:', keeans- inertia.)
‘Step 7: Experiment with Different k Values You can also experiment with different values of kto find the optimal numberof clusters using the
Elbow Method,
anertias = (] °
keyalues = range(a, 19)
srcons » Weans(o_clusters-k)
ireans- #5800)
Aner #1as.append(kneans. sere)
he snerta vs number of clusters (Eabow Method)
(values, inertias, "bo-')
‘ithe(Eltow Method for optinalk")
label('Wamber oF clusters (k)')
spabel{"inertia")
ntpsifolab research google. comidrve/1WaStGINcOULatig6X_ZZIXX20QwcKarR-#sorolTo=JFP40EOxOpAnkpriniMode=true 281010912024, 08:16
pie
show)
ase/loeat//pythont.30/stst-packages/sklearn/cluster/_kewans.py 2816:
Stper(). check parang_9s_tnput(x, deraulta_tnites8)
asr/loeaifto/pyenon}-i0/dist-packages/sklearn/cluster/_keeans.py:2816
Stper(). check 2arans_vs_input (x, default intt-20)
asr/locai7i/pyenors. /dist-packages/sklean/cluster/_kewans.oy:2416
‘Stper(), check 2arans_vs_input (x, geeaultn_intt=20)
sasr/locai?i/pyenon3. 7 ist-packages/skleaen/cluster/_keeans.py:2416
Super), check parans_vs_input (x, geeaultn_intt=i8)
Jase/loeai7it/pyenon)-io/dtst-packages/sklearn/cluseer/_keeans.py:2816:
Stper(). check parans_vs_input(x, derault_n_init-28)
ase laeai?to/python}i0/dist-packages/skleann/cluster/_keeans.py:2816
Stper(). check 2arans_vs_tnput(x, default intt-s0)
sasr/locai7iby/pytnon3. a7 dist-packages/skleaen/cluster/_keeans.py:2416
Super), check parans_vs_input Gk, geeault_n_intt=20)
Jase /locai7b/pythons. i/dist-packages/skl ean cluster/_kneans.py:2416
Stper(). creck parano_9s_ input (x, derault_tnit-28)
asr/loeai?to/pyenon}-10/3st-packages/sklearn/cluster/_keeans.py:2416
Stper(). creck parang_vsinput(x, gerault a initai8)
Elbow Method for Optimal k
keMeansipynb - Colab
Fatureaening:
Faturetoening:
Fatureaoning:
750
1500
2000
inertia
Number of clusters)
Smport pandas 28 ps
tram skiearn.cluster import Weans
1 step 1: coente dataset
np. randon.seea(@)
np.vstack((np.randon.rornal(loe~(2, 2], scaler
rnp.randon.nornal Qoe+(8, 8], sealent
5, sizen(50, 209.
1 sizes(58, 2003)
GF = pd.oataFrane(X, cotuanselX°, °YJ)
pe
pie
pie
pie
pie
seatten(afi'®'), a['Y"], blue’,
{ithe( ‘Sonple 29 Date")
abel XD
ylavel('¥")
shew)
4 step 3: Apply keeans
leneans = xheana(nelusters-2)
keane F008)
4 step 4: visualize the clusters
pis
pe
ne
pe
pe
ntpssioolab es
Scatter(af('X'], df['Y"], crlabels, 5°58, crage'viriais")
scatter(centroids{:, @]y centroids{:, 1], ce'red", 59208, alpharB.75, markers
ste leeane Clustering with k=2")
saabel(*X")
sabel(-¥")
Show)
the default value of inst
The default value of “init
The defsutt value of “inst
The default value of “init
the dofoult value of “inst
rch google. comdrive!I WaSGINCOULatig6X_ZZIX2QQwcKark-scrollTo=JFP4oEOxOpAngpriniMode=ttue
wan
wan
wan
change
charge
charge
351010912024, 08:16 keMeansipynt - Colao
4 step 5: bow Rethod (Options)
inertias = (1
values = ronge(a, 32)
sveans = Kieans(_clusters-k)
rears. #3200)
sner2ias,append(eneans. inertia)
plot(e.valves, inertias, “bo-")
‘ste eibow Hethod for Optimal")
rasbelCMamer of clusters (K)")
sjlabel(“irer
how)
ntpsifolab research google. comidrve/1WaStGINcOULatig6X_ZZIXX20QwcKarR-#sorolTo=JFP40EOxOpAnkprintMode=true1010912024, 08:16
Sample 2D Data
uper()-—check:parane_we Sopot, cea)
k-Means Clustering with k:
tnse-30)
keMeansipynb - Colab
2
asn/loeai/4o/python3.10/ctst-packages/sklearn/ cl
‘super() «check parama_vs_input(hy defautten, init
eo/_kewans.y:3416: Futureoontn:
)
ee/_keans.9y 4416: Futureantng:
he default value of“ intt” will change fo)
ntpsifolab research google. comidrve/1WaStGINcOULatig6X_ZZIXX20QwcKarR-#sorolTo=JFP40EOxOpAnkprintMode=true
ange #0
55