嗨,有一個資料框,它是 ML 模型的一些性能指標的集合:
> df
# A tibble: 10 x 6
Method AUC CA F1 Precision Recall
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Logistic Regression 0.732 0.684 0.413 0.681 0.296
2 Naive Bayes 0.729 0.694 0.463 0.679 0.352
3 Tree 0.678 0.694 0.429 0.717 0.306
4 Neural Network 0.674 0.684 0.413 0.681 0.296
5 AdaBoost 0.654 0.681 0.418 0.66 0.306
6 CN2 rule inducer 0.651 0.681 0.403 0.674 0.287
7 kNN 0.649 0.66 0.372 0.604 0.269
8 SVM 0.64 0.691 0.44 0.686 0.324
9 SGD 0.591 0.667 0.4 0.615 0.296
10 Constant 0.5 0.625 0 0 0
輸入:
structure(list(Method = c("Logistic Regression", "Naive Bayes",
"Tree", "Neural Network", "AdaBoost", "CN2 rule inducer", "kNN",
"SVM", "SGD", "Constant"), AUC = c(0.732, 0.729, 0.678, 0.674,
0.654, 0.651, 0.649, 0.64, 0.591, 0.5), CA = c(0.684, 0.694,
0.694, 0.684, 0.681, 0.681, 0.66, 0.691, 0.667, 0.625), F1 = c(0.413,
0.463, 0.429, 0.413, 0.418, 0.403, 0.372, 0.44, 0.4, 0), Precision = c(0.681,
0.679, 0.717, 0.681, 0.66, 0.674, 0.604, 0.686, 0.615, 0), Recall = c(0.296,
0.352, 0.306, 0.296, 0.306, 0.287, 0.269, 0.324, 0.296, 0)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
我需要在 excel 中將其組合在一行中,但是復制每一行的每個列名很累人。所以我想把所有的東西都作為一個字串(或一個字串串列)說:[模型名稱]:Col1_name Col1 值,Col2_name value2,...,等等。像這樣的東西:
`Logistic Regression: AUC 0.732, CA 0.684, F1 0.413, Precision 0.681, Recall 0.296
Naive Bayes: AUC 0.729, CA 0.694, F1 0.463, Precision 0.679, Recall 0.352
Tree ... (and so on).`
一行也可以:
Logistic Regression: AUC 0.732, CA 0.684, F1 0.413, Precision 0.681, Recall 0.296 Naive Bayes: AUC 0.729, CA 0.694, F1 0.463, Precision 0.679, Recall 0.352 Tree ... (and so on)
但我不知道如何在每個值之前添加每個列名。我將不勝感激任何幫助!
uj5u.com熱心網友回復:
也許這對你有用。
Output <- apply(df, 1, function(x) {
gsub(' AUC', ': AUC', paste(paste(names(x), x), collapse = ' '))
})
在這里,我假設AUC將始終是資料集中的第二列。如果不是,您可以相應地更改它。
uj5u.com熱心網友回復:
這與您要找的東西相近嗎?
my_df <- structure(list(Method = c("Logistic Regression", "Naive Bayes",
"Tree", "Neural Network", "AdaBoost", "CN2 rule inducer", "kNN",
"SVM", "SGD", "Constant"),
AUC = c(0.732, 0.729, 0.678, 0.674, 0.654, 0.651, 0.649, 0.64, 0.591, 0.5),
CA = c(0.684, 0.694, 0.694, 0.684, 0.681, 0.681, 0.66, 0.691, 0.667, 0.625),
F1 = c(0.413, 0.463, 0.429, 0.413, 0.418, 0.403, 0.372, 0.44, 0.4, 0),
Precision = c(0.681, 0.679, 0.717, 0.681, 0.66, 0.674, 0.604, 0.686, 0.615, 0),
Recall = c(0.296, 0.352, 0.306, 0.296, 0.306, 0.287, 0.269, 0.324, 0.296, 0)),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
my_df$Solution <- paste(my_df$Method, ":",
colnames(my_df)[2], my_df$AUC, ",",
colnames(my_df)[3], my_df$CA, ",",
colnames(my_df)[4], my_df$F1, ",",
colnames(my_df)[5], my_df$Precision, ",",
colnames(my_df)[6], my_df$Recall, ",")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/372052.html
上一篇:串列中元素之間的字串比較
