Siddharth Arya 76 ML Practical File
Siddharth Arya 76 ML Practical File
HARYANA
Plot No.39, Rajiv Gandhi Education City, P.S.Rai, Sonepat, Haryana – 131029
(Established under Haryana Private University Act, 2006 as amended by Act No. 8 of 2013)
1|Page
Bonafide Certificate
Computer Graphics Lab Certified that this is the Bonafide record of practical work done by
the aforesaid student in the CS 3113 Computer Networks Lab Laboratory during the
academic year 2020 -2021.
Examiner
(Name and signature with date)
2|Page
INDEX
3|Page
8 8.1 – Write R program to plot bar chart.
8.2 – Write R program to plot histogram.
8.3 – Write R program to plot piechart.
8.4 – Write R program to plot linechart. 16 – 19
8.5 – Write R program to plot scatterplot.
8.6 – Write R program to plot boxplot.
4|Page
Experiment – 1
1.1 - Write R program to perform arithmetic operations on vectors.
Program:
# Create two vectors.
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11,0,8,1,2)
# Vector addition.
add.result <- v1+v2
print(add.result)
# Vector subtraction.
sub.result <- v1-v2
print(sub.result)
# Vector multiplication.
multi.result <- v1*v2
print(multi.result)
# Vector division.
divi.result <- v1/v2
print(divi.result)
Output:
5|Page
Output:
Output:
6|Page
Experiment – 2
2.1 – Write R program to manipulate list elements.
# Create a list containing a vector, a matrix and a list.
list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8),
nrow = 2),
list("green",12.3))
Output:
7|Page
Output:
list2 <-list(10:14)
print(list2)
print(v1)
print(v2)
Output:
8|Page
Experiment – 3
Program:
Output:
Program:
9|Page
Output:
Program:
# Create two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")
# Print the element in the 1st row and 3rd column of the 1st
matrix.
print(result[1,3,1])
Output:
10 | P a g e
Experiment – 4
Program:
Output:
Program:
11 | P a g e
matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
print(matrix2)
Output:
Program:
12 | P a g e
Output:
13 | P a g e
Experiment – 5
Program:
Output:
5.2 – Write R program to get the structure and summary of the dataframe.
Program:
14 | P a g e
Output:
Program:
Output:
15 | P a g e
Experiment – 6
Program:
print(data)
print(is.factor(data))
print(factor_data)
print(is.factor(factor_data))
Output:
Program:
Output:
16 | P a g e
6.3 – Write R program to generate factor levels.
Program:
Output:
17 | P a g e
Experiment – 7
Program:
Output:
Program:
Output:
18 | P a g e
7.3 – Write R program to show the lazy evaluation of function.
Program:
Output:
19 | P a g e
Experiment – 8
Program:
Output:
Program:
Output:
20 | P a g e
8.3 – Write R program to plot piechart.
Program:
21 | P a g e
8.4 – Write R program to plot linechart.
Program:
Output:
Program:
22 | P a g e
Output:
Program:
Output:
23 | P a g e
Experiment – 9
Program:
Output:
24 | P a g e
Experiment – 10
Program:
Output:
25 | P a g e
Experiment – 11
Program:
Output:
26 | P a g e
Experiment – 12
Program:
#Installing Packages
#install.packages("ClusterR")
#install.packages("cluster")
# Loading package
library(ClusterR)
library(cluster)
print(kmeans.re$cluster)
# Confusion Matrix
cm <- table(iris$Species, kmeans.re$cluster)
print(cm)
27 | P a g e
Output:
28 | P a g e
Experiment – 13
Program:
# Installing Packages
# install.packages("e1071")
# install.packages("caTools")
# install.packages("class")
# Loading package
library(e1071)
library(caTools)
library(class)
# Loading data
data(iris)
head(iris)
# Feature Scaling
train_scale <- scale(train_cl[, 1:4])
test_scale <- scale(test_cl[, 1:4])
# Confusiin Matrix
cm <- table(test_cl$Species, classifier_knn)
print(cm)
# K = 3
classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
29 | P a g e
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))
Output:
30 | P a g e