0% found this document useful (0 votes)
15 views

DM lab

The document certifies the completion of practical work in a Computer Laboratory for the academic year 2023-2024. It includes various programming tasks such as implementing the Apriori algorithm, K-means and hierarchical clustering, decision tree classification, linear regression, and data visualization using R. Each task is accompanied by sample code and outputs demonstrating the respective techniques.

Uploaded by

anandsb8302
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)
15 views

DM lab

The document certifies the completion of practical work in a Computer Laboratory for the academic year 2023-2024. It includes various programming tasks such as implementing the Apriori algorithm, K-means and hierarchical clustering, decision tree classification, linear regression, and data visualization using R. Each task is accompanied by sample code and outputs demonstrating the respective techniques.

Uploaded by

anandsb8302
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/ 18

This is to certify that the bonafide record of the work done in the

Computer Laboratory during academic year 2023 – 2024.

Name :

Class :

Register No. :

Roll No. :

Lab :

For the practical examination held on ________________

Staff In-Charge Examiners:

1.

Head of the Department 2.


Name :

Register No.:

Roll No. :
Page
S.No Date Title Staff sign
no

10

Staff Signature:
1)Apriori Algorithm

# Loading Libraries

library(arules)

library(arulesViz)

library(RColorBrewer)

# import dataset

data("Groceries")

# using apriori() function

rules <- apriori(Groceries,

parameter = list(supp = 0.01, conf = 0.2))

# using inspect() function

inspect(rules[1:10])

# using itemFrequencyPlot() function

arules::itemFrequencyPlot(Groceries, topN = 20,

col = brewer.pal(8, 'Pastel2'),

main = 'Relative Item Frequency Plot',

type = "relative",

ylab = "Item Frequency (Relative)")


Output:
2) implement K-means clustering techniques

# Generate a sample dataset

set.seed(123)

data <- data.frame(

X1 = rnorm(100, mean = 10, sd = 2),

X2 = rnorm(100, mean = 20, sd = 3)

# Implement K-means clustering

k <- 3 # Number of clusters

kmeans_result <- kmeans(data, centers = k, nstart = 20)

# Print the cluster centers

print(kmeans_result$centers)

# Print the cluster assignments for each data point

print(kmeans_result$cluster)

# Visualize the original dataset with cluster assignments

plot(data, col = kmeans_result$cluster, main = "K-means Clustering")

points(kmeans_result$centers, col = 1:k, pch = 8, cex = 2)


Output:
3)Hieararchical clustering

# Generate a sample dataset

set.seed(123)

data <- matrix(rnorm(100), ncol = 2)

# Implement hierarchical clustering

hierarchical_result <- hclust(dist(data))

# Cut the dendrogram to get clusters

num_clusters <- 3

clusters <- cutree(hierarchical_result, k = num_clusters)

# Visualize the hierarchical clustering dendrogram

plot(hierarchical_result, main = "Hierarchical Clustering Dendrogram")

# Visualize the clustered data

plot(data, col = clusters, pch = 19, main = "Hierarchical Clustering Results")


Output:
4)implementing classification algorithm

# Load necessary libraries

library(rpart)

library(rpart.plot)

# Load the Iris dataset

data(iris)

# Split the dataset into training and testing sets

set.seed(123)

sample_indices <- sample(1:nrow(iris), 0.7 * nrow(iris))

train_data <- iris[sample_indices, ]

test_data <- iris[-sample_indices, ]

# Build a decision tree classifier

classifier <- rpart(Species ~ ., data = train_data, method = "class")

# Visualize the decision tree

rpart.plot(classifier, main = "Decision Tree Classifier")

# Make predictions on the test set

predictions <- predict(classifier, test_data, type = "class")

# Evaluate the classifier

confusion_matrix <- table(predictions, test_data$Species)

print("Confusion Matrix:")

print(confusion_matrix)
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)

print(paste("Accuracy:", round(accuracy, 2)))


Output:
5.Decision tree

library(rpart)

library(rpart.plot)

# Load the iris dataset

data(iris)

# Build the decision tree model

tree <- rpart(Species ~ ., data = iris, method = "class")

# Plot the decision tree

rpart.plot(tree, extra = 106)


Output:
6)Linear regression

# Generate a sample dataset

set.seed(123)

x <- rnorm(100, mean = 10, sd = 2)

y <- 3 * x + rnorm(100, mean = 0, sd = 3)

# Create a data frame

data <- data.frame(x = x, y = y)

# Implement linear regression

linear_model <- lm(y ~ x, data = data)

# Print the summary of the linear regression model

print(summary(linear_model))

# Visualize the linear regression line

plot(x, y, main = "Linear Regression", xlab = "X", ylab = "Y")

abline(linear_model, col = "red")


Output:
7) Data visualization

# Install and load ggplot2 library


if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
library(ggplot2)

# Load the mtcars dataset


data(mtcars)

# Create a scatter plot of mpg (miles per gallon) vs. hp (horsepower)


ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
labs(title = "Scatter Plot of MPG vs. Horsepower",
x = "Horsepower",
y = "Miles per Gallon")
Output:

You might also like