Table Of ContentEgo autem et domus
mea serviemus Domino.
MACHINE LEARNING
MADE EASY
With R
An Intuitive Step by Step Blueprint for
Beginners
Dr. N.D Lewis
Copyright © 2017 by N.D. Lewis
All rights reserved. No part of this publication may be reproduced, dis-
tributed, or transmitted in any form or by any means, including photo-
copying, recording, or other electronic or mechanical methods, without
thepriorwrittenpermissionoftheauthor,exceptinthecaseofbriefquo-
tationsembodiedincriticalreviewsandcertainothernoncommercialuses
permitted by copyright law. For permission requests, contact the author
at: www.AusCov.com.
Disclaimer: Although the author and publisher have made every effort to
ensure that the information in this book was correct at press time, the
author and publisher do not assume and hereby disclaim any liability to
anypartyforanyloss,damage,ordisruptioncausedbyerrorsoromissions,
whether such errors or omissions result from negligence, accident, or any
other cause.
Ordering Information: Quantity sales. Special discounts are available on
quantity purchases by corporations, associations, and others. For details,
email: info@NigelDLewis.com
Image photography by Deanna Lewis with helpful assistance
from Naomi Lewis.
ISBN-13: 978-1546483755
ISBN-10: 1546483756
Contents
Acknowledgements iii
Preface viii
1 Introduction to Machine Learning 1
WhoisthisBookfor? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
WhatisMachineLearning? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
HowtoGettheAbsoluteMostPossibleBenefitfromthisBook . . . . . . . . 5
GettingR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
LearningR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
UsingPackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
EffectiveUseofFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Decision Trees 11
UnderstandingDecisionTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
TheDivideandConquerAlgorithm . . . . . . . . . . . . . . . . . . . . 14
UnderstandingEntropy . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CalculatingEntropy-AStepbyStepExample . . . . . . . . . . . . . . 18
AdvantagesofDecisionTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
PracticalApplicationofDecisionTrees. . . . . . . . . . . . . . . . . . . . . . 24
IntelligentShoes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
MicroRibonucleicAcid . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
TrafficAccidents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Example-IdentifyingCounterfeitCurrency . . . . . . . . . . . . . . . . . . . 29
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 30
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 32
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 33
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 36
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 37
LimitationsofDecisionTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3 k-Nearest Neighbors 47
UnderstandingNearestNeighbors. . . . . . . . . . . . . . . . . . . . . . . . . 48
MeasuringClosenesswithDistanceMetrics . . . . . . . . . . . . . . . . 48
AnExampletoBoostYourUnderstanding. . . . . . . . . . . . . . . . . 50
Advantagesofk-NearestNeighbors . . . . . . . . . . . . . . . . . . . . . 53
i
PracticalApplicationofk-NearestNeighbors . . . . . . . . . . . . . . . . . . 53
LeafClassification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
ClassifyingHumanActivity . . . . . . . . . . . . . . . . . . . . . . . . . 55
IdentifyingAnimalBehavior. . . . . . . . . . . . . . . . . . . . . . . . . 55
Example-IdentifyingWineCultivars . . . . . . . . . . . . . . . . . . . 57
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 57
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 60
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 62
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 63
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 64
Limitationsofk-NearestNeighbors . . . . . . . . . . . . . . . . . . . . . . . . 65
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4 Naive Bayes Classifier 69
UnderstandingtheNaiveBayesClassifier . . . . . . . . . . . . . . . . . . . . 70
TheCoreConceptofBayesRule . . . . . . . . . . . . . . . . . . . . . . 70
TheBayesClassifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
CoreAssumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
AStepbyStepExampletoBuildYourIntuition . . . . . . . . . . . . . 74
AdvantagesoftheNaiveBayesClassifier . . . . . . . . . . . . . . . . . . . . . 77
PracticalApplicationoftheNaiveBayesClassifier . . . . . . . . . . . . . . . 77
GestureRecognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
MedicalResearch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
PredictingTrafficCongestion . . . . . . . . . . . . . . . . . . . . . . . . 79
Example-ClassifyingSimulationData . . . . . . . . . . . . . . . . . . . . . 80
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 81
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 83
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 83
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 84
Step5-AssessTestSetPerformance . . . . . . . . . . . . . . . . . . . 86
Example-IdentifyingRadarSignals . . . . . . . . . . . . . . . . . . . . . . 87
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 87
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 90
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 92
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 93
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 94
LimitationsoftheNaiveBayesClassifier . . . . . . . . . . . . . . . . . . . . . 98
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5 Linear Discriminant Analysis 101
UnderstandingLinearDiscriminantAnalysis . . . . . . . . . . . . . . . . . . 102
AClarifyingIllustration . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
LinearDiscriminantAnalysisinaNutshell. . . . . . . . . . . . . . . . . 104
AdvantagesofLinearDiscriminantAnalysis . . . . . . . . . . . . . . . . 104
PracticalApplicationofLinearDiscriminantAnalysis . . . . . . . . . . . . . 105
FoodAuthentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
GradingChickenFillets . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
AutismSpectrumClassification . . . . . . . . . . . . . . . . . . . . . . . 108
Example-MolecularClassificationviaGeneExpression . . . . . . . . . . . . 109
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 109
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 113
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 117
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 122
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 124
LimitationsofLinearDiscriminantAnalysis . . . . . . . . . . . . . . . . . . . 126
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6 Linear Regression 129
UnderstandingLinearRegression . . . . . . . . . . . . . . . . . . . . . . . . . 130
SimpleLinearRegression . . . . . . . . . . . . . . . . . . . . . . . . . . 130
ClarifyingExpectedValue . . . . . . . . . . . . . . . . . . . . . . . . . . 131
ExplainingOrdinaryLeastSquares . . . . . . . . . . . . . . . . . . . . . 134
CoefficientofDetermination . . . . . . . . . . . . . . . . . . . . . . . . . 135
MultipleRegression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
AssumptionsoftheLinearRegressionModel . . . . . . . . . . . . . . . 138
AdvantagesofLinearRegression . . . . . . . . . . . . . . . . . . . . . . 139
PracticalApplicationofLinearRegression . . . . . . . . . . . . . . . . . . . . 140
SoilDensityPrediction. . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
AgeFriendlyCities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
ForeignDirectInvestmentinNepal . . . . . . . . . . . . . . . . . . . . . 144
Example-PredictingtheLengthofPaleolithicHand-Axes . . . . . . . . . . 145
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 147
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 149
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 152
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . . 154
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 173
LimitationsofLinearRegression . . . . . . . . . . . . . . . . . . . . . . . . . 178
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
7 Logistic Regression 181
UnderstandingLogisticRegression . . . . . . . . . . . . . . . . . . . . . . . . 182
OddsRatio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
TheLogisticCurve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
MaximumLikelihoodEstimation . . . . . . . . . . . . . . . . . . . . . . 188
AsaBayesClassifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
AdvantagesofLogisticRegression. . . . . . . . . . . . . . . . . . . . . . 189
PracticalApplicationofLogisticRegression . . . . . . . . . . . . . . . . . . . 190
CustomerChurn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
OpinionMining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
ZoonoticDiseaseClassification . . . . . . . . . . . . . . . . . . . . . . . 193
Example-ClassifyingPurpleRockCrabs . . . . . . . . . . . . . . . . . . . . 194
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 194
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 197
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 197
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 201
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 210
LimitationsofLogisticRegression . . . . . . . . . . . . . . . . . . . . . . . . 218
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
8 Support Vector Machines 221
UnderstandingSupportVectorMachines. . . . . . . . . . . . . . . . . . . . . 222
ExplainingtheOptimalHyperplane . . . . . . . . . . . . . . . . . . . . 223
AdvantagesofSupportVectorMachines . . . . . . . . . . . . . . . . . . 226
PracticalApplicationofSupportVectorMachines . . . . . . . . . . . . . . . 226
ForecastingtheStockMarket . . . . . . . . . . . . . . . . . . . . . . . . 226
IdentificationofEarlyOnsetBreastCancer . . . . . . . . . . . . . . . . 227
SignatureAuthentication . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Example-ClassifyingDiabetes . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 229
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 233
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 234
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 239
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . . 240
LimitationsofSupportVectorMachines . . . . . . . . . . . . . . . . . . . . . 243
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
9 Random Forests 247
UnderstandingRandomForests . . . . . . . . . . . . . . . . . . . . . . . . . 248
TheRandomForestsAlgorithm . . . . . . . . . . . . . . . . . . . . . . . 248
AdvantagesofRandomForests . . . . . . . . . . . . . . . . . . . . . . . 250
PracticalApplicationofRandomForests. . . . . . . . . . . . . . . . . . . . . 250
Waste-waterDeteriorationModeling . . . . . . . . . . . . . . . . . . . . 251
GlaucomaPrediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
ObesityRiskFactors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Example-ClassifyingThyroidStatus . . . . . . . . . . . . . . . . . . . . . . 252
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 253
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 255
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 255
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 255
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 257
LimitationsofRandomForests . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
10Boosting 263
UnderstandingBoosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
TheBasicApproach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
TheAdaboostAlgorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 265
AdvantagesofBoosting . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
PracticalApplicationofBoosting . . . . . . . . . . . . . . . . . . . . . . . . . 268
ReverberationSuppression. . . . . . . . . . . . . . . . . . . . . . . . . . 268
BasketballPlayerDetection . . . . . . . . . . . . . . . . . . . . . . . . . 269
VehicleLogoRecognition . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Example-ClassifyingSonarSignals . . . . . . . . . . . . . . . . . . . . . . . 270
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 271
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 272
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 273
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 274
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 275
LimitationsofBoosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
11K- Means Clustering 279
UnderstandingK-MeansClustering . . . . . . . . . . . . . . . . . . . . . . . 280
HowK-meansClusteringWorks . . . . . . . . . . . . . . . . . . . . . . 281
AdvantagesofK-MeansClustering. . . . . . . . . . . . . . . . . . . . . 285
PracticalApplicationofK-MeansClustering . . . . . . . . . . . . . . . . . . 286
PinpointingtheRasberryCrazyAnt . . . . . . . . . . . . . . . . . . . . 286
AutomaticShags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
IdentifyingFibromyalgiaSubgroups . . . . . . . . . . . . . . . . . . . . 290
Example-Example-ClassifyingtheWell-beingofCitizens . . . . . . . . . . 292
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 293
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 295
Step3-TrainModelusingSampleData. . . . . . . . . . . . . . . . . . 296
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 297
Step5-ImprovingModelPerformance . . . . . . . . . . . . . . . . . . 298
LimitationsofK-MeansClustering. . . . . . . . . . . . . . . . . . . . . . . . 307
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
12Tips to Enhance Performance 309
ClarifyingOccam’sRazor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Usefulinterpretations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
ModelClarity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
UnderstandingDataSnooping. . . . . . . . . . . . . . . . . . . . . . . . . . . 311
TheDangerofSnooping . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
TheBenefitofaValidationSet . . . . . . . . . . . . . . . . . . . . . . . 312
PracticalLessonsoftheNoFreeLunchTheorem . . . . . . . . . . . . . . . . 313
Don’tFallinLove . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
AllModelsAreLimited . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
WorkwithDomainExperts . . . . . . . . . . . . . . . . . . . . . . . . . 315
ExplainingtheBiasVarianceTrade-off . . . . . . . . . . . . . . . . . . . . . . 316
InterpretingReducibleError. . . . . . . . . . . . . . . . . . . . . . . . . 316
SimplifyingBias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
IlluminatingVariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
TheSecretoftheHoldOutTechnique . . . . . . . . . . . . . . . . . . . . . . 319
TheArtofEffectiveCrossValidation. . . . . . . . . . . . . . . . . . . . . . . 322
k-foldCrossValidation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
ChoosingtheNumberofFolds . . . . . . . . . . . . . . . . . . . . . . . 323
LeaveOneOutCrossValidation . . . . . . . . . . . . . . . . . . . . . . 323
Example-ClassifyingPurpleRockCrabswithNaiveBayes . . . . . . . . . . 324
Step1-CollectingandExploringtheData . . . . . . . . . . . . . . . . 324
Step2-PreparingtheData. . . . . . . . . . . . . . . . . . . . . . . . . 324
Step3-TrainModelusingTrainSet. . . . . . . . . . . . . . . . . . . . 325
Step4-EvaluateModelPerformance . . . . . . . . . . . . . . . . . . . 326
Step5-AssessingTestSetPerformance . . . . . . . . . . . . . . . . . . 327
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
SuggestedReading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Index 336
Description:Finally, A Blueprint for Machine Learning with R!Machine Learning Made Easy with R offers a practical tutorial that uses hands-on examples to step through real-world applications using clear and practical case studies. Through this process it takes you on a gentle, fun and unhurried journey to creat