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. ...
随机推荐
- python中pip的安装问题
当系统中同时存在python2.python3的时候,在进行pip下载的时总出现不知道用哪个 情况,此时就需要这样做: python2 -m pip install numpy //使用pip给pyt ...
- Golang函数-不定参函数
Golang函数-不定参函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- 从高通出重拳,iPhone遭禁售看巨头们的专利大战
一直以来,高通看起来都是很有"骨气"的.面对与苹果的专利纷争,高通始终是据理力争.顽强对抗.截至今年6月中旬,高通和苹果打了50多场专利官司,且这些官司分布于6个不同国家的16个司 ...
- 官网英文版学习——RabbitMQ学习笔记(三)Hello World!
参考http://www.rabbitmq.com/tutorials/tutorial-one-java.html,我们直接上代码,由于我们的RabbitMQ服务是安装在虚拟机上的,具体参考上一节. ...
- /和//的区别(python)
/ 除 得到的是浮点数,结果是大数的时候会使用科学计数法 但是 / 会在遇到大数时候运算不准确 因为将两个int相除会产生一个浮点数,并且除法的确切结果不能精确地表示为float. 精确结果必须四舍 ...
- 五十一、SAP中使程序结构化,模块化
一.我们创建3个引用单元,这3个单元里面分别存放定义相关,执行相关和子程序相关的内容 二.将程序中的各部分都拆分到INCLUDE文件中,DEF文件内容如下 三.INC文件内容 四.MAIN文件中写程序 ...
- 17 —— 服务端渲染 —— art-template
一,前端渲染数据 的弊端 仿 apache 服务器与客户端的几次交互: 1,加载静态页面 2,加载静态资源 3,发送 ajax 请求 ,接收请求并处理返回 . 4,前端浏览器接收数据循环遍历. 存在的 ...
- ng-options用法详解
ng-options属性可以在表达式中使用数组或对象来自动生成一个select中的option列表.ng-options与ng-repeat很相似,很多时候可以用ng-repeat来代替ng-opti ...
- DCGAN
Deep Convolutional Generative Adversarial Networks we introduced the basic ideas behind how GANs wor ...
- 66.Python中startswith和endswith的使用
定义模型的models.py,示例代码如下: from django.db import models class Category(models.Model): name = models.Char ...