我正在尝试使用 R 中的“mlr”库和 iris 数据集上的“c50”算法(使用 F1 分数作为指标):
library(mlr)
library(C50)
data(iris)
zooTask <- makeClassifTask(data = iris, target = "Species")
forest <- makeLearner("classif.C50")
forestParamSpace <- makeParamSet(
makeIntegerParam("minCases", lower = 1, upper = 100))
randSearch <- makeTuneControlRandom(maxit = 100)
cvForTuning <- makeResampleDesc("CV", iters = 5, measures = f1)
tunedForestPars <- tuneParams(forest, task = zooTask,
resampling = cvForTuning,
par.set = forestParamSpace,
control = randSearch)
tunedForestPars
Run Code Online (Sandbox Code Playgroud)
但这会导致以下错误:
Error in makeResampleDescCV(iters = 5, measures = list(id = "f1", minimize = FALSE, :
unused argument (measures = …
Run Code Online (Sandbox Code Playgroud) 我在尝试将数据集中的行放入“垃圾箱”时遇到了很多困难。例如,假设我有一个带有“var1”和“var2”的数据框“df”:
我想创建一个名为“var3”的新变量,它遵循以下逻辑(R 代码):
1) if var1 <5 and var2<5 .... then var3 = "a"
2) if var1 between (5,10) and var2 between (5,10) .... then var3 = "b"
3) if var1 > 10 and and var2>10 .... then var3 = "c"
Run Code Online (Sandbox Code Playgroud)
从我发布的上一个问题(If statements with multiple range (R))中,我尝试了以下逻辑:
library(dplyr)
df %>%
mutate(var3 = case_when(var1 < 5 & var2 < 5 ~ 'a',
var1 > 5 & var1 < 10 & var2 > 5 & var2 < 10 ~ 'b', …
Run Code Online (Sandbox Code Playgroud) 我正在使用 R 编程语言。最近,我学习了如何使用 ggplot 库“绘制”水平线和垂直线:
library(ggplot2)
# Simple scatter plot
sp <- ggplot(data=mtcars, aes(x=wt, y=mpg)) + geom_point()
# Add horizontal line at y = 2O
a = sp + geom_hline(yintercept=20)
b = sp + geom_vline(xintercept = 3)
Run Code Online (Sandbox Code Playgroud)
现在,我尝试将这些陈述结合在一起 - 例如:
c = sp + geom_hline(yintercept=20) + geom_hline(yintercept=15) + geom_vline(xintercept = 3) +
geom_vline(xintercept = 5) + geom_vline(xintercept = 6) + geom_hline(yintercept=35)
Run Code Online (Sandbox Code Playgroud)
问题:我正在尝试修改上面的图,使其看起来像这样:
有人可以告诉我该怎么做吗?或者您是否必须手动将此图导出到 Microsoft Paint 中并在那里进行更改?
谢谢
有人可以解释一下 R 中“extdata”的含义吗?
例如,我正在查看 R 中的“cronR”库(用于自动调度作业),并遇到了术语“extdata”:
f <- system.file(package = "cronR", "extdata", "helloworld.R")
cmd <- cron_rscript(f)
cmd
cron_add(command = cmd, frequency = 'minutely',
id = 'test1', description = 'My process 1', tags = c('lab', 'xyz'))
cron_add(command = cmd, frequency = 'daily', at='7AM', id = 'test2')
cron_njobs()
cron_ls()
cron_clear(ask=TRUE)
cron_ls()
Run Code Online (Sandbox Code Playgroud)
同样,“taskscheduleR”包(也用于自动调度作业)也引用了“extdata”:
library(taskscheduleR)
myscript <- system.file("extdata", "helloworld.R", package = "taskscheduleR")
## run script once within 62 seconds
taskscheduler_create(taskname = "myfancyscript", rscript = myscript,
schedule = "ONCE", starttime = format(Sys.time() + 62, "%H:%M")) …
Run Code Online (Sandbox Code Playgroud) 我从小就一直有这样的疑问:
这是问题:
我尝试使用 R 为这种情况编写一个模拟:
original_data = data.frame(id = 1:100, state = "start")
number_fries_selected_in_first_flip = sample(1:100, 1, replace=F)
fries_selected_in_first_flip = sample(1:100, number_fries_selected_in_first_flip, replace=F)
Run Code Online (Sandbox Code Playgroud)
这就是我陷入困境的地方 - 如果我能以某种方式“标记”所选的炸薯条,我就可以以 50% 的概率为这些炸薯条分配“烧焦/完全煮熟”状态:
status <- c("perfectly cooked","burnt")
original_data$tagged_fries_status <- sample(status, number_fries_selected_in_first_flip, replace=TRUE, prob=c(0.5, 0.5))
Run Code Online (Sandbox Code Playgroud)
如果我能完成模拟,我可以将模拟扩展到第二次翻转、第三次翻转等。在模拟结束时(例如,5 次翻转后),我可以制作一个图表,显示烧焦的炸薯条数量与煮得很完美。然后,我可以多次重复模拟(例如1000次),并找出炸焦/完全煮熟的薯条的平均数量。
有人可以告诉我如何编写这个模拟吗?
谢谢你!
我有一个 PDF 图像文件(例如“p1.pdf” - 这是原始文档的扫描副本)保存在我的计算机上 - 该文件看起来像这样(我添加了红线以显示区别):
我想将此 PDF 导入到 R 中,并将其转换为“表对象”。我尝试按照本教程(+ https://cran.r-project.org/web/packages/tesseract/vignettes/intro.html )来做到这一点:
library(tesseract)
library(magick)
library(png)
library(pdftools)
library(tidyverse)
pngfile <- pdftools::pdf_convert('p1.pdf', dpi = 600)
text <- tesseract::ocr(pngfile)
cat(text)
raw_img <- image_read(pngfile)
raw_img %>%
image_ocr()
Run Code Online (Sandbox Code Playgroud)
这似乎有效 - 我继续按照教程进行操作:
num_only <- tesseract::tesseract(
options = list(tessedit_char_whitelist = c(".0123456789 "))
)
raw_img %>%
image_quantize(colorspace = 'gray') %>%
image_threshold() %>%
image_crop(geometry_area(100, 0, 600, 40)) %>%
ocr(engine = num_only)
combo <- tesseract::tesseract(
options = list(
tessedit_char_whitelist = paste0(
c(letters, LETTERS, " ", ".0123456789 …
Run Code Online (Sandbox Code Playgroud) 我正在使用 R 编程语言。
我有下面的“树”,它描述了抛硬币游戏的结果(从 5 点开始,每轮有 0.5 个 +1 的概率和 0.5 个 -1 的概率):
outcomes <- c(-1, 1)
combinations <- expand.grid(rep(list(outcomes), 10))
colnames(combinations) <- paste("Turn", 1:10)
library(data.tree)
generate_tree <- function(node, depth, total) {
if (depth == 0) {
node$Set(total = total)
return(node)
} else {
for (outcome in outcomes) {
child <- node$AddChild(name = as.character(total + outcome), total = total + outcome)
generate_tree(child, depth - 1, total + outcome)
}
return(node)
}
}
root <- Node$new("Start", total = 5)
root …
Run Code Online (Sandbox Code Playgroud) 我正在使用 R 编程语言。我正在尝试学习如何在图表上叠加点,然后将它们可视化。
使用以下代码,我可以生成一些时间序列数据,按月聚合它们,取平均值/最小值/最大值,并绘制以下图表:
library(xts)
library(ggplot2)
library(dplyr)
library(plotly)
library(lubridate)
set.seed(123)
#time series 1
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
property_damages_in_dollars <- rnorm(731,100,10)
final_data <- data.frame(date_decision_made, property_damages_in_dollars)
#####aggregate
final_data$year_month <- format(as.Date(final_data$date_decision_made), "%Y-%m")
final_data$year_month <- as.factor(final_data$year_month)
f = final_data %>% group_by (year_month) %>% summarise(max_value = max(property_damages_in_dollars), mean_value = mean(property_damages_in_dollars), min_value = min(property_damages_in_dollars))
####plot####
fig <- plot_ly(f, x = ~year_month, y = ~max_value, type = 'scatter', mode = 'lines',
line = list(color = 'transparent'),
showlegend = FALSE, name = 'max_value')
fig <- fig %>% add_trace(y …
Run Code Online (Sandbox Code Playgroud) 我模拟了一个数据集,进行了一些数据操作(以一种非常笨拙的方式)并绘制了以下图。
模拟数据:
# Step 1 : Simulate Data
set.seed(123)
Hospital_Visits = sample.int(20, 5000, replace = TRUE)
Weight = rnorm(5000, 90, 10)
disease <- c("Yes","No")
disease <- sample(disease, 5000, replace=TRUE, prob=c(0.4, 0.6))
Disease <- as.factor(disease)
my_data = data.frame(Weight, Hospital_Visits, Disease)
my_data$hospital_ntile <- cut(my_data$Hospital_Visits, breaks = c(0, 5, 10, Inf), labels = c("Less than 5", "5 to 10", "More than 10"), include.lowest = TRUE)
Run Code Online (Sandbox Code Playgroud)
数据处理:
# Step 2: Data Manipulation:
my_data$weight_ntile <- cut(my_data$Weight, breaks = seq(min(my_data$Weight), max(my_data$Weight), by = (max(my_data$Weight) - min(my_data$Weight)) …
Run Code Online (Sandbox Code Playgroud) 我正在使用 R 编程语言。
我正在尝试学习如何模拟队列。
例如:
我想在一段时间内进行模拟和绘图:
从数学中,我知道泊松到达之间的差异具有指数分布。如果我选择 lambda1 和 lambda2 的某些值 - 我可以在运行循环之前模拟所有到达时间和服务时间。这是因为到达时间和服务时间完全相互独立且相互关联:
library(ggplot2)
# Parameters
lambda1 <- 5 # customer arrival rate
lambda2 <- 7 # order completion rate
time_period <- 1000 # total time period
# Initialize
arrival_times <- cumsum(rexp(time_period, rate = 1/lambda1))
service_times <- rexp(time_period, rate = 1/lambda2)
completion_times <- numeric(time_period)
queue_length <- numeric(time_period)
total_wait_time <- numeric(time_period)
Run Code Online (Sandbox Code Playgroud)
然后我可以尝试在 1000 个时间点上模拟这个队列:
# Simulation
for (i in 1:time_period) { …
Run Code Online (Sandbox Code Playgroud) r ×10
dplyr ×2
ggplot2 ×2
graph ×1
if-statement ×1
mlr ×1
ocr ×1
plot ×1
plotly ×1
python ×1
r-plotly ×1
range ×1
scatter-plot ×1
simulation ×1
tesseract ×1
time-series ×1
tree ×1