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

chapter 3 p2

The document provides an overview of R programming, including how to write scripts, the use of comments, and the basic classes of objects in R such as vectors, lists, matrices, and data frames. It explains data types, attributes, keywords, and operators in R, detailing their functions and examples. Additionally, it covers logical, relational, and arithmetic operators, along with their usage in programming.

Uploaded by

sagarmeravi563
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

chapter 3 p2

The document provides an overview of R programming, including how to write scripts, the use of comments, and the basic classes of objects in R such as vectors, lists, matrices, and data frames. It explains data types, attributes, keywords, and operators in R, detailing their functions and examples. Additionally, it covers logical, relational, and arithmetic operators, along with their usage in programming.

Uploaded by

sagarmeravi563
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

R Programming script file:-

usually you will do your programming by writing your program in


script file and then you execute the script at your command
prompt with the help of R interpreter called R script.

Comments:-
• comments are like helping text in your R program and they are
ignore by the interpreter while executing actual program.
• single comment is write using hash symbol(#) in the beginning of
the statement as follows
# my first program in R programming

R does not support multiline comments but you can perform trick
which is something as follows
if ( false){
“ this is a demo for multiline comments and it should be put inside either
single or double quote”
}
myString<-” hi welcome!”
print( myString)
[1] “ I welcome
Essentials of R programming
Objects of R:-
• R has five basic or atomic classes of object.
• everything you see or created in R is an object.
• A Vector, matrix, data frame even a variable is an object.
• R treat it that way. so has five basic classes of object which are as
follows:
1) character
2) numeric( real number)
3) integer( whole numbers)
4) complex
5) logical( true/ false)
Attributes:-
these classes have attributes. think of attributes as their identifier,
name or number which appropriately identify them.
an object can have following attribute
1) names, dimension names
2) dimension
3)class
4) length

attributes of an object can be access using attributes()


function. The attributes() function Returns or sets all attributes
of data object.
R - Data Types
In contrast to other programming languages like C and
java in R, the variables are not declared as some data
type.

The variables are assigned with R-Objects and the data


type of the R-object becomes the data type of the
variable.
There are many types of R-objects. The frequently used
ones are −
•Vectors
•Lists
•Matrices
•Arrays
•Factors
•Data Frame
The simplest of these objects is the vector object and
there are six data types of these atomic vectors, also
termed as six classes of vectors. The other R-Objects are
built upon the atomic vectors.

In R programming, the very basic data types are the


R-objects called vectors which hold elements of different
classes as shown below.
Data Type Example Verify

Logical TRUE, FALSE


v <- TRUE print(class(v))it produces the following result −
[1] "logical"

Numeric 12.3, 5, 999 v <- 23.5 print(class(v))it produces the following result −
[1] "numeric"

Integer 2L, 34L, 0L v <- 2L print(class(v))it produces the following result −


[1] "integer"

Complex 3 + 2i v <- 2+5i print(class(v))it produces the following result −


[1] "complex"

Character 'a' , '"good", "TRUE", '23.4' v <- "TRUE" print(class(v))it produces the following result −
[1] "character"

Raw "Hello" is stored as 48 65 6c 6c 6f v <- charToRaw("Hello") print(class(v))it produces the following


result −
[1] "raw"
Vectors
When you want to create vector with more than one
element, you should use c() function which means to
combine the elements into a vector.

# Create a vector.
apple <- c('red','green',"yellow")
print(apple)

# Get the class of the vector.


print(class(apple))
When we execute the above code, it produces the following
result −

[1] "red" "green" "yellow"


[1] "character"
Lists
A list is an R-object which can contain many different
types of elements inside it like vectors, functions and even
another list inside it.

# Create a list.
list1 <- list(c(2,5,3),21.3,sin)

# Print the list.


print(list1)
When we execute the above code, it produces the
following result −
[[1]]
[1] 2 5 3

[[2]]
[1] 21.3

[[3]]
function (x) .Primitive("sin")
Matrices

A matrix is a two-dimensional rectangular data set. It can


be created using a vector input to the matrix function.

# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow =
TRUE)
print(M)
When we execute the above code, it produces the
following result −

[,1] [,2] [,3]


[1,] "a" "a" "b"
[2,] "c" "b" "a"
Arrays
While matrices are confined to two dimensions, arrays can
be of any number of dimensions.
The array function takes a dim attribute which creates the
required number of dimension.
In the below example we create an array with two
elements which are 3x3 matrices each.
# Create an array.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
When we execute the above code, it produces the
following result −
,,1

[,1] [,2] [,3]


[1,] "green" "yellow" "green"
[2,] "yellow" "green" "yellow"
[3,] "green" "yellow" "green"

,,2

[,1] [,2] [,3]


[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"
Factors

Factors are the r-objects which are created using a vector.

It stores the vector along with the distinct values of the


elements in the vector as labels.

The labels are always character irrespective of whether it


is numeric or character or Boolean etc.
in the input vector. They are useful in statistical modeling.

Factors are created using the factor() function.


The nlevels functions gives the count of levels.
# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')

# Create a factor object.


factor_apple <- factor(apple_colors)

# Print the factor.


print(factor_apple)
print(nlevels(factor_apple))
When we execute the above code, it produces the
following result −
[1] green green yellow red red red green
Levels: green red yellow
[1] 3
Data Frames

Data frames are tabular data objects.


Unlike a matrix in data frame each column can contain
different modes of data.
The first column can be numeric while the second column
can be character and third column can be logical. It is a
list of vectors of equal length.
Data Frames are created using the data.frame() function.
# Create the data frame.
BMI <- data.frame(
gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)
When we execute the above code, it produces the
following result −

gender height weight Age


1 Male 152.0 81 42
2 Male 171.5 93 38
3 Female 165.0 78 26
Keywords in R Programming
In programming, a keyword is a word which is
reserved by a program because it has a special
meaning.

A keyword can be a command or a parameter.


Like in C, C++, Java, there is also a set of
keywords in R.
A keyword can't be used as a variable name.
Keywords are also called as "reserved names."
There are the following keywords as
per ?reserved or help(reserved) command:

if else repeat

while function for

next break TRUE

FALSE NULL Inf

NaN NA NA_integer_

NA_real_ NA_complex_ NA_character_


Operators in R
In computer programming, an operator is a
symbol which represents an action.

An operator is a symbol which tells the compiler to


perform
specific logical or mathematical manipulations.

R programming is very rich in built-in operators.


In R programming, there are different types of
operator, and each operator performs a different
task.
For data manipulation, There are some advance
operators also such as model formula and list
indexing.
There are the following types of operators
used in R:

1.Arithmetic Operators
2.Relational Operators
3.Logical Operators
4.Assignment Operators
5.Miscellaneous Operators
Arithmetic Operators
S. No Operator Description Example
1. + This operator is used to add two vectors in R. a <- c(2, 3.3, 4) b <- c(11, 5, 3) print(a+b) It will give us
the following output: [1] 13.0 8.3 5.0

2. - This operator is used to divide a vector from another one. a <- c(2, 3.3, 4) b <- c(11, 5, 3) print(a-b) It will give us
the following output: [1] -9.0 -1.7 3.0

3. * This operator is used to multiply two vectors with each other. a <- c(2, 3.3, 4) b <- c(11, 5, 3) print(a*b) It will give us
the following output: [1] 22.0 16.5 4.0

4. / This operator divides the vector from another one. a <- c(2, 3.3, 4) b <- c(11, 5, 3) print(a/b)It will give us
the following output: [1] 0.1818182
0.6600000 4.0000000

5. %% This operator is used to find the remainder of the first vector with the second b <- c(11, 5, 3) print(a%%b) It will give
vector. a <- c(2, 3.3, 4) us the following output: [1] 2.0 3.3 0

6. %/% This operator is used to find the division of the first vector with the a <- c(2, 3.3, 4) b <- c(11, 5, 3)
second(quotient). print(a%/%b) It will give us the
following output: [1] 0 0 4

7. ^ This operator raised the first vector to the exponent of the second vector. a <- b <- c(11, 5, 3) print(a^b) It will give us
c(2, 3.3, 4) the following output: [1] 0248.0000
391.3539 4.0000
Relational Operators
A relational operator is a symbol which defines
some kind of relation between two entities.
These include numerical equalities and inequalities.

A relational operator compares each element of


the first vector with the corresponding element of
the second vector.
The result of the comparison will be a Boolean
value.
S. No Operator Description Example
1. > This operator will return TRUE when a <- c(1, 3, 5) b <- c(2, 4, 6) print(a>b) It will give us
every element in the first vector is the following output: [1] FALSE FALSE FALSE
greater than the corresponding
element of the second vector.

2. < This operator will return TRUE when a <- c(1, 9, 5) b <- c(2, 4, 6) print(a<b) It will give us
every element in the first vector is the following output: [1] FALSE TRUE FALSE
less then the corresponding element
of the second vector.

3. <= This operator will return TRUE when a <- c(1, 3, 5) b <- c(2, 3, 6) print(a<=b) It will give us
every element in the first vector is the following output: [1] TRUE TRUE TRUE
less than or equal to the
corresponding element of another
vector.

4. >= This operator will return TRUE when a <- c(1, 3, 5) b <- c(2, 3, 6) print(a>=b) It will give us
every element in the first vector is the following output: [1] FALSE TRUE FALSE
greater than or equal to the
corresponding element of another
vector.

5. == This operator will return TRUE when a <- c(1, 3, 5) b <- c(2, 3, 6) print(a==b) It will give us
every element in the first vector is the following output:[1] FALSE TRUE FALSE
equal to the corresponding element of
the second vector.

6. != This operator will return TRUE when a <- c(1, 3, 5) b <- c(2, 3, 6) print(a>=b) It will give us
every element in the first vector is the following output: [1] TRUE FALSE TRUE
not equal to the corresponding
element of the second vector.
Logical Operators
The logical operators allow a program to make a
decision on the basis of multiple conditions.
In the program, each operand is considered as a
condition which can be evaluated to a false or true
value.
The value of the conditions is used to determine
the overall value of the op1 operator op2.
Logical operators are applicable to those vectors
whose type is logical, numeric, or complex.
The logical operator compares each element of the
first vector with the corresponding element of the
second vector.
There are the following types of operators which
are supported by R:
S. Operator Description Example
No
1. & This operator is known as the Logical AND operator. Each bit represent separate result. a <- c(3, 0, TRUE, 2+2i) b <- c(2,
When two bits are true then it gives result true otherwise it gives result False 4, TRUE, 2+3i) print(a&b) It will
give us the following output:
[1] TRUE FALSE TRUE TRUE

2. | This operator is called the Logical OR operator. Each bit represent separate result. When two a <- c(3, 0, TRUE, 2+2i) b <- c(2,
bits are false then it gives result false otherwise it will give result True. 4, TRUE, 2+3i) print(a|b) It will
give us the following output:
[1] TRUE TRUE TRUE TRUE

3. ! This operator is known as Logical NOT operator. This operator takes the first element of the a <- c(3, 0, TRUE, 2+2i) print(!a)
vector and gives the opposite logical value as a result. It will give us the following
output: [1] FALSE TRUE FALSE
FALSE

4. && This operator takes the first element of both the vector and gives TRUE as a result, only if a <- c(3, 0, TRUE, 2+2i) b <- c(2,
both are TRUE. 4, TRUE, 2+3i) print(a&&b) It
will give us the following
output: [1] TRUE

5. || This operator takes the first element of both the vector and gives the result TRUE, if one of a <- c(3, 0, TRUE, 2+2i) b <- c(2,
them is true. 4, TRUE, 2+3i) print(a||b) It will
give us the following output:
[1] TRUE
Truth table

Logical NAND Logical NOR

p↑ p↓
p q p q
q q
T T F T T F
T F T T F F
F T T F T F
F F T F F T
Assignment Operators
An assignment operator is used to assign a new
value to a variable.
In R, these operators are used to assign values to
vectors.
There are the following types of assignment
S. Operator Description Example
No
1. <- or = or These operators are known as left a <- c(3, 0, TRUE, 2+2i) b <<- c(2, 4, TRUE, 2+3i) d = c(1, 2, TRUE,
<<- assignment operators. 2+3i) print(a) print(b) print(d) It will give us the following output: [1]
3+0i 0+0i 1+0i 2+2i [1] 2+0i 4+0i 1+0i 2+3i [1] 1+0i 2+0i 1+0i 2+3i

2. -> or ->> These operators are known as right c(3, 0, TRUE, 2+2i) -> a c(2, 4, TRUE, 2+3i) ->> b print(a) print(b) It will
assignment operators. give us the following output: [1] 3+0i 0+0i 1+0i 2+2i [1] 2+0i 4+0i
1+0i 2+3i
Miscellaneous Operators
Miscellaneous operators are used for a special and
specific purpose.
These operators are not used for general
mathematical or logical computation.
There are the following miscellaneous operators
which are supported in R
S. No Operator Description Example
1. : The colon operator is used to create the series v <- 1:8 print(v) It will give us the following output: [1] 1 2
of numbers in sequence for a vector. 345678

2. %in% This is used when we want to identify if an a1 <- 8 a2 <- 12 d <- 1:10 print(a1%in%d) print(a2%in%d) It
element belongs to a vector. will give us the following output: [1] FALSE [1] FALSE

3. %*% It is used to multiply a matrix with its M=matrix(c(1,2,3,4,5,6), nrow=2, ncol=3, byrow=TRUE)
transpose. T=m%*%T(m) print(T) It will give us the following output:
14 32 32 77
R - Variables

A variable provides us with named storage that our


programs can manipulate.
A variable in R can store an atomic vector, group of
atomic vectors or a combination of many Robjects
A valid variable name consists of letters, numbers and the
dot or underline characters. The variable name starts with
a letter or the dot not followed by a number.
Variable Name Validity Reason

var_name2. valid Has letters, numbers, dot and


underscore

var_name% Invalid Has the character '%'. Only dot(.)


and underscore allowed.

2var_name invalid Starts with a number


valid
Can start with a dot(.) but the
.var_name,
dot(.)should not be followed by a
var.name
number.

.2var_name invalid The starting dot is followed by a


number making it invalid.

_var_name invalid Starts with _ which is not valid


Variable Assignment
• The variables can be assigned values using leftward,
rightward and equal to operator.
• The values of the variables can be printed
using print() or cat() function.
• The cat() function combines multiple items into a
continuous print output.
# Assignment using equal operator.
var.1 = c(0,1,2,3)

# Assignment using leftward operator.


var.2 <- c("learn","R")

# Assignment using rightward operator.


c(TRUE,1) -> var.3

print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")
When we execute the above code, it produces the
following result −

[1] 0 1 2 3
var.1 is 0 1 2 3
var.2 is learn R
var.3 is 1 1

Data Type of a Variable

R is called a dynamically typed language, which means


that we can change a variable’s data type of the same
variable again and again when using it in a program.
In R, a variable itself is not declared of any data type,
rather it gets the data type of the R - object assigned to it.
var_x <- "Hello"
cat("The class of var_x is ",class(var_x),"\n")

var_x <- 34.5


cat(" Now the class of var_x is ",class(var_x),"\n")

var_x <- 27L


cat(" Next the class of var_x becomes ",class(var_x),"\n")

When we execute the above code, it produces the


following result −

The class of var_x is character


Now the class of var_x is numeric
Next the class of var_x becomes integer
Steps in machine learning
Supervised learning
• Supervised learning as the name indicates the
presence of a supervisor as a teacher.
• Basically supervised learning is a learning in which we
teach or train the machine using data which is well
labeled that means some data is already tagged with
the correct answer.
• After that, the machine is provided with a new set of
examples(data) so that supervised learning algorithm
analyses the training data(set of training examples)
and produces a correct outcome from labeled data.
For instance, suppose you are given a basket filled with
different kinds of fruits. Now the first step is to train the
machine with all different fruits one by one like this:

• If shape of object is rounded and depression at top


having color Red then it will be labeled as –Apple.
• If shape of object is long curving cylinder having color
Green-Yellow then it will be labeled as –Banana.
Now suppose after training the data, you have given a new
separate fruit say Apple from basket and asked to identify
it.

Since the machine has already learned the things from


previous data and this time have to use it wisely.
It will first classify the fruit with its shape and color and
would confirm the fruit name as Apple and put it in Apple
category.
Thus the machine learns the things from training
data(basket containing fruits) and then apply the
knowledge to test data(new fruit).
Supervised learning classified into two categories of
algorithms:
• Classification: A classification problem is when the
output variable is a category, such as “Red” or “blue” or
“disease” and “no disease”.
• Regression: A regression problem is when the output
variable is a real value, such as “dollars” or “weight”.

Supervised learning deals with or learns with “labeled”


data.
Which implies that some data is already tagged with the
correct answer.

Types:-
•Regression
•Logistic Regression
•Classification
•Naïve Bayes Classifiers
•Decision Trees
•Support Vector Machine
Advantages:-
• Supervised learning allows collecting data and produce data
output from the previous experiences.
• Helps to optimize performance criteria with the help of
experience.
• Supervised machine learning helps to solve various
types of real-world computation problems.

Disadvantages:-
• Classifying big data can be challenging.
• Training for supervised learning needs a lot of
computation time. So, it requires a lot of time.
Unsupervised learning
• Unsupervised learning is the training of machine using
information that is neither classified nor labeled and
allowing the algorithm to act on that information
without guidance.

• Here the task of machine is to group unsorted


information according to similarities, patterns and
differences without any prior training of data.
• Unlike supervised learning, no teacher is provided that
means no training will be given to the machine.
• Therefore machine is restricted to find the hidden
structure in unlabeled data by our-self.
For instance, suppose it is given an image having both
dogs and cats which have not seen ever.

Thus the machine has no idea about the features of dogs


and cat so we can’t categorize it in dogs and cats.
But it can categorize them according to their similarities,
patterns, and differences i.e., we can easily categorize the
above picture into two parts. First may contain all pics
having dogs in it and second part may contain all pics
having cats in it.
Here you didn’t learn anything before, means no training
data or examples.
It allows the model to work on its own to discover
patterns and information that was previously undetected.
It mainly deals with unlabeled data.

Unsupervised learning classified into two categories of


algorithms:
• Clustering: A clustering problem is where you want to
discover the inherent groupings in the data, such as
grouping customers by purchasing behavior.
• Association: An association rule learning problem is
where you want to discover rules that describe large
portions of your data, such as people that buy X also
tend to buy Y.
Types of Unsupervised Learning:-
Clustering
1.Exclusive (partitioning)
2.Agglomerative
3.Overlapping
4.Probabilistic

Clustering Types:-
1.Hierarchical clustering
2.K-means clustering
3.K-NN (k nearest neighbours)
4.Principal Component Analysis
5.Singular Value Decomposition
6.Independent Component Analysis
Reinforcement learning
• Reinforcement learning is an area of Machine Learning

• It is about taking suitable action to maximize reward in


a particular situation.
• It is employed by various software and machines to
find the best possible behavior or path it should take in
a specific situation.
• Reinforcement learning differs from the supervised
learning in a way that in supervised learning the training
data has the answer key with it so the model is trained
with the correct answer itself whereas in reinforcement
learning, there is no answer but the reinforcement
agent decides what to do to perform the given task.

• In the absence of a training dataset, it is bound to learn


from its experience.
Example: The problem is as follows: We have an agent
and a reward, with many hurdles in between. The agent is
supposed to find the best possible path to reach the
reward. The following problem explains the problem more
easily.

The above image shows the robot, diamond, and fire. The
goal of the robot is to get the reward that is the diamond and
avoid the hurdles that are fire. The robot learns by trying all
the possible paths and then choosing the path which gives
him the reward with the least hurdles. Each right step will
give the robot a reward and each wrong step will subtract
the reward of the robot. The total reward will be calculated
when it reaches the final reward that is the diamond.
Main points in Reinforcement learning –

• Input: The input should be an initial state from which


the model will start
• Output: There are many possible output as there are
variety of solution to a particular problem
• Training: The training is based upon the input, The
model will return a state and the user will decide to
reward or punish the model based on its output.
• The model keeps continues to learn.
• The best solution is decided based on the maximum
reward.
Types of Reinforcement: There are two types of
Reinforcement:
1.Positive –
Positive Reinforcement is defined as when an event, occurs due to a
particular behavior, increases the strength and the frequency of the behavior.
In other words, it has a positive effect on behavior.
Advantages of reinforcement learning are:
1. Maximizes Performance
2. Sustain Change for a long period of time
Disadvantages of reinforcement learning:
3. Too much Reinforcement can lead to overload of states which can
diminish the results
2.Negative –
Negative Reinforcement is defined as strengthening of a behavior because a
negative condition is stopped or avoided.
Advantages of reinforcement learning:
4. Increases Behavior
5. Provide defiance to minimum standard of performance
1.Disadvantages of reinforcement learning:
2. It Only provides enough to meet up the minimum behavior
Various Practical applications of Reinforcement Learning –
• RL can be used in robotics for industrial automation.
• RL can be used in machine learning and data processing
• RL can be used to create training systems that provide custom instruction
and materials according to the requirement of students.

RL can be used in large environments in the following situations:


1. A model of the environment is known, but an analytic solution is not
available;
2. Only a simulation model of the environment is given (the subject of
simulation-based optimization)
3. The only way to collect information about the environment is to interact
with it.
Unsupervised Learning
Supervised Learning

Uses Known and Labeled


Input Data Uses Unknown Input Data
Input Data

Less Computational
Computational Complexity Very Complex in Computation
Complexity

Uses Real Time Analysis of


Real Time Uses off-line analysis
Data

Number of Classes is not


Number of Classes Number of Classes is Known
Known

Accuracy of Results Accurate and Reliable Results Moderate Accurate


K-NN Classifier
K-Nearest Neighbor or K-NN is a Supervised Non-linear
classification algorithm.
K-NN is a Non-parametric algorithm i.e it doesn’t make
any assumption about underlying data or its distribution.
It is one of the simplest and widely used algorithm which
depends on it’s k value(Neighbors) and finds it’s
applications in many industries like finance industry,
healthcare industry etc.
Theory
In the KNN algorithm, K specifies the number of neighbors
and its algorithm is as follows:
• Choose the number K of neighbor.
• Take the K Nearest Neighbor of unknown data point
according to distance.
• Among the K-neighbors, Count the number of data
points in each category.
• Assign the new data point to a category, where you
counted the most neighbors.
For the Nearest Neighbor classifier, the distance between
two points is expressed in the form of Euclidean
Distance.
Example:
Consider a dataset containing two features Red and Blue
and we classify them. Here K is 5 i.e we are considering 5
neighbors according to Euclidean distance.

So, when a new data point enters, out of 5 neighbors, 3


are Blue and 2 are Red. We assign the new data point to
the category with most neighbors i.e Blue.
The Dataset
Iris dataset consists of 50 samples from each of 3 species of Iris(Iris
setosa, Iris virginica, Iris versicolor) and a multivariate dataset
introduced by British statistician and biologist Ronald Fisher in his
1936 paper The use of multiple measurements in taxonomic problems.

Four features were measured from each sample i.e length


and width of the sepals and petals and based on the
combination of these four features, Fisher developed a
linear discriminant model to distinguish the species from
each other.
# Loading data
data(iris)

# Structure
str(iris)
Performing K Nearest Neighbor on Dataset
Using the K-Nearest Neighbor algorithm on the dataset
which includes 11 persons and 6 variables or attributes.
# 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)
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)
classfier_knn
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))
Naïve Bayes Classifier Algorithm
• Naïve Bayes algorithm is a supervised learning algorithm,
which is based on Bayes theorem and used for solving
classification problems.
• It is mainly used in text classification that includes a
high-dimensional training dataset.
• Naïve Bayes Classifier is one of the simple and most
effective Classification algorithms which helps in building
the fast machine learning models that can make quick
predictions.
• It is a probabilistic classifier, which means it
predicts on the basis of the probability of an object.
• Some popular examples of Naïve Bayes Algorithm
are spam filtration, Sentimental analysis, and
classifying
Why is it called Naïve Bayes?
• The Naïve Bayes algorithm is comprised of two
words Naïve and Bayes, Which can be described
as:
• Naïve: It is called Naïve because it assumes
that the occurrence of a certain feature is
independent of the occurrence of other features.
Such as if the fruit is identified on the bases of
color, shape, and taste, then red, spherical, and
sweet fruit is recognized as an apple. Hence
each feature individually contributes to identify
that it is an apple without depending on each
other.
• Bayes: It is called Bayes because it depends on
the principle of Bayes' Theorem.
Bayes' Theorem:
• Bayes' theorem is also known as Bayes'
Rule or Bayes' law, which is used to determine
the probability of a hypothesis with prior
knowledge. It depends on the conditional
probability.
• The formula for Bayes' theorem is given as:

Where,
P(A|B) is Posterior probability: Probability of
hypothesis A on the observed event B.
P(B|A) is Likelihood probability: Probability of
the evidence given that the probability of a
hypothesis is true.
P(A) is Prior Probability: Probability of
hypothesis before observing the evidence.
P(B) is Marginal Probability: Probability of
Evidence.
Working of Naïve Bayes' Classifier:
• Working of Naïve Bayes' Classifier can be understood with the
help of the below example:
• Suppose we have a dataset of weather conditions and
corresponding target variable "Play". So using this dataset we
need to decide that whether we should play or not on a
particular day according to the weather conditions. So to solve
this problem, we need to follow the below steps:

1. Convert the given dataset into frequency tables.


2.Generate Likelihood table by finding the probabilities of given
features.
3.Now, use Bayes theorem to calculate the posterior probability.

• Problem: If the weather is sunny, then the Player should play


or not?
• Solution: To solve this, first consider the below dataset:
Outlook Play

0 Rainy Yes
1 Sunny Yes
2 Overcast Yes
3 Overcast Yes
4 Sunny No
5 Rainy Yes
6 Sunny Yes
7 Overcast Yes
8 Rainy No
9 Sunny No
10 Sunny Yes
11 Rainy No
12 Overcast Yes
13 Overcast Yes
Frequency table for the Weather Conditions:

Weather Yes No
Overcast 5 0
Rainy 2 2
Sunny 3 2
Total 10 5

Likelihood table weather condition:


Weather No Yes
Overcast 0 5 5/14= 0.35
Rainy 2 2 4/14=0.29
Sunny 2 3 5/14=0.35
All 4/14=0.29 10/14=0.71
Applying Bayes'theorem:
P(A)=Yes
P(B)=Sunny

P(Yes|Sunny)= P(Sunny|Yes)*P(Yes)
--------------------------
P(Sunny)

No. of Total Yes condition=10


No.Of Yes condition in sunny=3

P(Sunny|Yes)= 3/10= 0.3=P(B/A)


Total No.of Sunny conditions=5
No. of Total conditions=14
P(Sunny)=5/14=0.35
P(Sunny)= 0.35=P(B)
Total No.of occurances of of yes=10
No. of Total conditions=14
P(Yes)=10/14=0.71
P(Yes)=0.71=P(A)
So P(Yes|Sunny) = 0.3*0.71/0.35= 0.60
P(No|Sunny)= P(Sunny|No)*P(No)
…………………………….
P(Sunny)
P(Sunny|NO)= 2/4=0.5
P(No)= 0.29
P(Sunny)= 0.35
So P(No|Sunny)= 0.5*0.29
………………. = 0.41
0.35
So as we can see from the above calculation
that P(Yes|Sunny)>P(No|Sunny)
Hence on a Sunny day, Player can play the game.
Advantages of Naïve Bayes Classifier:
• Naïve Bayes is one of the fast and easy ML
algorithms to predict a class of datasets.
• It can be used for Binary as well as Multi-class
Classifications.
• It performs well in Multi-class predictions as
compared to the other Algorithms.
• It is the most popular choice for text
classification problems.

Disadvantages of Naïve Bayes


Classifier:
• Naive Bayes assumes that all features are
independent or unrelated, so it cannot learn the
relationship between
Applications features
of Naïve Bayes Classifier:
• It is used for Credit Scoring.
• It is used in medical data classification.
• It can be used in real-time
predictions because Naïve Bayes Classifier is
an eager learner.
• It is used in Text classification such as Spam
filtering and Sentiment analysis.
Types of Naïve Bayes Model:
There are three types of Naive Bayes Model, which are given below:
• Gaussian:
• The Gaussian model assumes that features follow a normal
distribution.
• This means if predictors take continuous values instead of discrete,
then the model assumes that these values are sampled from the
Gaussian distribution.
• Multinomial: The Multinomial Naïve Bayes classifier is used when the
data is multinomial distributed.
• It is primarily used for document classification problems, it means a
particular document belongs to which category such as Sports, Politics,
education, etc.
The classifier uses the frequency of words for the predictors.
• Bernoulli:
• The Bernoulli classifier works similar to the Multinomial classifier, but
the predictor variables are the independent Booleans variables. Such
as if a particular word is present or not in a document. This model is
also famous for document classification tasks.
Support Vector Machine Algorithm
• Support Vector Machine or SVM is one of the
most popular Supervised Learning algorithms,
which is used for Classification as well as
Regression problems. However, primarily, it is
used for Classification problems in Machine
Learning.
• The goal of the SVM algorithm is to create the
best line or decision boundary that can
segregate n-dimensional space into classes so
that we can easily put the new data point in the
correct category in the future. This best decision
boundary is called a hyperplane.
• SVM chooses the extreme points/vectors that
help in creating the hyperplane. These extreme
cases are called as support vectors, and hence
algorithm is termed as Support Vector Machine.
Consider the below diagram in which there are
two different categories that are classified using
a decision boundary or hyperplane:
What is Support Vector Machine?
The objective of the support vector machine algorithm is to find a
hyperplane in an N-dimensional space(N — the number of features) that
distinctly classifies the data points.

Possible hyperplanes
• To separate the two classes of data points, there are many possible
hyperplanes that could be chosen. Our objective is to find a plane that
has the maximum margin, i.e the maximum distance between data
points of both classes. Maximizing the margin distance provides some
reinforcement so that future data points can be classified with more
confidence.
Hyperplanes and Support Vectors
Hyperplanes are decision boundaries that help classify the data points. Data
points falling on either side of the hyperplane can be attributed to different
classes. Also, the dimension of the hyperplane depends upon the number of
features. If the number of input features is 2, then the hyperplane is just a
line. If the number of input features is 3, then the hyperplane becomes a
two-dimensional plane. It becomes difficult to imagine when the number of
features exceeds 3.

Support vectors are data points that are closer to the hyperplane and
influence the position and orientation of the hyperplane. Using these support
vectors, we maximize the margin of the classifier. Deleting the support
vectors will change the position of the hyperplane. These are the points that
help us build our SVM.
Advantages:
1. SVM works relatively well when there is a clear margin
of separation between classes.
2. SVM is more effective in high dimensional spaces.
3. SVM is effective in cases where the number of
dimensions is greater than the number of samples.
4. SVM is relatively memory efficient

Disadvantages:
1. SVM algorithm is not suitable for large data sets.
2. SVM does not perform very well when the data set has
more noise i.e. target classes are overlapping.
3. In cases where the number of features for each data
point exceeds the number of training data samples, the
SVM will underperform.
4. As the support vector classifier works by putting data
points, above and below the classifying hyperplane
there is no probabilistic explanation for the
classification.

You might also like