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. C++学习记录——(queue的清空)

    c++自带的queue并没有clear这个方法:所以只能自己写了. 一共三种(其实我决得就是两种): 第一种: 直接赋值 queue<int> MyQue; /* …… */ MyQue ...

  2. CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)

    题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集 ...

  3. [LeetCode] 928. Minimize Malware Spread II 最大程度上减少恶意软件的传播之二

    (This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...

  4. namenode节点无法自动切换主从

    当停掉主namenode节点,从节点无法切换到active状态,有两种可能导致这种问题 1.查看namenode上的zkfc日志,发现没有fuser命令,需要手动安装 yum install -y p ...

  5. 通过html5 touch事件封装手势识别组件

    html5移动端新增了touchstart,touchmove,touchend事件,利用这3个事件,判断手指的点击和划动轨迹,我们可以封装各种手势的识别功能, 这3个事件和pc端的mousedown ...

  6. ES6 之 Object 的方法总结

    阅读:Object 1.obj的"." 或 "[]"方法 读取对象的属性或方法 对象属性的读取:ES6中被Proxy的get(target, propKey, ...

  7. 留学萌新Essay写作须知

    Essay是留学生们接触比较多的一项留学生作业,但尽管如此,依旧有部分同学对于essay写作是没有足够的把握的.随着开学季的到来,很多萌新初次接触Essay写作,难免会有很多不懂得地方.所以今天小编就 ...

  8. 八、React实战:可交互待办事务表(表单使用、数据的本地缓存local srtorage、生命同期函数(页面加载就会执行函数名固定为componentDidMount()))

    一.项目功能概述 示例网址:http://www.todolist.cn/ 功能: 输入待做事项,回车,把任务添加到 [正在进行] [正在进行] 任务,勾选之后,变成已[经完成事项] [已完成事务], ...

  9. ACM-Fire Net

    题目描述:Fire Net   Suppose that we have a square city with straight streets. A map of a city is a squar ...

  10. 【Python】【Django】查询所有学生信息

    要做到以下效果: 改代码后效果: 从0开始顺序计数: 倒叙计数到0