INTRODUCTION
Dear reader,
If y'all are a newbie inwards the footing of machine learning, thence this tutorial is precisely what y'all require inwards social club to innovate yourself to this exciting novel component of the information scientific discipline world.
This ship includes a total machine learning projection that volition conduct y'all measuring yesteryear measuring to do a “template,” which y'all tin laissez passer on the sack purpose afterwards on other datasets.
Before proceeding, delight follow our short tutorial.
Look at the examples given as well as endeavour to sympathise the logic behind them. Then endeavour to solve the exercises below using R as well as without looking at the answers. Then run into the solutions to banking concern check your answers.
Look at the examples given as well as endeavour to sympathise the logic behind them. Then endeavour to solve the exercises below using R as well as without looking at the answers. Then run into the solutions to banking concern check your answers.
Exercise 1
Create a listing named “control” that runs a 10-fold cross-validation. HINT: Use
trainControl()
. Exercise 2
Use the metric of “Accuracy” to evaluate models.
Exercise 3
Build the “LDA”, “CART”, “kNN”, “SVM” as well as “RF” models.
Exercise 4
Create a listing of the five models y'all only built as well as cite it “results”. HINT: Use
resamples()
. Exercise 5
Report the accuracy of each model yesteryear using the summary business office on the listing “results”. HINT: Use
summary()
. Exercise 6
Create a plot of the model evaluation results as well as compare the spread as well as the hateful accuracy of each model. HINT: Use
dotplot()
. Exercise 7
Which model seems to last the most accurate?
Exercise 8
Summarize the results of the best model as well as impress them. HINT: Use
print()
. Exercise 9
Run the “LDA” model straight on the validation prepare to do a element named “predictions”. HINT: Use
predict()
. Exercise 10
Summarize the results inwards a confusion matrix. HINT: Use
confusionMatrix()
. __________________________________________
Below are the solutions to these exercises on applying machine learning to your dataset.
#################### # # # Exercise 1 # # # #################### install.packages("caret") library(caret) data(iris) validation <- createDataPartition(iris$Species, p=0.80, list=FALSE) validation20 <- iris[-validation,] iris <- iris[validation,] library(caret) control <- trainControl(method="cv", number=10) #################### # # # Exercise ii # # # #################### library(caret) control <- trainControl(method="cv", number=10) metric <- "Accuracy" #################### # # # Exercise three # # # #################### install.packages("rpart") install.packages("kernlab") install.packages("e1071") install.packages("randomForest") library(caret) library(rpart) library(kernlab) library(e1071) library(randomForest) # a) linear algorithms set.seed(7) fit.lda <- train(Species ., data=iris, method="lda", metric=metric, trControl=control) # b) nonlinear algorithms # CART set.seed(7) fit.cart <- train(Species ., data=iris, method="rpart", metric=metric, trControl=control) # kNN set.seed(7) fit.knn <- train(Species ., data=iris, method="knn", metric=metric, trControl=control) # c) advanced algorithms # SVM set.seed(7) fit.svm <- train(Species ., data=iris, method="svmRadial", metric=metric, trControl=control) # Random Forest set.seed(7) fit.rf <- train(Species ., data=iris, method="rf", metric=metric, trControl=control) #################### # # # Exercise iv # # # #################### library(caret) library(rpart) library(kernlab) library(e1071) library(randomForest) results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf)) #################### # # # Exercise five # # # #################### library(caret) library(rpart) library(kernlab) library(e1071) library(randomForest) results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf)) summary(results) #################### # # # Exercise half dozen # # # #################### library(caret) library(rpart) library(kernlab) library(e1071) library(randomForest) dotplot(results) #################### # # # Exercise seven # # # #################### #LDA #################### # # # Exercise 8 # # # #################### library(caret) library(rpart) library(kernlab) library(e1071) library(randomForest) print(fit.lda) #################### # # # Exercise nine # # # #################### library(caret) library(rpart) library(kernlab) library(e1071) library(randomForest) predictions <- predict(fit.lda, validation20) #################### # # # Exercise 10 # # # #################### library(caret) library(rpart) library(kernlab) library(e1071) library(randomForest) predictions <- predict(fit.lda, validation20) confusionMatrix(predictions, validation20$Species)
http://www.r-exercises.com/2017/09/15/applying-machine-learning-algorithms-exercises/
http://www.r-exercises.com/2017/09/15/applying-machine-learning-algorithms-exercises-solutions/
http://www.r-exercises.com/2017/09/15/applying-machine-learning-algorithms-exercises-solutions/