Rlab SS
Rlab SS
Program:
install.package(“arules”)
library(arules)
install.packages(“arulerViz”)
library(arulesViz)
data(“Groceries”)
rules<-apriori(Groceries,parameter=list(supp=0.01,conf=0.2))
inspect(riles[1:10])
Output:
one [0.00s].
> Apriori
Parameter specification:
confidence minval smax arem aval originalSupport maxtime support minlen maxlen
target ext
rules TRUE
Algorithmic control:
> inspect(rules[1:10])
lift count
[2] 1.607682 99
>
2.Implement K-means clustering technique
Program:
data(iris)
str(iris)
install.packages("ClusterR")
install.packages("cluster")
library(ClusterR)
library(cluster)
set.seed(240)
kmeans.re
kmeans.re$cluster
cm
plot(iris_1[c("Sepal.Length","Sepal.Width")])
plot(iris_1[c("Sepal.Length","Sepal.Width")],
col = kmeans.re$cluster)
plot(iris_1[c("Sepal.Length","Sepal.Width")],
col = kmeans.re$cluster,
kmeans.re$centers
kmeans.re$centers[, c("Sepal.Length","Sepal.Width")]
clusplot(iris_1[, c("Sepal.Length","Sepal.Width")],
y_kmeans,
lines = 0,
shade = TRUE,
color = TRUE,
labels = 2,
plotchar = FALSE,
span = TRUE,
xlab ='Sepal.Length',
ylab ='Sepal.Width')
Output:
> library(ClusterR)
> library(cluster)
> set.seed(240)
> kmeans.re
Cluster means:
Clustering vector:
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[40] 1 1 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3
[79] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3 3 3 3 3 3 2 2 3 3
[118] 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3 3 3 2 3 3 2
Available components:
[1] "cluster" "centers" "totss" "withinss" "tot.withinss"
> kmeans.re$cluster
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[40] 1 1 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3
[79] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3 3 3 3 3 3 2 2 3 3
[118] 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3 3 3 2 3 3 2
> cm
1 2 3
setosa 50 0 0
versicolor 0 48 2
virginica 0 14 36
> plot(iris_1[c("Sepal.Length","Sepal.Width")])
> plot(iris_1[c("Sepal.Length","Sepal.Width")],
+ col = kmeans.re$cluster)
> plot(iris_1[c("Sepal.Length","Sepal.Width")],
+ col = kmeans.re$cluster,
> kmeans.re$centers
Sepal.Length Sepal.Width
1 5.006000 3.428000
2 5.901613 2.748387
3 6.850000 3.073684
> y_kmeans<-kmeans.re$cluster
+ y_kmeans,
+ lines = 0,
+ shade = TRUE,
+ color = TRUE,
+ labels = 2,
+ plotchar = FALSE,
+ span = TRUE,
+ xlab ='Sepal.Length',
+ ylab ='Sepal.Width')
>
3.Implement anyone Hierarchal Clustering
Program:
distance_mat<- dist(mtcars, method = 'euclidean')
distance_mat
set.seed(240)
Hierar_cl
plot(Hierar_cl)
fit<- cutree(Hierar_cl, k = 3 )
fit
table(fit)
Datsun 710
Hornet 4 Drive
Hornet Sportabout
Valiant 152.1241352
Merc 230 Merc 280 Merc 280C Merc 450SE Merc 450SL
Datsun 710
Hornet 4 Drive
Hornet Sportabout
Valiant
Duster 360
Merc 240D
Merc 230
Toyota
Datsun 710
Hornet 4 Drive
Hornet Sportabout
Valiant
Duster 360
Merc 240D
Merc 230
Merc 280
Merc 280C
Merc 450SE
Merc 450SL
Merc 450SLC
Datsun 710
Hornet 4 Drive
Hornet Sportabout
Valiant
Duster 360
Merc 240D
Merc 230
Merc 280
Merc 280C
Merc 450SE
Merc 450SL
Merc 450SLC
Cadillac Fleetwood
Lincoln Continental
Chrysler Imperial
Datsun 710
Hornet 4 Drive
Hornet Sportabout
Valiant
Duster 360
Merc 240D
Merc 230
Merc 280
Merc 280C
Merc 450SE
Merc 450SL
Merc 450SLC
Cadillac Fleetwood
Lincoln Continental
Chrysler Imperial
Fiat 128
Honda Civic
Toyota Corolla
Toyota Corona
Volvo 142E
Datsun 710
Hornet 4 Drive
Hornet Sportabout
Valiant
Duster 360
Merc 240D
Merc 230
Merc 280
Merc 280C
Merc 450SE
Merc 450SL
Merc 450SLC
Cadillac Fleetwood
Lincoln Continental
Chrysler Imperial
Fiat 128
Honda Civic
Toyota Corolla
Toyota Corona
Dodge Challenger
AMC Javelin
Camaro Z28
Pontiac Firebird
Datsun 710
Hornet 4 Drive
Hornet Sportabout
Valiant
Duster 360
Merc 240D
Merc 230
Merc 280
Merc 280C
Merc 450SE
Merc 450SL
Merc 450SLC
Cadillac Fleetwood
Lincoln Continental
Chrysler Imperial
Fiat 128
Honda Civic
Toyota Corolla
Toyota Corona
Dodge Challenger
AMC Javelin
Camaro Z28
Pontiac Firebird
Fiat X1-9
Porsche 914-2
Lotus Europa
Ford Pantera L
> set.seed(240)
> Hierar_cl
Call:
Distance : euclidean
Number of objects: 32
> plot(Hierar_cl)
> abline(h = 110, col = "green")
> fit
1 1 1 2
2 2 2 1
1 1 1 2
2 2 2 2
2 1 1 1
1 2 2 2
2 1 1 1
2 1 3 1
> table(fit)
fit
1 2 3
16 15 1
libary(rpart)
install.packages("rpart.plot")
library(rpart.plot)
data(iris)
head(iris)
set.seed(123)
train_indices<-sample(1:nrow(iris),0.7*nrow(iris))
train_data<-iris[train_indices,]
train_data<-iris[-train_indices,]
model<-rpart(Species~.,data=train_data)
#plot(model)
rpart.plot(model)
Output:
data(iris)
> head(iris)
> set.seed(123)
> train_indices<-sample(1:nrow(iris),0.7*nrow(iris))
> train_data<-iris[train_indices,]
> train_data<-iris[-train_indices,]
> model<-rpart(Species~.,data=train_data)
> #plot(model)
> rpart.plot(model)
>