在大数据如火如荼的时候,机器学习无疑成为了炙手可热的工具,机器学习是计算机科学和统计学的交叉学科,
旨在通过收集和分析数据的基础上,建立一系列的算法,模型对实际问题进行预测或分类。
R语言无疑为我们提供了很好的工具,它正是计算机科学和统计科学结合的产物,开源免费,
相对于Python、Orange Canvas、Weka、Kinme这些免费的数据挖掘软件来说,更容易上手,统计图形也更加美观。
今天在这里和大家介绍一下Caret机器学习包的一些基本用法。
 
一、数据收集
      下载kernlab包里的spam数据集,spam是一个邮件数据集,共有4601个观测值,58个变量,最后一个变量是一个二值变量,“spam”和“no spam”,我们要做的工作就是通过建立模型了预测观测值是否为“spam”。首先加载软件包和数据集:
 
> library(caret)
载入需要的程辑包:lattice
载入需要的程辑包:ggplot2
警告信息:
1: 程辑包‘caret’是用R版本3.1.1 来建造的 
2: 程辑包‘ggplot2’是用R版本3.1.1 来建造的 
> library(kernlab)
警告信息:
程辑包‘kernlab’是用R版本3.1.3 来建造的 
> data(spam)
> head(spam)
  make address  all num3d  our over remove internet order mail
1 0.00    0.64 0.64     0 0.32 0.00   0.00     0.00  0.00 0.00
2 0.21    0.28 0.50     0 0.14 0.28   0.21     0.07  0.00 0.94
3 0.06    0.00 0.71     0 1.23 0.19   0.19     0.12  0.64 0.25
4 0.00    0.00 0.00     0 0.63 0.00   0.31     0.63  0.31 0.63
5 0.00    0.00 0.00     0 0.63 0.00   0.31     0.63  0.31 0.63
6 0.00    0.00 0.00     0 1.85 0.00   0.00     1.85  0.00 0.00
  receive will people report addresses free business email  you
1    0.00 0.64   0.00   0.00      0.00 0.32     0.00  1.29 1.93
2    0.21 0.79   0.65   0.21      0.14 0.14     0.07  0.28 3.47
3    0.38 0.45   0.12   0.00      1.75 0.06     0.06  1.03 1.36
4    0.31 0.31   0.31   0.00      0.00 0.31     0.00  0.00 3.18
5    0.31 0.31   0.31   0.00      0.00 0.31     0.00  0.00 3.18
6    0.00 0.00   0.00   0.00      0.00 0.00     0.00  0.00 0.00
  credit your font num000 money hp hpl george num650 lab labs telnet
1   0.00 0.96    0   0.00  0.00  0   0      0      0   0    0      0
2   0.00 1.59    0   0.43  0.43  0   0      0      0   0    0      0
3   0.32 0.51    0   1.16  0.06  0   0      0      0   0    0      0
4   0.00 0.31    0   0.00  0.00  0   0      0      0   0    0      0
5   0.00 0.31    0   0.00  0.00  0   0      0      0   0    0      0
6   0.00 0.00    0   0.00  0.00  0   0      0      0   0    0      0
  num857 data num415 num85 technology num1999 parts pm direct cs
1      0    0      0     0          0    0.00     0  0   0.00  0
2      0    0      0     0          0    0.07     0  0   0.00  0
3      0    0      0     0          0    0.00     0  0   0.06  0
4      0    0      0     0          0    0.00     0  0   0.00  0
5      0    0      0     0          0    0.00     0  0   0.00  0
6      0    0      0     0          0    0.00     0  0   0.00  0
  meeting original project   re  edu table conference charSemicolon
1       0     0.00       0 0.00 0.00     0          0          0.00
2       0     0.00       0 0.00 0.00     0          0          0.00
3       0     0.12       0 0.06 0.06     0          0          0.01
4       0     0.00       0 0.00 0.00     0          0          0.00
5       0     0.00       0 0.00 0.00     0          0          0.00
6       0     0.00       0 0.00 0.00     0          0          0.00
  charRoundbracket charSquarebracket charExclamation charDollar
1            0.000                 0           0.778      0.000
2            0.132                 0           0.372      0.180
3            0.143                 0           0.276      0.184
4            0.137                 0           0.137      0.000
5            0.135                 0           0.135      0.000
6            0.223                 0           0.000      0.000
  charHash capitalAve capitalLong capitalTotal type
1    0.000      3.756          61          278 spam
2    0.048      5.114         101         1028 spam
3    0.010      9.821         485         2259 spam
4    0.000      3.537          40          191 spam
5    0.000      3.537          40          191 spam
6    0.000      3.000          15           54 spam
 
二、数据划分
      机器学习一般将数据划分成训练数据、验证数据(可选)、测试数据、三个部分,训练数据和验证数据用来训练模型,估计模型的具体参数,测试数据用来验证模型预测的准确程度。下面我们就对spam这个数据进行划分
inTrain <- createDataPartition(y=spam$type,p=0.75,list=FALSE)
training <- spam[inTrain, ]
testing <- spam[-inTrain, ]
nrow(training)
[1] 3451
nrow(testing)
[1] 1150
 
以上命令中createDataPartition( )就是数据划分函数,对象是spam$typ,p=0.75表示训练数据所占的比例为75%,list是输出结果的格式,默认list=FALSE。 training <- spam[inTrain, ],testing <- spam[-inTrain, ]分别制定具体的训练数据和测试数据。
 
三、训练模型
       以上的工作完成后就可以将训练数据放入训练器中对模型参数进行训练了
modelFit <- train(type~.,data=training,method="glm") train( )函数就是我们的训练器,type~是回归方程,data=training指定数据集,method="glm"指定具体的模型形式,这里我们用的是glm估计,当然读者也可以用SVM(支持向量机),nnet神经网络等其他模型形式,以下是模型的具体内容:
modelFit$finalModel
Coefficients:
(Intercept) make address all num3d 
-1.989e+00 -5.022e-01 -1.702e-01 1.553e-01 3.368e+00 
our over remove internet order 
7.554e-01 6.682e-01 2.220e+00 5.586e-01 1.144e+00 
mail receive will people report 
Degrees of Freedom: 3450 Total (i.e. Null); 3393 Residual
Null Deviance: 4628 
Residual Deviance: 1335 AIC: 1451(篇幅有限,中间有删减)
 
四、验证模型
       当模型的参数全部训练完毕后,就要将测试数据带入模型中进行验证预测了
predictions <- predict(modelFit,newdata=testing)
predictions####预测结果如下
[1] spam spam spam spam spam spam spam spam spam spam spam 
[12] spam spam spam spam spam spam spam spam spam spam spam 
[23] nonspam spam spam spam spam spam spam nonspam spam spam spam 
[34] spam spam spam spam spam spam spam spam spam spam spam 
[45] spam spam spam spam spam spam spam spam spam spam spam 
 
五、错误分类矩阵
      想知道模型预测的准确率如何呢?这个时候就要用到错误分类矩阵了,将模型预测的值和真实的值进行比较,计算错误分类率。通过观察错误分类矩阵,我们可知准确率为0.9252,结果还是很理想的。
 
confusionMatrix(predictions,testing$type)####输出结果如下
 
Confusion Matrix and Statistics
 
Reference
Prediction nonspam spam
nonspam 658 47
spam 39 406
 
Accuracy : 0.9252 
95% CI : (0.9085, 0.9398)
No Information Rate : 0.6061 
P-Value [Acc > NIR] : <2e-16 
 
Kappa : 0.8429 
Mcnemar's Test P-Value : 0.4504 
 
Sensitivity : 0.9440 
Specificity : 0.8962 
Pos Pred Value : 0.9333 
Neg Pred Value : 0.9124 
Prevalence : 0.6061 
Detection Rate : 0.5722 
Detection Prevalence : 0.6130 
Balanced Accuracy : 0.9201 
 
 
实例2:
library(caret)
library(mlbench)
data(Sonar)
set.seed(107)
inTrain<-createDataPartition(y = Sonar$Class,##the outcome data are needed
p=.75,##The percentage of data in the training set
list = FALSE##the format of the results
)
#The output is a set of integers for the rows of Sonar
#that belong in the training set.
> str(inTrain)
 int [1:157, 1] 98 100 101 102 103 105 107 109 110 111 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "Resample1"
 
> training <- Sonar[inTrain,]
> testing <- Sonar[-inTrain,]
> nrow(training)
[1] 157
 
> nrow(testing)
[1] 51
 
1)
library(pls)
plsFit <- train(Class~.,data = training,
method = 'pls',#Center and scale the predictors for the training set and all future samples,
preProc = c("center","scale"))
plot(plsFit)
2)
plsFit <- train(Class~.,data = training,
method = 'pls',
tuneLength = 15,
preProc = c("center","scale"))
plot(plsFit)
3)
ctrl <-trainControl(method = "repeatedcv",repeats=3)
plsFit <- train(Class~.,data = training,
method = 'pls',
tuneLength = 15,
trControl = ctrl,
preProc = c("center","scale"))
plot(plsFit)
4)
ctrl <- trainControl(method = "repeatedcv",repeats=3,
classProbs = TRUE,
summaryFunction = twoClassSummary)
plsFit <-train(Class~.,
data = training,
tuneLength = 15,
trControl = ctrl,
metric = "ROC",
preProc = C("center","scale"))
 
> plsFit
Partial Least Squares 
 
157 samples
 60 predictor
  2 classes: 'M', 'R' 
 
Pre-processing: centered, scaled 
Resampling: Cross-Validated (10 fold, repeated 3 times) 
 
Summary of sample sizes: 141, 141, 142, 141, 140, 142, ... 
 
Resampling results across tuning parameters:
 
  ncomp  Accuracy  Kappa  Accuracy SD  Kappa SD
   1     0.729     0.460  0.1291       0.254   
   2     0.807     0.614  0.0896       0.176   
   3     0.788     0.577  0.0880       0.176   
   4     0.780     0.558  0.0783       0.158   
   5     0.757     0.512  0.0953       0.193   
   6     0.762     0.524  0.0925       0.185   
   7     0.752     0.504  0.0943       0.188   
   8     0.739     0.477  0.0743       0.148   
   9     0.745     0.491  0.0861       0.170   
  10     0.747     0.493  0.0791       0.156   
  11     0.736     0.472  0.0845       0.167   
  12     0.758     0.514  0.0887       0.177   
  13     0.730     0.458  0.0883       0.176   
  14     0.734     0.466  0.0916       0.182   
  15     0.743     0.483  0.0964       0.193   
 
Accuracy was used to select the optimal model using  the largest value.
The final value used for the model was ncomp = 2. 
 
 
>  plsClasses <- predict(plsFit,newdata = testing)
> str(plsClasses)
 Factor w/ 2 levels "M","R": 2 1 1 2 1 2 2 2 2 2 ...
> plsProbs <- predict(plsFit,newdata = testing,type = "prob")
> head(plsProbs)
           M         R
4  0.3762529 0.6237471
5  0.5229047 0.4770953
8  0.5839468 0.4160532
16 0.3660142 0.6339858
20 0.7351013 0.2648987
25 0.2135788 0.7864212
> confusionMatrix(data = plsClasses,testing$Class)
Confusion Matrix and Statistics
 
          Reference
Prediction  M  R
         M 20  7
         R  7 17
                                          
               Accuracy : 0.7255          
                 95% CI : (0.5826, 0.8411)
    No Information Rate : 0.5294          
    P-Value [Acc > NIR] : 0.003347        
                                          
                  Kappa : 0.4491          
 Mcnemar's Test P-Value : 1.000000        
                                          
            Sensitivity : 0.7407          
            Specificity : 0.7083          
         Pos Pred Value : 0.7407          
         Neg Pred Value : 0.7083          
             Prevalence : 0.5294          
         Detection Rate : 0.3922          
   Detection Prevalence : 0.5294          
      Balanced Accuracy : 0.7245          
                                          
       'Positive' Class : M       
 
 

R语言机器学习之caret包运用的更多相关文章

  1. R语言:利用caret包中的dummyVars函数进行虚拟变量处理

    dummyVars函数:dummyVars creates a full set of dummy variables (i.e. less than full rank parameterizati ...

  2. R语言︱机器学习模型评估方案(以随机森林算法为例)

    笔者寄语:本文中大多内容来自<数据挖掘之道>,本文为读书笔记.在刚刚接触机器学习的时候,觉得在监督学习之后,做一个混淆矩阵就已经足够,但是完整的机器学习解决方案并不会如此草率.需要完整的评 ...

  3. R语言︱机器学习模型评价指标+(转)模型出错的四大原因及如何纠错

    笔者寄语:机器学习中交叉验证的方式是主要的模型评价方法,交叉验证中用到了哪些指标呢? 交叉验证将数据分为训练数据集.测试数据集,然后通过训练数据集进行训练,通过测试数据集进行测试,验证集进行验证. 模 ...

  4. R语言中的数据处理包dplyr、tidyr笔记

    R语言中的数据处理包dplyr.tidyr笔记   dplyr包是Hadley Wickham的新作,主要用于数据清洗和整理,该包专注dataframe数据格式,从而大幅提高了数据处理速度,并且提供了 ...

  5. r语言,安装外部包 警告: 无法将临时安装

    安装R语言中的外部包时,出现错误提示 试开URL’https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/contrib/3.3/ggplot2_2 ...

  6. R语言—如何安装Github包的解决方法,亲测有效

    R语言—如何安装Github包的解决方法,亲测有效 准备安装材料: R包-REmap GitHub下载地址:https://github.com/lchiffon/REmap R包-baidumap ...

  7. R语言:关于rJava包的安装

    R语言:关于rJava包的安装  盐池里的萝卜 2014-09-14 00:53:33 在做文本挖掘的时候,会发现分词时候rJava是必须要迈过去的坎儿,所以进行了总结: 第一步:安装rJava和jd ...

  8. R语言︱常用统计方法包+机器学习包(名称、简介)

    一.一些函数包大汇总 转载于:http://www.dataguru.cn/thread-116761-1-1.html 时间上有点过期,下面的资料供大家参考基本的R包已经实现了传统多元统计的很多功能 ...

  9. R 语言机器学习同步推进~

    教材就是传说中的机器学习和R语言--中文版,大家可以去图书馆借来看看~~~,例子都是来自书上的 首先介绍一下KNN算法,KNN还好吧,说白了就是一个算距离的公式然后以统计的方式呈现出来,以二维平面为例 ...

随机推荐

  1. AOP学习笔记一

    软件开发的目的是为了解决各种需求,包括业务需求和系统需求.目前,业界通过使用面向对象的编程思想,已经可以对业务需求等普通关注点进行很好的抽象与封装,并且使之模块化.但是对于系统需求一类的关注点来说,情 ...

  2. Java异常处理机制 —— 深入理解与开发应用

    本文为原创博文,严禁转载,侵权必究! Java异常处理机制在日常开发中应用频繁,其最主要的不外乎几个关键字:try.catch.finally.throw.throws,以及各种各样的Exceptio ...

  3. c++标准库容器【转】

    C++最原始的容器之一是数组.数组的特点有: 1.大小固定 2.单独存在的数组建立在栈上,作为对象成员存在的数组建立在堆上还是栈上则要看作为宿主对象是被建立在堆上还是栈上.栈空间是有限的,所以如果数组 ...

  4. 前端代码组织优化--小demo(进阶你的思路)

    事出必有因 最近在看老项目的代码,一个富客户端的js代码,几千行的代码,全是function(){} var...的垂直布局,真的是要感动的哭了. 一开始都是这样,想实现什么功能,不管三七二十一,fu ...

  5. SQL Server函数​---Union与Union All的区别

    SQL Server函数---Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称 ...

  6. .NET插件技术-应用程序热升级

    今天说一说.NET 中的插件技术,即 应用程序热升级.在很多情况下.我们希望用户对应用程序的升级是无感知的,并且尽可能不打断用户操作的. 虽然在Web 或者 WebAPI上,由于多点的存在可以逐个停用 ...

  7. vue.js随笔记---初识Vue.js

    1.基础要求: 1.1 HTML CSS JAVASCRIPT 1.2 模块化基础 1.3 Es6初步了解 2.vue.js 轻量级的MVVM模式框架,他同时吸收了recat和angular的优点,他 ...

  8. 读书笔记 effective c++ Item 53 关注编译器发出的警告

    许多程序员常常忽略编译器发出的警告.毕竟,如果问题很严重,它才将会变成一个error,不是么?相对来说,这个想法可能在其它语言是无害的,但是在C++中,我敢打赌编译器的实现者对于对接下来会发生什么比你 ...

  9. 简单的jquery左侧导航栏和页面选中效果

    这里是要实现导航的左侧并选中的,此功能需引用jquery 效果: 左侧导航 <div class="box"> <ul class="menu" ...

  10. AOJ/数据结构习题集

    ALDS1_3_A-Stack. Description: Write a program which reads an expression in the Reverse Polish notati ...