[go: up one dir, main page]

0% found this document useful (0 votes)
279 views30 pages

Siddharth Arya 76 ML Practical File

The document is a laboratory record submitted by Siddharth Arya, a student of the Department of Computer Science and Engineering at SRM University Delhi-NCR, Sonepat, Haryana for their Computer Networks Lab course. It contains 13 experiments on various R programming concepts like vectors, lists, arrays, matrices, dataframes, factors, functions, and machine learning algorithms. Each experiment section contains 3 short programs related to the topic and the output. The record is certified by the faculty in-charge and submitted for examination.

Uploaded by

Siddharth arya
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)
279 views30 pages

Siddharth Arya 76 ML Practical File

The document is a laboratory record submitted by Siddharth Arya, a student of the Department of Computer Science and Engineering at SRM University Delhi-NCR, Sonepat, Haryana for their Computer Networks Lab course. It contains 13 experiments on various R programming concepts like vectors, lists, arrays, matrices, dataframes, factors, functions, and machine learning algorithms. Each experiment section contains 3 short programs related to the topic and the output. The record is certified by the faculty in-charge and submitted for examination.

Uploaded by

Siddharth arya
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/ 30

SRM UNIVERSITY DELHI-NCR, SONEPAT,

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)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


COMPUTER NETWORKS LAB (CS 3113)
LABORATORY RECORD

SUBMITTED TO: - SUBMITTED BY:


Name: Mr. Ravi Verma Name: Siddharth Arya
Class: CSE – DS & AI
Section: D
Reg. No.: 11019210076
Semester: 5th

1|Page
Bonafide Certificate

Student Name Siddharth Arya


Registration No 11019210076
Year/Semester 3rd / 5th
Department/ Programme B. Tech Computer Science and Engineering Programme
Section D
Subject code/Subject Name CS 3119 / ML with R Language

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.

Faculty (In-charge) (Head of the Department)

Submission for the Practical Examination held on……………………………………

Examiner
(Name and signature with date)

2|Page
INDEX

Exp No TITLE Page. No

1 1.1 - Write R program to perform arithmetic operations on vectors.


1.2 - Write R program for vector element recycling. 1–2
1.3 - Write R program to sort the elements of a vector.

2 2.1 – Write R program to manipulate list elements.


2.2 – Write R program to merge two lists.
3–4
2.3 - Write R program to convert list into a vector.

3 3.1 – Write R program to create an array.


3.2 – Write R program to name rows and columns in an array.
5–6
3.3 – Write R program to access array elements.

4 4.1 – Write R program to create a matrix.


4.2 – Write R program to perform addition and subtraction between two
matrices.
7–9
4.3 – Write R program to perform multiplication and division between
two matrices.

5 5.1 – Write R program to create a dataframe.


5.2 – Write R program to get the structure and summary of the
dataframe. 10 – 11
5.3 – Write R program to add new column to the dataframe.

6 6.1 – Write R program to create factors.


6.2 – Write R program to change the order level of the factors.
12 – 13
6.3 – Write R program to generate factor levels.

7 7.1 – Write R program to create a function.


7.2 – Write R program to call a function with default arguments.
14 – 15
7.3 – Write R program to show the lazy evaluation of function.

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.

9 9.1 – Write R Program to implement linear Regression.


20 – 20

10 10.1 – Write R program to implement decision tree algorithm. 21 – 21

11 11.1 – Write R program to implement Random Forest ML algorithm. 22 – 22

12 12.1 – Write R program to implement K Means Clustering algorithm. 23 – 24

13 13.1 – Write R program to implement K Nearest Neighbour algorithm. 25 – 26

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:

1.2 - Write R program for vector element recycling.


Program:
v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11)

add.result <- v1+v2


print(add.result)

sub.result <- v1-v2


print(sub.result)

5|Page
Output:

1.3 - Write R program to sort the elements of a vector.


Program:
v <- c(3,8,4,5,0,11, -9, 304)

# Sort the elements of the vector.


sort.result <- sort(v)
print(sort.result)

# Sort the elements in the reverse order.


revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

# Sorting character vectors.


v <- c("Red","Blue","yellow","violet")
sort.result <- sort(v)
print(sort.result)

# Sorting character vectors in reverse order.


revsort.result <- sort(v, decreasing = TRUE)
print(revsort.result)

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

# Give names to the elements in the list.


names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")

# Add element at the end of the list.


list_data[4] <- "New element"
print(list_data[4])

# Remove the last element.


list_data[4] <- NULL

# Print the 4th Element.


print(list_data[4])

# Update the 3rd Element.


list_data[3] <- "updated element"
print(list_data[3])

Output:

2.2 – Write R program to merge two lists.


# Create two lists.
list1 <- list(1,2,3)
list2 <- list("Sun","Mon","Tue")

# Merge the two lists.


merged.list <- c(list1,list2)

# Print the merged list.


print(merged.list)

7|Page
Output:

2.3 - Write R program to convert list into a vector.


Program:
# Create lists.
list1 <- list(1:5)
print(list1)

list2 <-list(10:14)
print(list2)

# Convert the lists to vectors.


v1 <- unlist(list1)
v2 <- unlist(list2)

print(v1)
print(v2)

# Now add the vectors


result <- v1+v2
print(result)

Output:

8|Page
Experiment – 3

3.1 – Write R program to create an array.

Program:

# Create two vectors of different lengths.


vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)

# Take these vectors as input to the array.


result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)

Output:

3.2 – Write R program to name rows and columns in an array.

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

# Take these vectors as input to the array.


result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames =
list(row.names,column.names,
matrix.names))
print(result)

9|Page
Output:

3.3 – Write R program to access array elements.

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

# Take these vectors as input to the array.


result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames =
list(row.names,
column.names, matrix.names))

# Print the third row of the second matrix of the array.


print(result[3,,2])

# Print the element in the 1st row and 3rd column of the 1st
matrix.
print(result[1,3,1])

# Print the 2nd Matrix.


print(result[,,2])

Output:

10 | P a g e
Experiment – 4

4.1 – Write R program to create a matrix.

Program:

# Elements are arranged sequentially by row.


M <- matrix(c(3:14), nrow = 4, byrow = TRUE)
print(M)

# Elements are arranged sequentially by column.


N <- matrix(c(3:14), nrow = 4, byrow = FALSE)
print(N)

# Define the column and row names.


rownames = c("row1", "row2", "row3", "row4")
colnames = c("col1", "col2", "col3")

P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames =


list(rownames, colnames))
print(P)

Output:

4.2 – Write R program to perform addition and subtraction between two


matrices.

Program:

# Create two 2x3 matrices.


matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
print(matrix1)

11 | P a g e
matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
print(matrix2)

# Add the matrices.


result <- matrix1 + matrix2
cat("Result of addition","\n")
print(result)

# Subtract the matrices


result <- matrix1 - matrix2
cat("Result of subtraction","\n")
print(result)

Output:

4.3 – Write R program to perform multiplication and division between two


matrices.

Program:

# Create two 2x3 matrices.


matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
print(matrix1)

matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)


print(matrix2)

# Multiply the matrices.


result <- matrix1 * matrix2
cat("Result of multiplication","\n")
print(result)

# Divide the matrices


result <- matrix1 / matrix2
cat("Result of division","\n")
print(result)

12 | P a g e
Output:

13 | P a g e
Experiment – 5

5.1 – Write R program to create a dataframe.

Program:

# Create the data frame.


emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),

start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-


15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Print the data frame.
print(emp.data)

Output:

5.2 – Write R program to get the structure and summary of the dataframe.

Program:

# Create the data frame.


emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),

start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-


15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Get the structure of the data frame.
str(emp.data)
print(summary(emp.data))

14 | P a g e
Output:

5.3 – Write R program to add new column to the dataframe.

Program:

# Create the data frame.


emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),

start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-


15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
print(emp.data)

# Add the "dept" coulmn.


emp.data$dept <- c("IT","Operations","IT","HR","Finance")
v <- emp.data
print(v)

Output:

15 | P a g e
Experiment – 6

6.1 – Write R program to create factors.

Program:

# Create a vector as input.


data <-
c("East","West","East","North","North","East","West","West","West
","East","North")

print(data)
print(is.factor(data))

# Apply the factor function.


factor_data <- factor(data)

print(factor_data)
print(is.factor(factor_data))

Output:

6.2 – Write R program to change the order level of the factors.

Program:

data <- c("East","West","East","North","North","East","West",


"West","West","East","North")
# Create the factors
factor_data <- factor(data)
print(factor_data)

# Apply the factor function with required order of the level.


new_order_data <- factor(factor_data,levels =
c("East","West","North"))
print(new_order_data)

Output:

16 | P a g e
6.3 – Write R program to generate factor levels.

Program:

v <- gl(3, 4, labels = c("Tampa", "Seattle","Boston"))


print(v)

Output:

17 | P a g e
Experiment – 7

7.1 – Write R program to create a function.

Program:

# Create a function to print squares of numbers in sequence.


new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}

# Call the function new.function supplying 6 as an argument.


new.function(6)

Output:

7.2 – Write R program to call a function with default arguments.

Program:

# Create a function with arguments.


new.function <- function(a = 3, b = 6) {
result <- a * b
print(result)
}

# Call the function without giving any argument.


new.function()

# Call the function with giving new values of the argument.


new.function(9,5)

Output:

18 | P a g e
7.3 – Write R program to show the lazy evaluation of function.

Program:

# Create a function with arguments.


new.function <- function(a, b) {
print(a^2)
print(a)
print(b)
}

# Evaluate the function without supplying one of the arguments.


new.function(6)

Output:

19 | P a g e
Experiment – 8

8.1 – Write R program to plot bar chart.

Program:

# Create the data for the chart


H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")

# Plot the bar chart


barplot(H,names.arg=M,xlab="Month",ylab="Revenue",col="blue",
main="Revenue chart",border="red")

Output:

8.2 – Write R program to plot histogram.

Program:

# Create data for the graph.


v <- c(9,13,21,8,36,22,12,41,31,33,19)

# Create the histogram.


hist(v,xlab = "Weight",col = "yellow",border = "blue")

Output:

20 | P a g e
8.3 – Write R program to plot piechart.

Program:

# Create data for the graph.


x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")
# Plot the chart with title and rainbow color pallet.
pie(x, labels, main = "City pie chart", col = rainbow(length(x)))
Output:

21 | P a g e
8.4 – Write R program to plot linechart.

Program:

# Create the data for the chart.


v <- c(7,12,28,3,41)

# Plot the bar chart.


plot(v,type = "o", col = "red", xlab = "Month", ylab = "Rain
fall",
main = "Rain fall chart")

Output:

8.5 – Write R program to plot scatterplot.

Program:

# Get the input values.


input <- mtcars[,c('wt','mpg')]
# Plot the chart for cars with weight between 2.5 to 5 and
mileage between 15 and 30.
plot(x = input$wt,y = input$mpg,
xlab = "Weight",
ylab = "Milage",
xlim = c(2.5,5),
ylim = c(15,30),
main = "Weight vs Milage"
)

22 | P a g e
Output:

8.6 – Write R program to plot boxplot.

Program:

# Plot the chart.


boxplot(mpg ~ cyl, data = mtcars, xlab = "Number of Cylinders",
ylab = "Miles Per Gallon", main = "Mileage Data")

Output:

23 | P a g e
Experiment – 9

9.1 – Write R Program to implement linear Regression.

Program:

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

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

Output:

24 | P a g e
Experiment – 10

10.1 – Write R program to implement decision tree algorithm.

Program:

# Load the party package. It will automatically load other


# dependent packages.
library(party)

# Create the input data frame.


input.dat <- readingSkills[c(1:105),]

# Create the tree.


output.tree <- ctree(
nativeSpeaker ~ age + shoeSize + score,
data = input.dat)

# Plot the tree.


plot(output.tree)

Output:

25 | P a g e
Experiment – 11

11.1 – Write R program to implement Random Forest ML algorithm.

Program:

# Load the party package. It will automatically load other


# required packages.
library(party)
library(randomForest)

# Create the forest.


output.forest <- randomForest(nativeSpeaker ~ age + shoeSize +
score,
data = readingSkills)

# View the forest results.


print(output.forest)

# Importance of each predictor.


print(importance(output.forest,type = 2))

Output:

26 | P a g e
Experiment – 12

12.1 – Write R program to implement K Means Clustering algorithm.

Program:

#Installing Packages
#install.packages("ClusterR")
#install.packages("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

set.seed(240) # Setting seed


kmeans.re <- kmeans(iris_1, centers = 3, nstart = 20)
print(kmeans.re)

# Cluster identification for


# each observation

print(kmeans.re$cluster)

# Confusion Matrix
cm <- table(iris$Species, kmeans.re$cluster)
print(cm)

y_kmeans <- kmeans.re$cluster


clusplot(iris_1[, c("Sepal.Length", "Sepal.Width")],
y_kmeans,
lines = 0,
shade = TRUE,
color = TRUE,
labels = 2,
plotchar = FALSE,
span = TRUE,
main = paste("Cluster iris"),
xlab = 'Sepal.Length',
ylab = 'Sepal.Width')

27 | P a g e
Output:

28 | P a g e
Experiment – 13

13.1 – Write R program to implement K Nearest Neighbour algorithm.

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)

# Splitting data into train


# and test data
split <- sample.split(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])

# Fitting KNN Model


# to training dataset
classifier_knn <- knn(train = train_scale,
test = test_scale,
cl = train_cl$Species,
k = 1)
print(classifier_knn)

# Confusiin Matrix
cm <- table(test_cl$Species, classifier_knn)
print(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,

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

You might also like