0% found this document useful (0 votes)
38 views17 pages

Minor Project by Ali (Intrainz)

Uploaded by

VITS21 05I4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views17 pages

Minor Project by Ali (Intrainz)

Uploaded by

VITS21 05I4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

A minor project on

K - means Clustering with different iterations

DONE BY

SHAIK SOUKAT ALI


K MEANS CLUSTERING
between B and 1 using our own function.
iris.new<- -ris[,c(l,2,’',3))
iris.class<- iris[,”5pecics"]
head(-r-s.new)

4i Sc al.Larry.|i /›epel.l'iidih Petal.Len:.|: Metal.'?Tidt[.


4i 1 5.. .,.? 1.4 0.2
fi 2 4.9 3.0 1.4 0.2
f# 3 4.7 3.2 1.3 0.2
#i 4 4.6 3.1 1.5 0.2
#i 5 5.0 3.6 1.4 0.2
#i 6 5.4 3.9 1.7 0.4

head (i r i s . c e s s ;

#i [1] setosa setosa setosa setosa setosa setosa


#i Levels: setosa versicolor virginica

normalize <- function(x)(


return ((x-min(x))/ (max(x7-min(x)))

ins.new??e)al.Lend-)i<- normalise(iris.frs'?Se:al.Ler:(;t)i)
iris.newS%epal.Width<- -ormalize(iris.rev Nepal.AiJth)
-r-s.newSFetal.Le.ng’.)i<- )oz-iiializu(uis.nc^.ñPctal.Lai.‹TLIi)
iris.newsPetal.Width<- nornoli e(iris.new$°e’al.Width)
head(iris.new,

## ePal.Lerij h Nepal.Width Pet-l.Leg-L Petal.Wi.dLk


ii 1 %.22222222 J.ñ2a01'30 J.1C7?%ñ6l U.Uñl6?*%?
fi 2 U.?ñbñ66h U.4i* b6i 0.UDP 9b')l U.Uñlhc6%
ii 3 0.11111111 C.tJUJU?U ?.Url°'.’-.5 U.Uñlh?*.%
ii 4 O.U."?J?33J C.4ñ6?3°?.0.°›'. '.h’5 U.Uñl5c°%
#i 5 0.1'444444 U.o6666c’.' U.Ub’.'796o U.U4I6c6b.'
fi 6 U.G3d53l5ñ U.’/Sl66c’.' 0.l18hJ407 U.l?iUU1UC

results- Kmeans( iis.new,d) --,::-'..’ -' ...:.

#i [1] 61 39 50

resultFr=nrers # o:res value of cluster center ds! :’po:.’:' value{: centers for <-.’

#i 5Cpel.LCng:h Sepal.Wid-h PcteI .Lend:b Petal.Wndtk


#i 1 0.%(12556 2.3G73770 O.ÜÜ5715'IE 3.?'1*l6H?*Ü
#i 2 ü. 0/26bC 0.4508547 0.79704476 0.62'/663?
#i 3 0.1961111 (J.ú9 JUJH H.U t:JH)?? 0.Ü5OE3i’3.?

result$cluster ^cives c7uster vector =.L. , 'g the o7uster wLere eaof record fa77s

5. Apply k-means clustering algorithm with k = 3


-- [38] 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
-- [75] 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 2 1 2 2 2 2
.. [11?]? ? 1 ? ? ? 1 ? 1 ? 1 ? ? 1 1 ? ? ? ? 1 1 ? ? ? 1 ? ? ? 1 ? 1 ?

9. Plot to see how Sepal.Length and Sepal .Width data po›nts have Deen d›stributed in clusters
1.0
0.8

O O
SepaI.Width

O O O OO O O
O O
0.4

O0 O OO O O O O O

. O O O OO O O
O .0

0.0 0.2 0.4 0.6 0.8 1.0

Sepal. Length

10. Plot to see how Sepal.Length and Sepal.Width data po›nts have been distributed originally as per ’ class” attr›bute ›n dataset
1.0
0.8

OO O
SepaI.Width

0.6

O 0 O O O OO O O
0 O O O O
O O0
0.4

Q O O O

OO O OO O O O O O
0.2

G O

0.0 0.2 0.4 0.6 0.8 1.0

Sepal.Length

O OO
1.0

OOOO O
OOOO O O
0.8

o o o
Petal.Width

0.6

O OOOOOO'O
OO O O O
0.4
0.2

0
O OOO O
O OOOOOO O

0.0 0.2 0.4 0.6 0.8 1.0

Petal.Length

12. Plot to see now Petal.Lengtn and Petal.Widfh data Ooints have been distributed originally as per “class” attribule in dataset
1.0

OOOO O
0.8

OO O OO O O O
Petal.Width

0.6

0 O OOO 0
O OOOOQO0
OO
0.4

O O O OO
0.2

0.0 0.2 0.4 0.6 O.8 1.0

red iltC(lii5tel’ <- at. f a l t o I ( I t ’al?, ñas?tr)

library(qQploL2,

fi harming: prckagt 'g:ploLz' was Lu l under R version 3.3.3

13. Install the package ggplot2 and import it and Plot the cluster results using ggplol
ggplot(irir.new, aes(Petal.fenj-h, Metal.Width, color = esult6cluster}) + gecm pair.K(,

plot(iris.ncw}c("Sepal.Length”, ”Scpal.Wiotn")], col=rosult?cluctcr)


1.00 — • ••

0.75 — •• • •

result8cluster
• 1
0/>0- • •••••••
• 2

0.25 —

0.00 - • ••

0.00 0.25 0.50 075 1.00


Petal.Length
1.0
0.8

O O

O O O O
SepaI.Width

0.6

D o Do o D O O0
O O O O O O O O O O O
0 0 0 0 0 0
oD
0.4

D O0 0 0 O O O 0 O O O0 O O 0 0
OO O0 0 0 O 0 O
0 0 0 0 0 0 0 0 O
D 0 0 O
O
0.2

0 0 0
0 O O
O O

0.0 0.2 0.4 0.6 0.8 1.0

Sepal.Length
ggplor (irir.rew, ate(Petal.Le.g-h, Petal.Win k, color = result c4ust )) * gecm_poir.t(:

14. Display the clustering results with all parameters

1.00 — • ••

0.75 — •• • •

result8cluster
• 1
0/>0- • •••••••
• 2

0.25 —

0.00 - • ••

0.00 0.25 0.50 075 1.00


Petal.Length
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0J 0.4 0.6 0.8 1.0

Sepal.Length ° ay, o

TO
0.0 O.2 0,4 0.8 0.8 1.0
Sepal.Width

k
0

O
Petal.Width

0
0,0 0.2 0.4 O.6 0.8 1.0

Petal.Length

taLlo(rosu-tSC-ustcr,iris.class) 5Psu2t of tab2P shows that C2uste f


.OispIay.the resorts-in taf›le , ,- ro

## iris.cIas*
fi setosa versicolor virginica
#i 1 0 47 14
#1 2 0 3 36
fi 3 50 0

library(animation)
kJ <-kmezns.zni(iris.new, ?)

Total number of correctly classified instances are: 36 + 47 50= 133 Total number of incorrectly classified instances are: 3 + 14= 17
Accuracy = 133/(133+17) = 0.88 i.e our model has achieved 88% accuracy! In order to Improve this accuracy further, we may try different
values of k

=————————=——————————————————=——————————————————=——————————————————=———————=== K means algoritnms with Animation


16. Disp|ay fhe K Means Algorithm with Animation and visualize the changes in the cluster center
SepaI.Width SepaI.Width

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

0
.

0.0
0

Sep
SeLpa

L
a

0
06

6
Le

Le
+ng

n
h

+
g
h
o >
4

4
Find cluster? Move centers!
0

0
SepaI.Width SepaI.Width

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

0
.

0.0
0

Sep
SeLpa

L
a

0 O OØ O
06

6
Le

Le
+ng

n
h

+
g
h
D O O
4

4
Find cluster? Move centers!
0

0
SepaI.Width SepaI.Width

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

0
.

0.0
0

Sep
SeLpa

L
a

0
06

6
Le

Le
+ng

n
h

+
g
h
D O O
4

4
Find cluster? Move centers!
0

0
1.0
0.8

Move centers!
SepaI.Width

0.6
0.4

O
0.2

0.0 0.2 0.4 0.6 0.8 1.0

Sepal. Length
1.0
0.8
SepaI.Width

0.6
0.4
0.2

0.0 0.2 0.4 0.6 0.8 1.0

Sepal. Length

17. Impart !actoextra package and visualize the cluster resuIt


library› ':’r rix -'

-- Welcome! Want to learn more° See two - r'-o: x- -related books at ’l


’§ sJaĘuao %ńPO ’? T T !1G2Ł\ - g/
'p = szaąuao O<au’siz;)suPawy -> pq
'g = szaąuao %<au’siz;) susan -> pp
' - szaąuao '^au’siz;) suPau -> ą

-0'

Jajsnjn
60t
Dim2

f9
joid ‹ajsnjg
k=2 k =3

cluster
cluster
M 1
1
2

2 0 2 2 0
Dim1 (739») Dim1 (73%)

k =4 k=5

cluster
cluster
1
1-
2

-2 0 2 -2 0 2
Dim1 (73%) Dim1 (73'f’e)

Conclusion

K means clustering algorithm with ihis dataset is executed and visualized using various r libraries like ggplot,animation,gridExtra,facfoextra.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy