Machine Learning in R
apolol92
Overview
Linear Regression Model
Decision Tree Model
K Means Clustering
K-Nearest Neighbour(knn)
Support Vector Machine(SVM)
Naive Bayes
Linear Regression Model
# women heights and weights
str(women)
# Create regression model to predict the weight of a given height
model <-lm(data = women, weight ~ height)
# Predict weight of height=50
predict(model,newdata=data.frame(heights=50))
plot(x=weights, y=heights) # Plot weights ~ heights
abline(model) # Plot regression line
Decision Tree Model
install.packages(“party”) # Install package party
library(“party”) # Use party
str(iris) # We will use the iris dataset
# Create the Decision Tree Model to predict species
iris_tree <- ctree(data = iris, Species~Sepal.Length+Sepal.Width+Petal.Length
+Petal.Width)
predict(iris,newdata=iris[1,1:4]) # Predict species of first iris in data frame
plot(iris_tree) # Plot the Decision Tree
print(iris_tree) # Print the Decision Tree
K Means Clustering
install.packages("clue") # Use clue package
library("clue")
model <- kmeans(iris[,1:2],3) # Create K Means Model
test_data <- data.frame(c(4,2)) # Create test data
cl_predict(model, test_data) # predict cluster of test data
# Just a way to plot clusters
plot(iris[,1:2],col=cl_predict(party,iris[,1:2]))
K-Nearest Neighbour(knn)
install.packages("kknn")
library("kknn")
my_iris <- iris[sample(150),] # Randomize iris data
train_data <- my_iris[1:100,-6] # Get some training data
test_data <- my_iris[101:150,1:4] # Get test data
# Create K-Nearest Neighbour model
my_knn <- kknn(formula = formula(Species~.), train = train_data,
test = test_data,k=7,distance=1)
predict(my_knn, test_data) #Predict Species
Support Vector Machines
install.packages('e1071')
library('e1071')
model <- svm(iris$Species~.,data=iris[,c(1,2,5)) # Create SVM-Model
plot(model,iris[, c(1, 2, 5)])
test_data <- data.frame(cbind(c(4,2)) # Create test data
colnames(test_data) <- c("Sepal.Width","Sepal.Length") # Set colnames
predict(model,test_data) # Predict species
Naive Bayes
install.packages(‘e1071’)
library(‘e1071’)
model <- naiveBayes(x=iris[,1:4],y=iris[,5]) # Create Naive Bayes Model
predict(model,iris[1,-5]) # Predict
species
Neural Network(MLP)
install.packages(‘RSNNS’)
library(RSNNS)
iris[,5] <- as.numeric(iris[,5]) # Cast factor to numeric
model <- mlp(iris[,-5],iris[,5]) # Create default MLP
predict(model,c(2,3,4,1)) # Predict species

Machine learning in R

  • 1.
  • 2.
    Overview Linear Regression Model DecisionTree Model K Means Clustering K-Nearest Neighbour(knn) Support Vector Machine(SVM) Naive Bayes
  • 3.
    Linear Regression Model #women heights and weights str(women) # Create regression model to predict the weight of a given height model <-lm(data = women, weight ~ height) # Predict weight of height=50 predict(model,newdata=data.frame(heights=50)) plot(x=weights, y=heights) # Plot weights ~ heights abline(model) # Plot regression line
  • 4.
    Decision Tree Model install.packages(“party”)# Install package party library(“party”) # Use party str(iris) # We will use the iris dataset # Create the Decision Tree Model to predict species iris_tree <- ctree(data = iris, Species~Sepal.Length+Sepal.Width+Petal.Length +Petal.Width) predict(iris,newdata=iris[1,1:4]) # Predict species of first iris in data frame plot(iris_tree) # Plot the Decision Tree print(iris_tree) # Print the Decision Tree
  • 5.
    K Means Clustering install.packages("clue")# Use clue package library("clue") model <- kmeans(iris[,1:2],3) # Create K Means Model test_data <- data.frame(c(4,2)) # Create test data cl_predict(model, test_data) # predict cluster of test data # Just a way to plot clusters plot(iris[,1:2],col=cl_predict(party,iris[,1:2]))
  • 6.
    K-Nearest Neighbour(knn) install.packages("kknn") library("kknn") my_iris <-iris[sample(150),] # Randomize iris data train_data <- my_iris[1:100,-6] # Get some training data test_data <- my_iris[101:150,1:4] # Get test data # Create K-Nearest Neighbour model my_knn <- kknn(formula = formula(Species~.), train = train_data, test = test_data,k=7,distance=1) predict(my_knn, test_data) #Predict Species
  • 7.
    Support Vector Machines install.packages('e1071') library('e1071') model<- svm(iris$Species~.,data=iris[,c(1,2,5)) # Create SVM-Model plot(model,iris[, c(1, 2, 5)]) test_data <- data.frame(cbind(c(4,2)) # Create test data colnames(test_data) <- c("Sepal.Width","Sepal.Length") # Set colnames predict(model,test_data) # Predict species
  • 8.
    Naive Bayes install.packages(‘e1071’) library(‘e1071’) model <-naiveBayes(x=iris[,1:4],y=iris[,5]) # Create Naive Bayes Model predict(model,iris[1,-5]) # Predict species
  • 9.
    Neural Network(MLP) install.packages(‘RSNNS’) library(RSNNS) iris[,5] <-as.numeric(iris[,5]) # Cast factor to numeric model <- mlp(iris[,-5],iris[,5]) # Create default MLP predict(model,c(2,3,4,1)) # Predict species