iris数据集预测
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数据集预测的更多相关文章
- 机器学习笔记2 – sklearn之iris数据集
前言 本篇我会使用scikit-learn这个开源机器学习库来对iris数据集进行分类练习. 我将分别使用两种不同的scikit-learn内置算法--Decision Tree(决策树)和kNN(邻 ...
- 机器学习——logistic回归,鸢尾花数据集预测,数据可视化
0.鸢尾花数据集 鸢尾花数据集作为入门经典数据集.Iris数据集是常用的分类实验数据集,由Fisher, 1936收集整理.Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集.数据集包含150个数 ...
- Iris数据集实战
本次主要围绕Iris数据集进行一个简单的数据分析, 另外在数据的可视化部分进行了重点介绍. 环境 win8, python3.7, jupyter notebook 目录 1. 项目背景 2. 数据概 ...
- 从Iris数据集开始---机器学习入门
代码多来自<Introduction to Machine Learning with Python>. 该文集主要是自己的一个阅读笔记以及一些小思考,小总结. 前言 在开始进行模型训练之 ...
- 85、使用TFLearn实现iris数据集的分类
''' Created on 2017年5月21日 @author: weizhen ''' #Tensorflow的另外一个高层封装TFLearn(集成在tf.contrib.learn里)对训练T ...
- 用Python实现支持向量机并处理Iris数据集
SVM全称是Support Vector Machine,即支持向量机,是一种监督式学习算法.它主要应用于分类问题,通过改进代码也可以用作回归.所谓支持向量就是距离分隔面最近的向量.支持向量机就是要确 ...
- iris数据集(.csv .txt)免费下载
我看CSDN下载的iris数据集都需要币,我愿意免费共享,希望下载后的朋友们给我留个言 分享iris数据集(供学习使用): 链接: https://pan.baidu.com/s/1Knsp7zn-C ...
- R语言实现分层抽样(Stratified Sampling)以iris数据集为例
R语言实现分层抽样(Stratified Sampling)以iris数据集为例 1.观察数据集 head(iris) Sampling)以iris数据集为例"> 选取数据集中前6个 ...
- KNN算法实现对iris数据集的预测
KNN算法的实现 import pandas as pd from math import dist k = int(input("请输入k值:")) dataTest = pd. ...
随机推荐
- HihoCoder第十四周:无间道之并查集
#1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息--额,说远了,总之,小Hi和小H ...
- springboot启动微服务项目时,启动后没有端口号信息,也访问不了
2018-06-05 13:43:42.282 [localhost-startStop-1] DEBUG org.apache.catalina.core.ContainerBase - Add c ...
- 10.swoole学习笔记--进程队列通信
<?php //进程仓库 $workers=[]; //最大进程数 $worker_num=; //批量创建进程 ;$i<$worker_num;$i++){ //创建子进程 $proce ...
- 竟然把Ruoyi在我自己的Eclipse编译成功,并能跑通了。。。。服了我自己了
前几天,下载最新ECLISPSE2019压缩包,解压缩成功,没提示不是免费:eclipse-jee-2019-12-R-win32-x86_64.zip然后我配置好了maven于是我1月2日晚一时兴起 ...
- Springboot Bean循环依赖问题
参考博客原文地址: https://www.jb51.net/article/168398.htm https://www.cnblogs.com/mianteno/p/10692633.html h ...
- 030-PHP日期查询函数
<?php , , ))//检查日期函数 { print("2,18,1970 :" . "这是一个正确的日期格式"); } else { print(& ...
- 025-PHP常用字符串函数(二)
<?php $text = "My dog's name is Angus."; //print Angus print(substr($text, , )."&l ...
- 第五篇Django URL name 详解
Django URL name 详解 利用Django开发网站,可以设计出非常优美的url规则,如果url的匹配规则(包含正则表达式)组织得比较好,view的结构就会比较清晰,比较容易维护. Djan ...
- MacOS通过ssh连接基于Virtualbox的Ubuntu虚拟机
以前总是用Windows软件putty进行ssh连接,今天尝试使用macos. 实验环境:主机:macos 10.15.3 客户机:Ubuntu 18.04 默认情况下,Ubuntu没有安装SSH,需 ...
- java的形参与实参的区别以及java的方法
package com.lv.study; public class Demo05 { public static void main(String[] args) { //我想要用什么分隔符进行分隔 ...