在 R 中循环多维 NumPy fileArray
Loop over multidimensional NumPy fileArray in R
我是 R 新手,在生成 for 循环时遇到问题:
我在 R 中有一个多维数组(NumPy 文件),想自动执行数组部分的请求。我的数组的形状为 (500, 192)。我想绘制样本 1、2 的图表...直到样本 500。
AP 具有以下结构:
num [1:500, 1:192] 0.0323 0.0532 0.0135 0.0474 0.2026 ...
AP.shap 具有以下结构:
num [1:192] 3.23e-02 4.88e-04 1.39e-03 7.49e-04 5.82e-05 ...
library(reticulate)
library(RcppCNPy)
library(ggplot2)
#Sample 1
AP.shap <- (AP[1 ,]) # Sample 1
# convert to dataframe
typeof(AP.shap) # type double
AP.shap <- as.data.frame(AP.shap)
typeof(AP.shap) # list
# New Column with Nrow
AP.shap$Hour <- seq.int(nrow(AP.shap))
columnnames <- c("Shap", "Hour")
colnames(AP.shap) <- columnnames
#Plot
ggplot(AP.shap, aes(Hour, Shap))+
geom_bar(stat = "identity") + theme_minimal() +
ggtitle(expression(paste("Hours of Sample 1 - Air Pressure - G_PM"[1], " - All Season"))) +
xlab("Hour") +
ylab("Shap Value") +
ggsave("1_Hour_AP_G_allseason.png", plot = last_plot(), device = "png", path = "xy")
下一个是:
#Sample 2
AP.shap <- (AP[**2** ,]) # Sample 1
# convert to dataframe
typeof(AP.shap) # type double
AP.shap <- as.data.frame(AP.shap)
typeof(AP.shap) # list
# New Column with Nrow
AP.shap$Hour <- seq.int(nrow(AP.shap))
columnnames <- c("Shap", "Hour")
colnames(AP.shap) <- columnnames
#Plot
ggplot(AP.shap, aes(Hour, Shap))+
geom_bar(stat = "identity") + theme_minimal() +
ggtitle(expression(paste("Hours of Sample **2** - Air Pressure - G_PM"[1], " - All Season"))) +
xlab("Hour") +
ylab("Shap Value") +
ggsave("**2**_Hour_AP_G_allseason.png", plot = last_plot(), device = "png", path = "xy")
只需将您的流程概括为一个 user-defined 方法,该方法接收一个数字作为输入参数,因为这是唯一发生变化的项目。将参数切片数据用于样本并传递到标题和文件名中。请参阅下面使用 num
的地方。
然后,用 for
、while
或 lapply
迭代调用它,后者可以将绘图 objects 存储到列表中以供以后继续使用。
build_plot <- function(num) {
# Sample by num
AP.shap <- (AP[num, ]) # Sample num
# convert to dataframe
AP.shap <- as.data.frame(AP.shap)
# New Column with Nrow
AP.shap$Hour <- seq.int(nrow(AP.shap))
colnames(AP.shap) <- c("Shap", "Hour")
# Plot
p <- ggplot(AP.shap, aes(Hour, Shap)) +
geom_bar(stat = "identity") + theme_minimal() +
ggtitle(expression(paste("Hours of Sample", num,
"- Air Pressure - G_PM"[1], " - All Season"))) +
xlab("Hour") + ylab("Shap Value")
# Save
ggsave(paste0(num, "_Hour_AP_G_allseason.png"), plot = p, device = "png", path = "xy")
return(p)
}
# SAVES EACH PLOT FILE AND STORES PLOT TO R LIST
plot_list <- lapply(seq(1, 500), build_plot)
# INTERACTIVELY DISPLAY PLOT
plot_list[[1]]
plot_list[[2]]
plot_list[[3]]
对于传统循环,请参阅不将 objects 存储到 R 列表的 for
和 while
方法。注意:在您的环境中打印 500 个图时要小心。删除 print()
以简单地将绘图保存到文件。
# SAVES EACH PLOT FILE AND DISPLAYS PLOT TO SCREEN
for (i in seq(1, 500)) {
print(build_plot(i))
}
i = 1
# SAVES EACH PLOT FILE AND DISPLAYS PLOT TO SCREEN
while(i <= 500) {
print(build_plot(i))
i = i + 1
}
我是 R 新手,在生成 for 循环时遇到问题: 我在 R 中有一个多维数组(NumPy 文件),想自动执行数组部分的请求。我的数组的形状为 (500, 192)。我想绘制样本 1、2 的图表...直到样本 500。
AP 具有以下结构: num [1:500, 1:192] 0.0323 0.0532 0.0135 0.0474 0.2026 ...
AP.shap 具有以下结构: num [1:192] 3.23e-02 4.88e-04 1.39e-03 7.49e-04 5.82e-05 ...
library(reticulate)
library(RcppCNPy)
library(ggplot2)
#Sample 1
AP.shap <- (AP[1 ,]) # Sample 1
# convert to dataframe
typeof(AP.shap) # type double
AP.shap <- as.data.frame(AP.shap)
typeof(AP.shap) # list
# New Column with Nrow
AP.shap$Hour <- seq.int(nrow(AP.shap))
columnnames <- c("Shap", "Hour")
colnames(AP.shap) <- columnnames
#Plot
ggplot(AP.shap, aes(Hour, Shap))+
geom_bar(stat = "identity") + theme_minimal() +
ggtitle(expression(paste("Hours of Sample 1 - Air Pressure - G_PM"[1], " - All Season"))) +
xlab("Hour") +
ylab("Shap Value") +
ggsave("1_Hour_AP_G_allseason.png", plot = last_plot(), device = "png", path = "xy")
下一个是:
#Sample 2
AP.shap <- (AP[**2** ,]) # Sample 1
# convert to dataframe
typeof(AP.shap) # type double
AP.shap <- as.data.frame(AP.shap)
typeof(AP.shap) # list
# New Column with Nrow
AP.shap$Hour <- seq.int(nrow(AP.shap))
columnnames <- c("Shap", "Hour")
colnames(AP.shap) <- columnnames
#Plot
ggplot(AP.shap, aes(Hour, Shap))+
geom_bar(stat = "identity") + theme_minimal() +
ggtitle(expression(paste("Hours of Sample **2** - Air Pressure - G_PM"[1], " - All Season"))) +
xlab("Hour") +
ylab("Shap Value") +
ggsave("**2**_Hour_AP_G_allseason.png", plot = last_plot(), device = "png", path = "xy")
只需将您的流程概括为一个 user-defined 方法,该方法接收一个数字作为输入参数,因为这是唯一发生变化的项目。将参数切片数据用于样本并传递到标题和文件名中。请参阅下面使用 num
的地方。
然后,用 for
、while
或 lapply
迭代调用它,后者可以将绘图 objects 存储到列表中以供以后继续使用。
build_plot <- function(num) {
# Sample by num
AP.shap <- (AP[num, ]) # Sample num
# convert to dataframe
AP.shap <- as.data.frame(AP.shap)
# New Column with Nrow
AP.shap$Hour <- seq.int(nrow(AP.shap))
colnames(AP.shap) <- c("Shap", "Hour")
# Plot
p <- ggplot(AP.shap, aes(Hour, Shap)) +
geom_bar(stat = "identity") + theme_minimal() +
ggtitle(expression(paste("Hours of Sample", num,
"- Air Pressure - G_PM"[1], " - All Season"))) +
xlab("Hour") + ylab("Shap Value")
# Save
ggsave(paste0(num, "_Hour_AP_G_allseason.png"), plot = p, device = "png", path = "xy")
return(p)
}
# SAVES EACH PLOT FILE AND STORES PLOT TO R LIST
plot_list <- lapply(seq(1, 500), build_plot)
# INTERACTIVELY DISPLAY PLOT
plot_list[[1]]
plot_list[[2]]
plot_list[[3]]
对于传统循环,请参阅不将 objects 存储到 R 列表的 for
和 while
方法。注意:在您的环境中打印 500 个图时要小心。删除 print()
以简单地将绘图保存到文件。
# SAVES EACH PLOT FILE AND DISPLAYS PLOT TO SCREEN
for (i in seq(1, 500)) {
print(build_plot(i))
}
i = 1
# SAVES EACH PLOT FILE AND DISPLAYS PLOT TO SCREEN
while(i <= 500) {
print(build_plot(i))
i = i + 1
}