R语言与数据分析

时间:2022-03-31 16:32:31

K最近邻(k-Nearest Neighbor,KNN)分类算法

  R语言实现包:R语言中有kknn package实现了weighted k-nearest neighbor。

决策树:

  R语言实现决策树:rpart扩展包

iris.rp = rpart(Species~.,data = iris,method = "class")
print(iris.rp)#输出模型结果

node), split, n, loss, yval, (yprob)
    * denotes terminal node

1) root 150 100 setosa (0.33333333 0.33333333 0.33333333)
  2) Petal.Length< 2.45 50 0 setosa (1.00000000 0.00000000 0.00000000) *
  3) Petal.Length>=2.45 100 50 versicolor (0.00000000 0.50000000 0.50000000)
    6) Petal.Width< 1.75 54 5 versicolor (0.00000000 0.90740741 0.09259259) *
    7) Petal.Width>=1.75 46 1 virginica (0.00000000 0.02173913 0.97826087) *

plot(iris.rp,uniform = T,branch =  ,margin = 0.1)
test(iris.rp,use.n=T,fancy = T,col="blue")