R分类

34 篇文章 1 订阅
12 篇文章 0 订阅
分类
传统意义下的分类:生物物种
预测:天气预报
决策:yes or no
分类的传统模型
常见分类模型与算法
 线性判别法
 距离判别法
 贝叶斯分类器
 决策树
 支持向量机(SVM)

 神经网络

线性判别法(Fisher)
G=c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2)
x1=c(-1.9,-6.9,5.2,5.0,7.3,6.8,0.9,-12.5,1.5,3.8,0.2,-0.1,0.4,2.7,2.1,-4.6,-1.7,-2.6,2.6,-2.8)
x2=c(3.2,0.4,2.0,2.5,0.0,12.7,-5.4,-2.5,1.3,6.8,6.2,7.5,14.6,8.3,0.8,4.3,10.9,13.1,12.8,10.0)
a=data.frame(G,x1,x2)
plot(x1,x2)
text(x1,x2,G,adj=-0.5) #显示G的值在x1,x2坐标系
用一条直线来划分学习集
MASS包与线性判别函数lda( )
library(MASS)
ld=lda(G~x1+x2)
ld
分类判断
z=predict(ld)
newG=z$class
newG
y=cbind(G,z$x,newG)
y
距离判别法
原理:计算待测点与各类的距离,取最短者为其所属分类
马氏距离(薛毅书p445,为什么不用欧氏距离?),计算函数mahalanobis( )

discriminiant.distance <- function
(TrnX1, TrnX2, TstX = NULL, var.equal = FALSE){
  if (is.null(TstX) == TRUE) TstX <- rbind(TrnX1,TrnX2)
  if (is.vector(TstX) == TRUE) TstX <- t(as.matrix(TstX))
  else if (is.matrix(TstX) != TRUE)
    TstX <- as.matrix(TstX)
  if (is.matrix(TrnX1) != TRUE) TrnX1 <- as.matrix(TrnX1)
  if (is.matrix(TrnX2) != TRUE) TrnX2 <- as.matrix(TrnX2)
  nx <- nrow(TstX)
  blong <- matrix(rep(0, nx), nrow=1, byrow=TRUE,
                  dimnames=list("blong", 1:nx))
  mu1 <- colMeans(TrnX1); mu2 <- colMeans(TrnX2)
  if (var.equal == TRUE || var.equal == T){
    S <- var(rbind(TrnX1,TrnX2))
    w <- mahalanobis(TstX, mu2, S)
    - mahalanobis(TstX, mu1, S)
  }
  else{
    S1 < -var(TrnX1); S2 <- var(TrnX2)
    w <- mahalanobis(TstX, mu2, S2)
    - mahalanobis(TstX, mu1, S1)
  }
  for (i in 1:nx){
    if (w[i] > 0)
      blong[i] <- 1
    else
      blong[i] <- 2
  }
  blong
}
source("discriminiant.distance.R")
classX1<-data.frame(
  x1=c(6.60, 6.60, 6.10, 6.10, 8.40, 7.2, 8.40, 7.50,
       7.50, 8.30, 7.80, 7.80),
  x2=c(39.00,39.00, 47.00, 47.00, 32.00, 6.0, 113.00, 52.00,
       52.00,113.00,172.00,172.00),
  x3=c(1.00, 1.00, 1.00, 1.00, 2.00, 1.0, 3.50, 1.00,
       3.50, 0.00, 1.00, 1.50),
  x4=c(6.00, 6.00, 6.00, 6.00, 7.50, 7.0, 6.00, 6.00,
       7.50, 7.50, 3.50, 3.00),
  x5=c(6.00, 12.00, 6.00, 12.00, 19.00, 28.0, 18.00, 12.00,
       6.00, 35.00, 14.00, 15.00),
  x6=c(0.12, 0.12, 0.08, 0.08, 0.35, 0.3, 0.15, 0.16,0.16, 0.12, 0.21, 0.21),
  x7=c(20.00,20.00, 12.00, 12.00, 75.00, 30.0, 75.00, 40.00,
       40.00,180.00, 45.00, 45.00)
)
classX2<-data.frame(
  x1=c(8.40, 8.40, 8.40, 6.3, 7.00, 7.00, 7.00, 8.30,
       8.30, 7.2, 7.2, 7.2, 5.50, 8.40, 8.40, 7.50,
       7.50, 8.30, 8.30, 8.30, 8.30, 7.80, 7.80),
  x2=c(32.0 ,32.00, 32.00, 11.0, 8.00, 8.00, 8.00,161.00,
       161.0, 6.0, 6.0, 6.0, 6.00,113.00,113.00, 52.00,
       52.00, 97.00, 97.00,89.00,56.00,172.00,283.00),
  x3=c(1.00, 2.00, 2.50, 4.5, 4.50, 6.00, 1.50, 1.50,
       0.50, 3.5, 1.0, 1.0, 2.50, 3.50, 3.50, 1.00,
       1.00, 0.00, 2.50, 0.00, 1.50, 1.00, 1.00),
  x4=c(5.00, 9.00, 4.00, 7.5, 4.50, 7.50, 6.00, 4.00,
       2.50, 4.0, 3.0, 6.0, 3.00, 4.50, 4.50, 6.00,
       7.50, 6.00, 6.00, 6.00, 6.00, 3.50, 4.50),
  x5=c(4.00, 10.00, 10.00, 3.0, 9.00, 4.00, 1.00, 4.00,
       1.00, 12.0, 3.0, 5.0, 7.00, 6.00, 8.00, 6.00,
       8.00, 5.00, 5.00,10.00,13.00, 6.00, 6.00),
  x6=c(0.35, 0.35, 0.35, 0.2, 0.25, 0.25, 0.25, 0.08,
       0.08, 0.30, 0.3, 0.3, 0.18, 0.15, 0.15, 0.16,
       0.16, 0.15, 0.15, 0.16, 0.25, 0.21, 0.18),
  x7=c(75.00,75.00, 75.00, 15.0,30.00, 30.00, 30.00, 70.00,
       70.00, 30.0, 30.0, 30.0,18.00, 75.00, 75.00, 40.00,
       40.00,180.00,180.00,180.00,180.00,45.00,45.00)
)
discriminiant.distance(classX1, classX2, var.equal=TRUE)
贝叶斯分类器
原理(薛毅书p455)
利用贝叶斯分类器判断垃圾邮件


决策树 decision tree
输入:学习集
输出:分类规则(决策树)
ID3算法

library(rpart)
iris.rp = rpart(Species~., data=iris,method="class")
plot(iris.rp, uniform=T, branch=0,margin=0.1, main="ClassificationTree\nIris Species by Petal and SepalLength")
text(iris.rp, use.n=T, fancy=T, col="blue")
Rule 1: if Petal.Length>=2.45&Petal.Width<1.75, then it is versicolor(0/49/5)
Rule2: if Petal.Length>=2.45&Petal.Width>=1.75, then it is virginica (0/1/45)
Rule 3: if Petal.Length<2.45, then it is setosa (50/0/0)

Knn算法
1 选取k个和待分类点距离最近的样本点
2 看1中的样本点的分类情况,投票决定待分类点所属的类

人工神经网络  感知学习算法
使用R语言实现人工神经网络
install.packages("AMORE")
library(AMORE)
P <- matrix(sample(seq(-1,1,length=1000), 1000, replace=FALSE), ncol=1)
target <- P^2
net <- newff(n.neurons=c(1,3,2,1), learning.rate.global=1e-2, momentum.global=0.5,error.criterium="LMS", Stao=NA, hidden.layer="tansig",output.layer="purelin", method="ADAPTgdwm")
result <- train(net, P, target, error.criterium="LMS", report=TRUE, show.step=100, n.shows=5 )
y <- sim(result$net, P)
plot(P,y, col="blue", pch="+")
points(P,target, col="red", pch="x")
改造样例代码,解决之前的问题
x1=c(1,1,1,1,0,0,0,0)
x2=c(0,0,1,1,0,1,1,0)
x3=c(0,1,0,1,1,0,1,0)
y=c(-1,1,1,1,-1,-1,1,-1)
P=cbind(x1,x2,x3)
target=y
net <- newff(n.neurons=c(3,1,1), learning.rate.global=1e-2,momentum.global=0.4,error.criterium="LMS", Stao=NA, hidden.layer="tansig",output.layer="purelin", method="ADAPTgdwm")
result <- train(net, P, target, error.criterium="LMS", report=TRUE, show.step=100,n.shows=5 )
z <- sim(result$net, P)

y
用BP神经网络处理非线性拟合问题
Hopfield神经网络
OCR的思路
参考书
 《 神经网络设计》 ,机械工业出版社
 《 神经网络与机器学习》 ,机械工业出版社
 《 人工神经网络理论、设计及应用》 ,化学工业出版社
 《 MATLAB神经网络30个案例分析》 ,北京航空航天大学出版社


支持向量机 SVM
最优分离平面(决策边界)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值