0% found this document useful (0 votes)
3 views22 pages

R Lab Manual

The document outlines a series of exercises for implementing various data mining and machine learning techniques using R, including the apriori algorithm for association rule mining, k-means clustering, hierarchical clustering, classification algorithms, decision trees, linear regression, and data visualization. Each exercise includes a clear aim, step-by-step procedure, and example code, with successful execution confirmed in the results. The document serves as a practical guide for users to apply these techniques in R.

Uploaded by

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

R Lab Manual

The document outlines a series of exercises for implementing various data mining and machine learning techniques using R, including the apriori algorithm for association rule mining, k-means clustering, hierarchical clustering, classification algorithms, decision trees, linear regression, and data visualization. Each exercise includes a clear aim, step-by-step procedure, and example code, with successful execution confirmed in the results. The document serves as a practical guide for users to apply these techniques in R.

Uploaded by

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

EX-NO : 1 Implement apriori algorithm to extract association rule of data mining

AIM:

To implement apriori algorithm to extract association rule of data mining Using R studio

PROCEDURE:

Step 1: Start R studio.

Step 2: Select File R script.

Step 3: Dialog box will appear. Enter filename.r and click save.

Step 4: If needed choose configure project else save.

Step 5: Write the coding in filename.r file.

Step 6: Save and run the application.

1
PROGRAM:

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)")

2
OUTPUT:

RESULT:

Hence the program has been executed successfully and the output is verified.

3
EX-NO : 2 Implement k-means clustering technique

AIM:

To implement k-means clustering technique using R.

PROCEDURE:

Step 1: Start R studio.

Step 2: Select File R script.

Step 3: Dialog box will appear. Enter filename.r and click save.

Step 4: If needed choose configure project else save.

Step 5: Write the coding in filename.r file.

Step 6: Save and run the application.

4
PROGRAM:

# Generate synthetic data

set.seed(123)

x <- cbind(x1 = rnorm(50, 0, 1), x2 = rnorm(50, 0, 1))

y <- cbind(x1 = rnorm(50, 5, 1), x2 = rnorm(50, 5, 1))

data <- rbind(x, y)

# Apply k-means clustering

k <- 2 # Number of clusters

kmeans_result <- kmeans(data, centers = k)

# Print cluster centers

print(kmeans_result$centers)

# Visualize clusters

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

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

5
OUTPUT:

RESULT:

Hence the program has been executed successfully and the output is verified.

6
EX-NO : 3 Implement any one hierarchical clustering

AIM:

To implement any one hierarchical clustering using R

PROCEDURE:

Step 1: Start R studio.

Step 2: Select File R script.

Step 3: Dialog box will appear. Enter filename.r and click save.

Step 4: If needed choose configure project else save.

Step 5: Write the coding in filename.r file.

Step 6: Save and run the application.

7
PROGRAM:

# Load the mtcars dataset

data(mtcars)

# View the structure of the dataset

str(mtcars)

# Perform hierarchical clustering using complete linkage

hc <- hclust(dist(mtcars), method = "complete")

# Plot the dendrogram

plot(hc, main = "Dendrogram of mtcars Dataset", xlab = "Cars", sub = "", cex = 0.6)

# Cut the dendrogram to get clusters

clusters <- cutree(hc, k = 3) # Specify the number of clusters (k)

# Add cluster labels to the dataset

mtcars$cluster <- clusters

# View the first few rows of the dataset with cluster labels

head(mtcars)

# Plot using cluster colors

plot(mtcars$mpg, mtcars$wt, col = clusters, pch = 19, main = "Clusters of mtcars Dataset", xlab =
"Miles/(US) gallon", ylab = "Weight (1000 lbs)")

legend("topright", legend = unique(clusters), col = 1:length(unique(clusters)), pch = 19)

8
OUTPUT:

RESULT:

Hence the program has been executed successfully and the output is verified.

9
EX-NO : 4 Implement classification algorithm

AIM:

To implement classification algorithm using R.

PROCEDURE:

Step 1: Start R studio.

Step 2: Select File R script.

Step 3: Dialog box will appear. Enter filename.r and click save.

Step 4: If needed choose configure project else save.

Step 5: Write the coding in filename.r file.

Step 6: Save and run the application

10
PROGRAM:

# Generate synthetic data for binary classification

set.seed(123)

n <- 100 # Number of data points

x1 <- runif(n, 0, 10) # Feature 1

x2 <- runif(n, 0, 10) # Feature 2

class <- as.factor(ifelse(x1 + x2 > 10, "A", "B")) # Binary class label

# Combine features and class labels into a data frame

data <- data.frame(x1, x2, class)

# View the first few rows of the data

head(data)

# Plot the synthetic data

plot(x1, x2, col = as.integer(class), pch = 19, xlab = "Feature 1", ylab = "Feature 2")

legend("topright", legend = levels(class), col = 1:length(levels(class)), pch = 19)

# Train a logistic regression model

model <- glm(class ~ x1 + x2, data = data, family = binomial)

# Summary of the model

summary(model)

# Predict using the trained model

predictions <-

predict(model, newdata = data, type = "response")

# Convert probabilities to class labels

predicted_class <- ifelse(predictions > 0.5, "A", "B")

# Calculate accuracy

accuracy <- mean(predicted_class == data$class)

print(paste("Accuracy of the logistic regression model:", round(accuracy*100, 2), "%"))

11
OUTPUT:

RESULT:

Hence the program has been executed successfully and the output is verified.

12
EX-NO : 5 Implement decision tree

AIM:

To implement decision tree using R.

PROCEDURE:

Step 1: Start R studio.

Step 2: Select File R script.

Step 3: Dialog box will appear. Enter filename.r and click save.

Step 4: If needed choose configure project else save.

Step 5: Write the coding in filename.r file.

Step 6: Save and run the application

13
PROGRAM:

# Load required library

library(rpart)

# Generate synthetic dataset

set.seed(123) # for reproducibility

n <- 100 # number of samples

x1 <- rnorm(n, mean = 0, sd = 1) # feature 1

x2 <- rnorm(n, mean = 0, sd = 1) # feature 2

y <- ifelse(x1 + x2 > 0, 1, 0) # target variable

# Combine features into a dataframe

data <- data.frame(x1, x2, y)

# Build decision tree model

model <- rpart(y ~ x1 + x2, data = data, method = "class")

# Plot the decision tree

plot(model)

text(model, use.n = TRUE)

14
OUTPUT:

RESULT:

Hence the program has been executed successfully and the output is verified.

15
EX-NO : 6 Linear Regression

AIM:

To implement linear regression using R.

PROCEDURE:

Step 1: Start R studio.

Step 2: Select File R script.

Step 3: Dialog box will appear. Enter filename.r and click save.

Step 4: If needed choose configure project else save.

Step 5: Write the coding in filename.r file.

Step 6: Save and run the application

16
PROGRAM:

# Generate synthetic data

set.seed(123)

x <- 1:100 # Independent variable

y <- 2*x + rnorm(100, mean = 0, sd = 5) # Dependent variable with noise

# Fit linear regression model

model <- lm(y ~ x)

# Summary of the model

summary(model)

# Plot the data and the regression line

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

abline(model, col = "red")

17
OUTPUT:

RESULT:

Hence the program has been executed successfully and the output is verified.

18
EX-NO : 7 Data Visualization

AIM:

To implement data visualization using R.

PROCEDURE:

Step 1: Start R studio.

Step 2: Select File R script.

Step 3: Dialog box will appear. Enter filename.r and click save.

Step 4: If needed choose configure project else save.

Step 5: Write the coding in filename.r file.

Step 6: Save and run the application

19
PROGRAM:

# Generate synthetic data

set.seed(123)

x <- 1:100

y <- rnorm(100, mean = 0, sd = 1)

# Plot histogram

hist(y, main = "Histogram of y", xlab = "Value of y", col = "skyblue")

OUTPUT:

# Plot scatter plot

plot(x, y, main = "Scatter plot of x and y", xlab = "x", ylab = "y")

OUTPUT:

20
# Plot line plot

plot(x, y, type = "l", main = "Line plot of x and y", xlab = "x", ylab = "y", col = "red")

OUTPUT:

# Plot box plot

boxplot(y, main = "Boxplot of y", ylab = "Value of y", col = "orange")

OUTPUT:

21
# Plot bar plot

barplot(table(cut(y, breaks = 5)), main = "Bar plot of y", xlab = "Bins of y", ylab = "Frequency", col =
"green")

OUTPUT:

RESULT:

Hence the program has been executed successfully and the output is verified.

22

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