0% found this document useful (0 votes)
30 views20 pages

Implementing Apriori Algorithm in R

The document describes implementing various machine learning algorithms and data visualization techniques in R. It includes summaries of how to: 1. Apply the Apriori algorithm to extract association rules from transaction data and visualize the results. 2. Perform K-means clustering on the Iris dataset to identify clusters among data points. 3. Implement hierarchical clustering on the mtcars dataset using Euclidean distance. 4. Build a decision tree model on a reading skills dataset to predict native language. 5. Conduct linear regression analysis to model the relationship between weight and height with a scatter plot. 6. Generate visualizations including a histogram and heatmap.
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)
30 views20 pages

Implementing Apriori Algorithm in R

The document describes implementing various machine learning algorithms and data visualization techniques in R. It includes summaries of how to: 1. Apply the Apriori algorithm to extract association rules from transaction data and visualize the results. 2. Perform K-means clustering on the Iris dataset to identify clusters among data points. 3. Implement hierarchical clustering on the mtcars dataset using Euclidean distance. 4. Build a decision tree model on a reading skills dataset to predict native language. 5. Conduct linear regression analysis to model the relationship between weight and height with a scatter plot. 6. Generate visualizations including a histogram and heatmap.
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

1.

APRIORI ALGORITHM

Aim:

To implement Apriori algorithm to extract association rule in datamining.

Algorithm:

1. Open the R environment.


2. Create new script by choosing File-> New Script command.
3. Set the working directory to the location where the dataset and R script reside.
4. Load necessary R libraries including 'arules', 'arulesViz', and 'RColorBrewer'.
5. Load the 'Groceries' dataset which contains transaction data.
6. Use the apriori() function to mine association rules from the transaction data and set
parameters as minimum support (supp) and minimum confidence (conf)
7. Use the inspect () function to view the generated association rules.
8. Use the itemFrequencyPlot () function to visualize the relative frequency of items in the
dataset.
9. Close the R environment.
R Code
setwd("Z:/R lab/Subjects/2023-2024");
# 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 = [Link](8, 'Pastel2'),
main = 'Relative Item Frequency Plot',
type = "relative",
ylab = "Item Frequency (Relative)")
2. K- MEANS CLUSTERING TECHNIQUE

Aim:

To implement K-means clustering on the Iris dataset to identify clusters among the data points.

Algorithm:

1. Open the R environment.


2. Create new script by choosing File-> New Script command.
3. Set the working directory to the location where the dataset and R script reside.
4. Load the Iris dataset.
5. Install required R packages such as "ClusterR" and "cluster".
6. Load the necessary R packages, including "ClusterR" and "cluster".
7. Remove the species labels from the original dataset, leaving only the numerical features.
8. Fit a K-means clustering model to the training dataset using the kmeans() function.
9. Set the number of clusters to 3 and the number of random starts nstart to 20.
10. Retrieve the cluster assignments for each observation from the fitted K-means model.
11. Generate a confusion matrix to evaluate the performance of the clustering model.
12. Plot the original data points in a scatter plot and data points with colors representing the
assigned clusters.
13. Visualize the clusters using a cluster plot.
14. Close the R environment.
setwd("Z:/R lab/Subject/2023-2024");
# Loading data
data(iris)
# Structure
str(iris)
# Installing Packages
[Link]("ClusterR")
[Link]("cluster")
# Loading package
library(ClusterR)
library(cluster)
# Removing initial label of
# Species from original dataset
iris_1 <- iris[, -5]
# Fitting K-Means clustering Model
# to training dataset
[Link](240) # Setting seed
[Link] <- kmeans(iris_1, centers = 3, nstart = 20)
[Link]
# Cluster identification for
# each observation
[Link]$cluster
# Confusion Matrix
cm <- table(iris$Species, [Link]$cluster)
cm
# Model Evaluation and visualization
plot(iris_1[c("[Link]", "[Link]")])
plot(iris_1[c("[Link]", "[Link]")],
col = [Link]$cluster)
plot(iris_1[c("[Link]", "[Link]")],
col = [Link]$cluster,
main = "K-means with 3 clusters")
## Plotiing cluster centers
[Link]$centers
[Link]$centers[, c("[Link]", "[Link]")]
# cex is font size, pch is symbol
points([Link]$centers[, c("[Link]", "[Link]")],
col = 1:3, pch = 8, cex = 3)
## Visualizing clusters
y_kmeans <- [Link]$cluster
clusplot(iris_1[, c("[Link]", "[Link]")],
y_kmeans,
lines = 0,
shade = TRUE,
color = TRUE,
labels = 2,
plotchar = FALSE,
span = TRUE,
main = paste("Cluster iris"),
xlab = '[Link]',
ylab = '[Link]')
[Link] CLUSTERING

Aim:

To implement hierarchical clustering on the built-in mtcars dataset using the Euclidean distance
metric.

Algorithm:

1. Open the R environment.


2. Create new script by choosing File-> New Script command.
3. Use the dist() function to compute the Euclidean distance matrix between observations in
the mtcars dataset.
4. Apply the hclust() function to perform hierarchical clustering on the computed distance
matrix.
5. Plot the dendrogram using the plot() function on the hierarchical clustering object.
6. Determine the number of clusters by specifying a height or the desired number of
clusters.
7. Visualize the identified clusters by highlighting them on the dendrogram using
rectangles.
8. Close the R environment.
# Finding distance matrix
distance_mat <- dist(mtcars, method = 'euclidean')
distance_mat

# Fitting Hierarchical clustering Model


# to training dataset
[Link](240) # Setting seed
Hierar_cl <- hclust(distance_mat, method = "average")
Hierar_cl

# Plotting dendrogram
plot(Hierar_cl)

# Choosing no. of clusters


# Cutting tree by height
abline(h = 110, col = "green")

# Cutting tree by no. of clusters


fit <- cutree(Hierar_cl, k = 3 )
fit

table(fit)
[Link](Hierar_cl, k = 3, border = "dark orange")

5. DECISION TREE
Aim:

To implement Decision Tree algorithm on the readingSkills dataset.

Algorithm:

1. Open the R environment.


2. Create new script by choosing File-> New Script command.
3. Install and load necessary packages such as datasets, caTools, party, dplyr, and
magrittr.
4. Load the readingSkills dataset from the datasets package.
5. Split the dataset into training and testing sets using the [Link] function from the
caTools package.
6. Train a classification tree ctree model using the training data.
7. Predict the target variable nativeSpeaker based on the other variables in the dataset.
8. Visualize the trained classification tree model using the plot function.
9. Close the R environment.
[Link]('datasets')
[Link]('caTools')
[Link]('party')
[Link]('dplyr')
[Link]('magrittr')

library(datasets)
library(caTools)
library(party)
library(dplyr)
library(magrittr)

data("readingSkills")
head(readingSkills)

sample_data = [Link](readingSkills, SplitRatio = 0.8)


train_data <- subset(readingSkills, sample_data == TRUE)
test_data <- subset(readingSkills, sample_data == FALSE)

model<- ctree(nativeSpeaker ~ ., train_data)


plot(model)
6. LINEAR REGRESSION

Aim:

To perform linear regression analysis on a given dataset and visualize the relationship between
the predictor variable (weight) and the response variable (height) using a scatter plot with a fitted
regression line.

Algorithm:

1. Open the R environment.


2. Create new script by choosing File-> New Script command.
3. Create two vectors `x` and `y`, representing the predictor (weight in kg) and response
(height in cm) variables, respectively.
4. Fit a linear regression model (`lm`) to the data, with `y` as the response variable and `x`
as the predictor variable.
5. Specify the file name for the output chart (`[Link]`).
6. Capturing the plot output using `png()`.
7. Plot the data points with `x` on the x-axis and `y` on the y-axis, represented by blue
points.
8. Close the device and save the plot as a PNG file using `[Link]()`.
9. Close the R environment.
# Create the predictor and response variable.
x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
relation <- lm(y~x)

# Give the chart file a name.


png(file = "[Link]")

#dataset
linear regression

# Plot the chart.


plot(y,x,col = "blue",main = "Height & Weight Regression",
abline(lm(x~y)),cex = 1.3,pch = 16,xlab = "Weight in Kg",ylab =
"Height in cm")

# Save the file.


[Link]()
7. DATA VISUALIZATION

Aim

To generate visualizations, specifically a histogram and a heatmap

Algorithm

1. Open the R environment.


2. Create new script by choosing File-> New Script command.
3. Load the airquality dataset from the datasets package.
4. Plot a histogram of the maximum daily temperature (Temp column) using the hist()
function.
5. Customize the histogram title, x-axis label, x-axis limits, fill color, and frequency display.
6. Generate example data in the form of a 5x5 matrix using rnorm() to create random
normal data.
7. Assign column and row names to the matrix.
8. Draw a heatmap of the data matrix using the heatmap() function.
# Histogram for Maximum Daily Temperature
data(airquality)

hist(airquality$Temp, main ="La Histogram\


Maximum Temperature(Daily)",
xlab ="Temperature(Fahrenheit)",
xlim = c(50, 125), col ="yellow",
freq = TRUE)

# Set seed for reproducibility


# [Link](110)

# Create example data


data <- matrix(rnorm(50, 0, 5), nrow = 5, ncol = 5)

# Column names
colnames(data) <- paste0("col", 1:5)
rownames(data) <- paste0("row", 1:5)

# Draw a heatmap
heatmap(data)
4. CLASSIFICATION ALGORITHM.

Aim

to implement k-nearest neighbors (KNN) classification on the Iris

Algorithm:

1. Open the R environment.


2. Create new script by choosing File-> New Script command.
3. Load the Iris dataset.
4. Split the dataset into training and testing sets.
5. Scale the features of both the training and testing sets.
6. Fit KNN models to the training data for different values of k (1, 3, 5, 7, 15, and 19).
7. Predict the classes of the testing set using each trained model.
8. Calculate the confusion matrix for each model.
9. Calculate the accuracy of each model using the misclassification error.
10. Plot the accuracy of the KNN model for different values of k using `ggplot2`.
# Loading data
data(iris)

# Structure
str(iris)

# Installing Packages
[Link]("e1071")
[Link]("caTools")
[Link]("class")

# Loading package
library(e1071)
library(caTools)
library(class)

# Loading data
data(iris)
head(iris)

# Splitting data into train and test data


split <- [Link](iris, SplitRatio = 0.7)
train_cl <- subset(iris, split == "TRUE")
test_cl <- subset(iris, split == "FALSE")

# Feature Scaling
train_scale <- scale(train_cl[, 1:4])
test_scale <- scale(test_cl[, 1:4])

head(train_scale)
head(test_scale)

# Fitting KNN Model to training dataset


classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
k = 1)
classifier_knn

# Confusiin Matrix
cm <- table(test_cl$Species, classifier_knn)
cm

# Model Evaluation - Choosing K


# Calculate out of Sample error
misClassError <- mean(classifier_knn != test_cl$Species)
print(paste('Accuracy =', 1-misClassError))

# K = 3
classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
k = 3)
misClassError <- mean(classifier_knn != test_cl$Species)
print(paste('Accuracy =', 1-misClassError))

# K = 5
classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
k = 5)
misClassError <- mean(classifier_knn != test_cl$Species)
print(paste('Accuracy =', 1-misClassError))

# K = 7
classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
k = 7)
misClassError <- mean(classifier_knn != test_cl$Species)
print(paste('Accuracy =', 1-misClassError))

# K = 15
classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
k = 15)
misClassError <- mean(classifier_knn != test_cl$Species)
print(paste('Accuracy =', 1-misClassError))

# K = 19
classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
k = 19)
misClassError <- mean(classifier_knn != test_cl$Species)
print(paste('Accuracy =', 1-misClassError))

library(ggplot2)

# Data preparation
k_values <- c(1, 3, 5, 7, 15, 19)

# Calculate accuracy for each k value


accuracy_values <- sapply(k_values, function(k) {
classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
k = k)
1 - mean(classifier_knn != test_cl$Species)
})

# Create a data frame for plotting


accuracy_data <- [Link](K = k_values, Accuracy =
accuracy_values)

# Plotting
ggplot(accuracy_data, aes(x = K, y = Accuracy)) +
geom_line(color = "lightblue", size = 1) +
geom_point(color = "lightgreen", size = 3) +
labs(title = "Model Accuracy for Different K Values",
x = "Number of Neighbors (K)",
y = "Accuracy") +
theme_minimal()

[Link]

Common questions

Powered by AI

KNN classification and Decision Trees both classify data but differ in approach. KNN classifies based on proximity to training data points, requiring scaling of Iris dataset features and testing various k-values to find the optimal classification accuracy. It is non-parametric, directly relying on data distribution. In contrast, Decision Trees, like the ctree used on readingSkills, recursively split data based on variable importance, creating model interpretability through tree structures. Decision Trees can handle categorical and continuous data simultaneously, whereas KNN primarily deals with continuous or transformed categorical data .

Feature scaling ensures that all features contribute equally to distance calculations central to KNN classifications. In datasets like Iris, scaling features to a common range is crucial as KNN depends on Euclidean distance, which can be skewed by unscaled features, potentially degrading performance by overemphasizing variables with larger numerical ranges. Without scaling, the model may misclassify due to biased distances. Scaling aligns feature contribution, leading to improved accuracy and ensuring fair assessment within and across features .

The ctree function models a dataset by constructing a decision tree where decisions are made based on variable splits that increase homogeneity of target values in each node. In the readingSkills dataset, after splitting data into training and test sets, the relationship between the nativeSpeaker and other variables is modeled. Plotting the decision tree allows for visual understanding of decision paths taken, which is crucial for interpreting how classifications are determined and for identifying possible improvements .

The Apriori algorithm uses minimum support and confidence settings to filter association rules. Minimum support is the threshold for how frequently a set of items appears in the dataset. Rules must meet or exceed this frequency to be considered. Minimum confidence represents the likelihood of finding item B in transactions that contain item A. By setting these parameters, the algorithm focuses on rules that are both common and reliable, reducing noise and computational complexity .

Hierarchical clustering on the mtcars dataset starts by computing an Euclidean distance matrix with dist(). Using hclust(), it applies hierarchical clustering on this matrix, choosing an averaging method. A dendrogram is plotted to visualize clusters hierarchically and aids in choosing the number of clusters by either cutting at a specified height or selecting a number of clusters to highlight in different colors. Dendrograms illustrate data point linkage at various levels, helping identify natural groupings .

Out-of-sample error, calculated as the misclassification error, evaluates model accuracy by comparing predicted classifications against true values in test data not included during training. For KNN, it indicates generalization capability, showing how well the model performs on unseen data. Low out-of-sample errors suggest robust model predictions, while higher errors may imply overfitting or an inadequate number of neighbors (k) chosen. This metric is indispensable for model validation and optimization .

Histogram and heatmap visualizations provide insights into dataset characteristics by displaying data distribution and density. In airquality, a histogram of maximum daily temperature highlights frequency distribution, revealing patterns or anomalies. A heatmap of a 5x5 matrix visualizes data intensity through color gradients, aiding in identifying clusters or correlations. These tools benefit users by simplifying complex data into intuitive graphics, facilitating better data-driven decisions .

The implementation begins with opening the R environment, loading the Iris dataset, and removing species labels. Key steps include using the kmeans() function with parameters like the number of clusters (centers) set to 3, and random starting points (nstart) to 20. The number of clusters affects the granularity of grouping, while nstart influences the robustness of results by avoiding local minima. A confusion matrix and cluster plot are used to evaluate and visualize the model performance .

Linear regression models relationships between variables by fitting a linear equation to observed data. For weight and height, it assumes a linear relationship which is quantified through parameters estimated from data using lm(). Visualization, such as with scatter plots overlaid by a regression line, helps in assessing model fit. Superb fits appear as points closely clustered along the regression line, making it easier to identify trends, outliers, and the strength and direction of associations .

Setting a seed value in randomized algorithms ensures that results can be replicated by regenerating the same sequence of random numbers. This is crucial in K-means clustering as it relies on random initialization of centroids. By setting a seed before clustering, the algorithm produces consistent results across multiple runs, facilitating reproducibility and comparison of clustering outcomes, crucial for validating results and maintaining integrity in scientific studies .

You might also like