iris数据集预测(对比随机森林和逻辑回归算法)

随机森林

library(randomForest)

#挑选响应变量

index <- subset(iris,Species != "setosa")
ir <- droplevels(index)

set.seed(1)

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

train<-ir[ind==1,]

test<-ir[ind==2,]

rf<-randomForest(Species~.,data=train,ntree=100)

rf

Call:
 randomForest(formula = Species ~ ., data = train, ntree = 100)
               Type of random forest: classification
                     Number of trees: 100
No. of variables tried at each split: 2         OOB estimate of  error rate: 5.88%
Confusion matrix:
           versicolor virginica class.error
versicolor         32         2  0.05882353
virginica           2        32  0.05882353

#随机森林的误差率

plot(rf)

#变量重要性

importance(rf)

importance(rf)
             MeanDecreaseGini
Sepal.Length        1.4398647
Sepal.Width         0.7037353
Petal.Length       11.1734509
Petal.Width        20.1025569

varImpPlot(rf)

#查看预测结果

pred<-predict(rf,newdata=test)

table(pred,test$Species)

pred         versicolor virginica
  versicolor         15         2
  virginica           1        14

#预测边距

plot(margin(rf,test$Species))

逻辑回归

library(pROC)​

g1<-glm(Species~.,family=binomial(link='logit'),data=train)​

pre1<-predict(g1,type="response")

g1

Call:  glm(formula = Species ~ ., family = binomial(link = "logit"),
data = train) Coefficients:
(Intercept) Sepal.Length Sepal.Width Petal.Length Petal.Width
-32.01349 -3.85855 -0.02084 6.65355 14.08817 Degrees of Freedom: Total (i.e. Null); Residual
Null Deviance: 94.27
Residual Deviance: 8.309 AIC: 18.31 summary(g1) Call:
glm(formula = Species ~ ., family = binomial(link = "logit"),
    data = train) Deviance Residuals:
     Min        1Q    Median        3Q       Max  
-1.73457  -0.02241  -0.00011   0.03691   1.76243   Coefficients:
              Estimate Std. Error z value Pr(>|z|)  
(Intercept)  -32.01349   28.51193  -1.123   0.2615  
Sepal.Length  -3.85855    3.16430  -1.219   0.2227  
Sepal.Width   -0.02084    4.85883  -0.004   0.9966  
Petal.Length   6.65355    5.47953   1.214   0.2246  
Petal.Width   14.08817    7.32507   1.923   0.0544 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 (Dispersion parameter for binomial family taken to be 1)     Null deviance: 94.268  on 67  degrees of freedom
Residual deviance:  8.309  on 63  degrees of freedom
AIC: 18.309 Number of Fisher Scoring iterations: 9

#方差分析

anova(g1,test="Chisq")
Analysis of Deviance Table Model: binomial, link: logit Response: Species Terms added sequentially (first to last) Df Deviance Resid. Df Resid. Dev Pr(>Chi)
NULL 94.268
Sepal.Length 14.045 80.223 0.0001785 ***
Sepal.Width 0.782 79.441 0.3764212
Petal.Length 62.426 17.015 2.766e-15 ***
Petal.Width 8.706 8.309 0.0031715 **
---
Signif. codes: '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' '

#计算最优阀值

modelroc1<-roc(as.factor(ifelse(train$Species=="virginica",1,0)),pre1)

plot(modelroc1,print.thres=TRUE)

评估模型的预测效果

predict <-predict(g1,type="response",newdata=test)​​

predict.results <-ifelse(predict>0.804,"virginica","versicolor")​

misClasificError <-mean(predict.results !=test$Species)

print(paste("Accuracy:",1-misClasificError))
[1] "Accuracy: 0.90625"

XGBoost

y<-data.matrix(as.data.frame(train$Species))-1

x<-data.matrix(train[-5])

bst <- xgboost(data =x, label = y, max.depth = , eta = ,nround = , objective = "binary:logistic")

[]    train-error:0.029412
[] train-error:0.029412

p<-predict(bst,newdata=data.matrix(test))

modelroc2<-roc(as.factor(ifelse(test$Species=="virginica",1,0)),p)

plot(modelroc2)

predict.results <-ifelse(p>0.11,"virginica","versicolor")

misClasificError <-mean(predict.results !=test$Species)

print(paste(-misClasificError))
[] "0.90625"

iris数据集预测的更多相关文章

  1. 机器学习笔记2 – sklearn之iris数据集

    前言 本篇我会使用scikit-learn这个开源机器学习库来对iris数据集进行分类练习. 我将分别使用两种不同的scikit-learn内置算法--Decision Tree(决策树)和kNN(邻 ...

  2. 机器学习——logistic回归,鸢尾花数据集预测,数据可视化

    0.鸢尾花数据集 鸢尾花数据集作为入门经典数据集.Iris数据集是常用的分类实验数据集,由Fisher, 1936收集整理.Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集.数据集包含150个数 ...

  3. Iris数据集实战

    本次主要围绕Iris数据集进行一个简单的数据分析, 另外在数据的可视化部分进行了重点介绍. 环境 win8, python3.7, jupyter notebook 目录 1. 项目背景 2. 数据概 ...

  4. 从Iris数据集开始---机器学习入门

    代码多来自<Introduction to Machine Learning with Python>. 该文集主要是自己的一个阅读笔记以及一些小思考,小总结. 前言 在开始进行模型训练之 ...

  5. 85、使用TFLearn实现iris数据集的分类

    ''' Created on 2017年5月21日 @author: weizhen ''' #Tensorflow的另外一个高层封装TFLearn(集成在tf.contrib.learn里)对训练T ...

  6. 用Python实现支持向量机并处理Iris数据集

    SVM全称是Support Vector Machine,即支持向量机,是一种监督式学习算法.它主要应用于分类问题,通过改进代码也可以用作回归.所谓支持向量就是距离分隔面最近的向量.支持向量机就是要确 ...

  7. iris数据集(.csv .txt)免费下载

    我看CSDN下载的iris数据集都需要币,我愿意免费共享,希望下载后的朋友们给我留个言 分享iris数据集(供学习使用): 链接: https://pan.baidu.com/s/1Knsp7zn-C ...

  8. R语言实现分层抽样(Stratified Sampling)以iris数据集为例

    R语言实现分层抽样(Stratified Sampling)以iris数据集为例 1.观察数据集 head(iris) Sampling)以iris数据集为例">  选取数据集中前6个 ...

  9. KNN算法实现对iris数据集的预测

    KNN算法的实现 import pandas as pd from math import dist k = int(input("请输入k值:")) dataTest = pd. ...

随机推荐

  1. UVA 12510/CSU 1119 Collecting Coins DFS

    前年的省赛题,难点在于这个石头的推移不太好处理 后来还是看了阳神当年的省赛总结,发现这个石头这里,因为就四五个子,就暴力dfs处理即可.先把石头当做普通障碍,进行一遍全图的dfs或者bfs,找到可以找 ...

  2. leetcode303 Range Sum Query - Immutable

    """ Given an integer array nums, find the sum of the elements between indices i and j ...

  3. 印度第一颗CPU横空出世!阵势庞大

    我们忙着推进国产芯片的同时,隔壁的印度也没闲着.作为印度顶级高校的印度理工学院(IIT)之马德拉斯校区已经发布了其首颗处理器“Shakti”(代表女性力量的印度神话人物)的SDK软件开发包,并承诺会很 ...

  4. Rabbitmq之高级特性——实现消费端限流&NACK重回队列

    如果是高并发下,rabbitmq服务器上收到成千上万条消息,那么当打开消费端时,这些消息必定喷涌而来,导致消费端消费不过来甚至挂掉都有可能. 在非自动确认的模式下,可以采用限流模式,rabbitmq ...

  5. 1 —— js 语法回顾 —— 数据类型。流程控制。数组

    一,数据类型 字符串 . 数值 .布尔. null . undefined . 对象  ( 数组 . 函数 function(){} . object) undefined 出现的情景 :  (1)变 ...

  6. jenkins打包iOS 报错:error: exportArchive: The data couldn’t be read because it isn’t in the correct format.

    在执行ios 打包的时候,我们通过执行下面的指令来打包ipa: mkdir arch archive_path=arch/${app_name}.xcarchive workspace_name=HP ...

  7. MySQL每日执行

    drop event if exists upload_deadline; DELIMITER $$ create event upload_deadline day starts timestamp ...

  8. R语言 plot()函数 基础用法

    plot(x=x轴数据,y=y轴数据,main="标题",sub="子标题",type="线型",xlab="x轴名称" ...

  9. Python 中使用 ddt 来进行数据驱动,批量执行用例,修改ddt代码

    1. 什么是数据驱动? 使用数据驱动有什么好处? 用例执行是靠数据来驱动的,每条测试用例除了测试数据不一样意外,所有的用例代码都是一样的,为了使用例批量执行,我们会使用数据驱动的思想来批量执行测试用例 ...

  10. flink初识及安装flink standalone集群

    flink architecture 1.可以看出,flink可以运行在本地,也可以类似spark一样on yarn或者standalone模式(与spark standalone也很相似),此外fl ...