R语言之决策树和随机森林

Python016

R语言之决策树和随机森林,第1张

R语言之决策树和随机森林

总结决策树之前先总结一下特征的生成和选择,因为决策树就是一种内嵌型的特征选择过程,它的特征选择和算法是融合在一起的,不需要额外的特征选择。

一、特征生成:

特征生成是指在收集数据之时原始数据就具有的数据特征,这些数据特征由收集的数据决定(其实也就是在产品定型时设定的需要收集的数据特征),当然,在数据预处理时,也可以在此基础上构造一些新的数据特征,这些特征越多越好,表示你考虑问题比较周全,具体那些变量有用或没用,这要交给下一步特征选择来决定。

二、特征选择

特征选择是指在原有数据特征的基础上,去除重要性比较低的特征变量,过滤出有用的特征变量。这里比较困难的是搞清楚什么样的特征比较重要?这需要根据具体的问题具体分析,有些变量的选择可以很直观的看出来,但这种直觉也不一定正确。对于常用特征选择方法主要有:过滤型、包装型、内嵌型。

过滤型:是指你可以根据某个统计量的大小排序来选择特征变量,如相关系数、p值、R值等

包装型:是指在一个特征集合中选取最优的特征子集。具体需要考虑:用什么样的算法来选取?选取的最优的标准是什么?

常用的算法是分步回归包括向前搜索、向后删除、双向搜索

向前搜索:每次选取一个能使模型预测或分类效果最好的特征变量进来,进来后不退出,直到模型改善效果不再明显;

向后删除:是指每次从特征全集中每次删除一个特征变量能使模型预测或分类效果最好,退出后不进来,直到模型改善效果不再明显;

双向搜索:是指每次每次删除一个特征变量或加入一个特征变量能使模型预测或分类效果最好,退出的不进来,进来的不退出,直到模型改善效果不再明显;

这里再提一下特征变量选择的几个标准:p值、R值、AIC(越小效果越好)、BIC(越小效果越好)、熵(越小效果越好)

内嵌型:这里应该主要就是像决策树这样的情况,算法内部完成特征变量的选取。

三、决策树

决策的几个要点:1、如何决策?(也就是如何树如何分叉)------熵和信息增益---这里面包含的就是特征的选择?哪个特征变量包含的信息量大,就排在前面,至于最后树的深度就决定特征变量的个数。

当然不同的算法使用的衡量的标准不同,还有:信息增益比、基尼不纯系数

2、如何剪枝?-----一般是事后剪枝

3、连续性变量如何离散化?-----阈值的选择

熵:是指信息的混合程度(混乱程度),熵【0-1】越大表示该集合中混合的信息越多,也就表明这次的分叉效果不好还是有很多不同类的信息混在一起

信息增益:熵值的减少量,越大越好

决策树模型特点:模型易于解释;存储空间较小,以树的形式存储,决策树是一个弱分类器,不能完全分类,需要把多个弱分类器通过多数投票法组合在一起。

四、R包实现决策树

library(rpart)

library(rpart.plot)

## rpart.control对树进行一些设置

## xval是10折交叉验证

## minsplit是最小分支节点数,这里指大于等于20,那么该节点会继续分划下去,否则停止

## minbucket:叶子节点最小样本数

## maxdepth:树的深度

## cp全称为complexity parameter,指某个点的复杂度,对每一步拆分,模型的拟合优度必须提高的程度

ct <- rpart.control(xval=10, minsplit=20, cp=0.1)

## kyphosis是rpart这个包自带的数据集

## na.action:缺失数据的处理办法,默认为删除因变量缺失的观测而保留自变量缺失的观测。

## method:树的末端数据类型选择相应的变量分割方法:

## 连续性method=“anova”,离散型method=“class”,计数型method=“poisson”,生存分析型method=“exp”

## parms用来设置三个参数:先验概率、损失矩阵、分类纯度的度量方法(gini和information)

## cost是损失矩阵,在剪枝的时候,叶子节点的加权误差与父节点的误差进行比较,考虑损失矩阵的时候,从将“减少-误差”调整为“减少-损失”

data("Kyphosis")

fit <- rpart(Kyphosis~Age + Number + Start,data=kyphosis, method="class",control=ct,parms = list(prior = c(0.65,0.35), split = "information"))

## 作图有2种方法

## 第一种:

par(mfrow=c(1,3))plot(fit)text(fit,use.n=T,all=T,cex=0.9)

## 第二种,这种会更漂亮一些:

rpart.plot(fit, branch=1, branch.type=2, type=1, extra=102,

shadow.col="gray", box.col="green",

border.col="blue", split.col="red",

split.cex=1.2, main="Kyphosis决策树")

## rpart包提供了复杂度损失修剪的修剪方法,printcp会告诉分裂到每一层,cp是多少,平均相对误差是多少

## 交叉验证的估计误差(“xerror”列),以及标准误差(“xstd”列),平均相对误差=xerror±xstd

printcp(fit)

## 通过上面的分析来确定cp的值

##调用CP(complexity parameter)与xerror的相关图,一种方法是寻找最小xerror点所对应

#的CP值,并由此CP值决定树的大小,另一种方法是利用1SE方法,寻找xerror+SE的最小点对应的CP值。

plotcp(fit)

##利用以下方法进行修剪:

## prune(fit, cp= fit$cptable[which.min(fit$cptable[,"xerror"]),"CP"])

fit2 <- prune(fit, cp=0.01)

#利用模型预测

ndata=data.frame(...)

predict(fit,newdata=ndata)

#案例

str(iris)

set.seed(1234)#设置随机数种子--使每次运行时产生的一组随机数相同,便于结果的重现

#抽样:从iris数据集中随机抽70%定义为训练数据集,30%为测试数据集(常用)

#这里是对行抽样,ind是一个只含1和2的向量

ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.7, 0.3))

trainData <- iris[ind==1,]

testData <- iris[ind==2,]

f<-Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width

#训练数据

fit<-rpart(f,trainData)

#预测

re<-predict(fit,testData)

#******************或者用其他包********************

library(party)

#建立决策树模型预测花的种类

myFormula <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width

iris_ctree <- ctree(myFormula, data=trainData)

# 查看预测的结果

z<-table(predict(iris_ctree), trainData$Species)

#可以根据以上列联表求出预测的正确率---评估模型

#计算准确度

q<-sum(diag(z))/sum(z)

五、机器集成与随机森林法则

前面说过,决策树的一个特点是:弱分类器,分类不完全,需要利用集成投票的方式来增加精确度和稳健性。

机器集成算法:对于数据集训练多个模型,对于分类问题,可以采用投票的方法,选择票数最多的类别作为最终的类别,而对于回归问题,可以采用取均值的方法,取得的均值作为最终的结果。主要的集成算法有bagging和adaboost算法。

随机森林:随机森林就是利用机器集成多个决策树,主要有两个参数,一个是决策树的个数,一个是每棵树的特征变量个数。

随机森林特点:精确度高、稳健性好,但可解释性差。(可以知道各个变量的重要性)

R包实现机器集成算法:

#adabag包均有函数实现bagging和adaboost的分类建模

#利用全部数据建模

library(adabag)

a<-boosting(Species~.,data=iris)

z0<-table(iris[,5],predict(a,iris)$class)

#计算误差率

E0<-(sum(z0)-sum(diag(z0)))/sum(z0)

barplot(a$importance)

b<-errorevol(a,iris)#计算全体的误差演变

plot(b$error,type="l",main="AdaBoost error vs number of trees") #对误差演变进行画图

a<-bagging(Species~.,data=iris)

z0<-table(iris[,5],predict(a,iris)$class)

#计算误差率

E0<-(sum(z0)-sum(diag(z0)))/sum(z0)

barplot(a$importance)

b<-errorevol(a,iris)#计算全体的误差演变

plot(b$error,type="l",main="AdaBoost error vs number of trees") #对误差演变进行画图

#5折交叉验证

set.seed(1044) #设定随机种子

samp=c(sample(1:50,25),sample(51:100,25),sample(101:150,25)) #进行随机抽样

a=boosting(Species~.,data=iris[samp,]) #利用训练集建立adaboost分类模

z0<-table(iris[samp,5],predict(a,iris[samp,])$class)#训练集结果

z1<-table(iris[-samp,5],predict(a,iris[-samp,])$class)#测试集结果

E0<-(sum(z0)-sum(diag(z0)))/sum(z0)

E1<-(sum(z0)-sum(diag(z0)))/sum(z1)

a=bagging(Species~.,data=iris[samp,]) #利用训练集建立adaboost分类模

z0<-table(iris[samp,5],predict(a,iris[samp,])$class)#训练集结果

z1<-table(iris[-samp,5],predict(a,iris[-samp,])$class)#测试集结果

E0<-(sum(z0)-sum(diag(z0)))/sum(z0)

E1<-(sum(z0)-sum(diag(z0)))/sum(z1)

R包实现随机森林:

#随机森林法则

library(randomForest)

library(foreign)

data("iris")

#抽样数据

ind<-sample(2,nrow(iris),replace = TRUE,prob=c(0.7,0.3))

traning<-iris[ind==1,]

testing<-iris[ind==2,]

#训练数据

rf <- randomForest(Species ~ ., data=traning, ntree=100, proximity=TRUE)

#预测

table(predict(rf),traning$Species)

table(predict(rf,testing),testing$Species)

#查看预测的效果

print(rf)

plot(rf)

#查看重要性

importance(rf)

varImpPlot(rf)

1、K最近邻(k-NearestNeighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一。该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别。

2、KNN算法中,所选择的邻居都是已经正确分类的对象。该方法在定类决策上只依据最邻近的一个或者几个样本的类别来决定待分样本所属的类别。 KNN方法虽然从原理上也依赖于极限定理,但在类别决策时,只与极少量的相邻样本有关。由于KNN方法主要靠周围有限的邻近的样本,而不是靠判别类域的方法来确定所属类别的,因此对于类域的交叉或重叠较多的待分样本集来说,KNN方法较其他方法更为适合。

3、KNN算法不仅可以用于分类,还可以用于回归。通过找出一个样本的k个最近邻居,将这些邻居的属性的平均值赋给该样本,就可以得到该样本的属性。更有用的方法是将不同距离的邻居对该样本产生的影响给予不同的权值(weight),如权值与距离成正比。

简言之,就是将未标记的案例归类为与它们最近相似的、带有标记的案例所在的类 。

原理及举例

工作原理:我们知道样本集中每一个数据与所属分类的对应关系,输入没有标签的新数据后,将新数据与训练集的数据对应特征进行比较,找出“距离”最近的k(通常k<20)数据,选择这k个数据中出现最多的分类作为新数据的分类。

算法描述

1、计算已知数据集中的点与当前点的距离

2、按距离递增次序排序

3、选取与当前数据点距离最近的K个点

4、确定前K个点所在类别出现的频率

5、返回频率最高的类别作为当前类别的预测

距离计算方法有"euclidean"(欧氏距离),”minkowski”(明科夫斯基距离), "maximum"(切比雪夫距离), "manhattan"(绝对值距离),"canberra"(兰式距离), 或 "minkowski"(马氏距离)等

Usage

knn(train, test, cl, k = 1, l = 0, prob =FALSE, use.all = TRUE)

Arguments

train

matrix or data frame of training set cases.

test

matrix or data frame of test set cases. A vector will  be interpreted as a row vector for a single case.

cl

factor of true classifications of training set

k

number of neighbours considered.

l

minimum vote for definite decision, otherwisedoubt. (More precisely, less thank-ldissenting votes are allowed, even

ifkis  increased by ties.)

prob

If this is true, the proportion of the votes for the

winning class are returned as attributeprob.

use.all

controls handling of ties. If true, all distances equal

to thekth largest are

included. If false, a random selection of distances equal to thekth is chosen to use exactlykneighbours.

kknn(formula = formula(train), train, test, na.action = na.omit(), k = 7, distance = 2, kernel = "optimal", ykernel = NULL, scale=TRUE, contrasts = c('unordered' = "contr.dummy", ordered = "contr.ordinal"))

参数:

formula                            A formula object.

train                                 Matrix or data frame of training set cases.

test                                   Matrix or data frame of test set cases.

na.action                         A function which indicates what should happen when the data contain ’NA’s.

k                                       Number of neighbors considered.

distance                          Parameter of Minkowski distance.

kernel                              Kernel to use. Possible choices are "rectangular" (which is standard unweighted knn), "triangular", "epanechnikov" (or beta(2,2)), "biweight" (or beta(3,3)), "triweight" (or beta(4,4)), "cos", "inv", "gaussian", "rank" and "optimal".

ykernel                            Window width of an y-kernel, especially for prediction of ordinal classes.

scale                                Logical, scale variable to have equal sd.

contrasts                         A vector containing the ’unordered’ and ’ordered’ contrasts to use

kknn的返回值如下:

fitted.values              Vector of predictions.

CL                              Matrix of classes of the k nearest neighbors.

W                                Matrix of weights of the k nearest neighbors.

D                                 Matrix of distances of the k nearest neighbors.

C                                 Matrix of indices of the k nearest neighbors.

prob                            Matrix of predicted class probabilities.

response                   Type of response variable, one of continuous, nominal or ordinal.

distance                     Parameter of Minkowski distance.

call                              The matched call.

terms                          The ’terms’ object used.

iris%>%ggvis(~Length,~Sepal.Width,fill=~Species)

library(kknn)

data(iris)

dim(iris)

m<-(dim(iris))[1]

val<-sample(1:m,size=round(m/3),replace=FALSE,prob=rep(1/m,m))

建立训练数据集

data.train<-iris[-val,]

建立测试数据集

data.test<-iris[val,]

调用kknn  之前首先定义公式

formula : Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width

iris.kknn<-kknn(Species~.,iris.train,iris.test,distance=1,kernel="triangular")

summary(iris.kknn)

# 获取fitted.values

fit <- fitted(iris.kknn)

# 建立表格检验判类准确性

table(iris.valid$Species, fit)

# 绘画散点图,k-nearest neighbor用红色高亮显示

pcol <- as.character(as.numeric(iris.valid$Species))

pairs(iris.valid[1:4], pch = pcol, col = c("green3", "red")[(iris.valid$Species != fit)+1]

二、R语言knn算法

install.packages("class")

library(class)

对于新的测试样例基于距离相似度的法则,确定其K个最近的邻居,在K个邻居中少数服从多数

确定新测试样例的类别

1、获得数据

2、理解数据

对数据进行探索性分析,散点图

如上例

3、确定问题类型,分类数据分析

4、机器学习算法knn

5、数据处理,归一化数据处理

normalize <- function(x){

num <- x - min(x)

denom <- max(x) - min(x)

return(num/denom)

}

iris_norm <-as.data.frame(lapply(iris[,1:4], normalize))

summary(iris_norm)

6、训练集与测试集选取

一般按照3:1的比例选取

方法一、set.seed(1234)

ind <- sample(2,nrow(iris), replace=TRUE, prob=c(0.67, 0.33))

iris_train <-iris[ind==1, 1:4]

iris_test <-iris[ind==2, 1:4]

train_label <-iris[ind==1, 5]

test_label <-iris[ind==2, 5]

方法二、

ind<-sample(1:150,50)

iris_train<-iris[-ind,]

iris_test<-iris[ind,1:4]

iris_train<-iris[-ind,1:4]

train_label<-iris[-ind,5]

test_label<-iris[ind,5]

7、构建KNN模型

iris_pred<-knn(train=iris_train,test=iris_test,cl=train_label,k=3)

8、模型评价

交叉列联表法

table(test_label,iris_pred)

实例二

数据集

http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data

导入数据

dir <-'http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data'wdbc.data <-read.csv(dir,header = F)

names(wdbc.data) <- c('ID','Diagnosis','radius_mean','texture_mean','perimeter_mean','area_mean','smoothness_mean','compactness_mean','concavity_mean','concave points_mean','symmetry_mean','fractal dimension_mean','radius_sd','texture_sd','perimeter_sd','area_sd','smoothness_sd','compactness_sd','concavity_sd','concave points_sd','symmetry_sd','fractal dimension_sd','radius_max_mean','texture_max_mean','perimeter_max_mean','area_max_mean','smoothness_max_mean','compactness_max_mean','concavity_max_mean','concave points_max_mean','symmetry_max_mean','fractal dimension_max_mean')

table(wdbc.data$Diagnosis)## M = malignant, B = benign

wdbc.data$Diagnosis <- factor(wdbc.data$Diagnosis,levels =c('B','M'),labels = c(B ='benign',M ='malignant'))