rm(list = ls())
library(dplyr)
df <-read.csv("/data/bigfiles/iris.csv")
thres <- 1.9
n_setosa_all <- df %>%
n_setosa_hit <-
df %>%
tpr <-
n_false_alarm <- df %>%
fpr <-
print(paste(round(tpr, 2), round(fpr, 2)))
完整的代码如下,望采纳
# 清空环境中的对象列表
rm(list = ls())
# 加载 dplyr 包
library(dplyr)
# 读取 iris 数据集
df <- read.csv("/data/bigfiles/iris.csv")
# 计算花萼和花瓣的长宽比
df <- df %>%
# 在数据集中添加两列:花萼长宽比和花瓣长宽比
mutate(sepal_ratio = Sepal.Length / Sepal.Width,
petal_ratio = Petal.Length / Petal.Width)
# 设置花萼长宽比的阈值
thres <- 1.9
# 计算数据集中 Iris-setosa 的个数
n_setosa_all <- df %>%
# 筛选出种类为 Iris-setosa 的样本
filter(Species == "Iris-setosa") %>%
# 计算样本数
nrow
# 计算正确识别的 Iris-setosa 的个数
n_setosa_hit <- df %>%
# 筛选出种类为 Iris-setosa 且花萼长宽比小于阈值的样本
filter(Species == "Iris-setosa", sepal_ratio < thres) %>%
# 计算样本数
nrow
# 计算真阳性率
tpr <- n_setosa_hit / n_setosa_all
# 计算假阳性的个数
n_false_alarm <- df %>%
# 筛选出种类不是 Iris-setosa 且花萼长宽比小于阈值的样本
filter(Species != "Iris-setosa", sepal_ratio < thres) %>%
# 计算样本数
nrow
# 计算假阳性率
fpr <- n_false_alarm / (nrow(df) - n_setosa_all)
# 输出结果
print(paste(round(tpr, 2), round(fpr, 2)))