当前位置: 首页 > news >正文

用java做的网站怎么发布百度网址是什么

用java做的网站怎么发布,百度网址是什么,电气工程WordPress模板,抖音企业服务平台基于R 4.2.2版本演示 一、写在前面 有不少大佬问做机器学习分类能不能用R语言,不想学Python咯。 答曰:可!用GPT或者Kimi转一下就得了呗。 加上最近也没啥内容写了,就帮各位搬运一下吧。 二、R代码实现随机森林分类 &#xff…

基于R 4.2.2版本演示

一、写在前面

有不少大佬问做机器学习分类能不能用R语言,不想学Python咯。

答曰:可!用GPT或者Kimi转一下就得了呗。

加上最近也没啥内容写了,就帮各位搬运一下吧。

二、R代码实现随机森林分类

(1)导入数据

我习惯用RStudio自带的导入功能:

(2)建立随机森林模型(默认参数)

# Load necessary libraries
library(caret)
library(pROC)
library(ggplot2)# Assume 'data' is your dataframe containing the data
# Set seed to ensure reproducibility
set.seed(123)# Split data into training and validation sets (80% training, 20% validation)
trainIndex <- createDataPartition(data$X, p = 0.8, list = FALSE)
trainData <- data[trainIndex, ]
validData <- data[-trainIndex, ]# Convert the target variable to a factor for classification
trainData$X <- as.factor(trainData$X)
validData$X <- as.factor(validData$X)# Define control method for training with cross-validation
trainControl <- trainControl(method = "cv", number = 10)# Fit Random Forest model on the training set
model <- train(X ~ ., data = trainData, method = "rf", trControl = trainControl)# Print the best parameters found by the model
best_params <- model$bestTune
cat("The best parameters found are:\n")
print(best_params)
# Predict on the training and validation sets
trainPredict <- predict(model, trainData, type = "prob")[,2]
validPredict <- predict(model, validData, type = "prob")[,2]# Calculate ROC curves and AUC values
trainRoc <- roc(response = trainData$X, predictor = trainPredict)
validRoc <- roc(response = validData$X, predictor = validPredict)# Plot ROC curves with AUC values
ggplot(data = data.frame(fpr = trainRoc$specificities, tpr = trainRoc$sensitivities), aes(x = 1 - fpr, y = tpr)) +geom_line(color = "blue") +geom_area(alpha = 0.2, fill = "blue") +geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "black") +ggtitle("Training ROC Curve") +xlab("False Positive Rate") +ylab("True Positive Rate") +annotate("text", x = 0.5, y = 0.1, label = paste("Training AUC =", round(auc(trainRoc), 2)), hjust = 0.5, color = "blue")ggplot(data = data.frame(fpr = validRoc$specificities, tpr = validRoc$sensitivities), aes(x = 1 - fpr, y = tpr)) +geom_line(color = "red") +geom_area(alpha = 0.2, fill = "red") +geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "black") +ggtitle("Validation ROC Curve") +xlab("False Positive Rate") +ylab("True Positive Rate") +annotate("text", x = 0.5, y = 0.2, label = paste("Validation AUC =", round(auc(validRoc), 2)), hjust = 0.5, color = "red")# Calculate confusion matrices based on 0.5 cutoff for probability
confMatTrain <- table(trainData$X, trainPredict >= 0.5)
confMatValid <- table(validData$X, validPredict >= 0.5)# Function to plot confusion matrix using ggplot2
plot_confusion_matrix <- function(conf_mat, dataset_name) {conf_mat_df <- as.data.frame(as.table(conf_mat))colnames(conf_mat_df) <- c("Actual", "Predicted", "Freq")p <- ggplot(data = conf_mat_df, aes(x = Predicted, y = Actual, fill = Freq)) +geom_tile(color = "white") +geom_text(aes(label = Freq), vjust = 1.5, color = "black", size = 5) +scale_fill_gradient(low = "white", high = "steelblue") +labs(title = paste("Confusion Matrix -", dataset_name, "Set"), x = "Predicted Class", y = "Actual Class") +theme_minimal() +theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(hjust = 0.5))print(p)
}# Now call the function to plot and display the confusion matrices
plot_confusion_matrix(confMatTrain, "Training")
plot_confusion_matrix(confMatValid, "Validation")# Extract values for calculations
a_train <- confMatTrain[1, 1]
b_train <- confMatTrain[1, 2]
c_train <- confMatTrain[2, 1]
d_train <- confMatTrain[2, 2]a_valid <- confMatValid[1, 1]
b_valid <- confMatValid[1, 2]
c_valid <- confMatValid[2, 1]
d_valid <- confMatValid[2, 2]# Training Set Metrics
acc_train <- (a_train + d_train) / sum(confMatTrain)
error_rate_train <- 1 - acc_train
sen_train <- d_train / (d_train + c_train)
sep_train <- a_train / (a_train + b_train)
precision_train <- d_train / (b_train + d_train)
F1_train <- (2 * precision_train * sen_train) / (precision_train + sen_train)
MCC_train <- (d_train * a_train - b_train * c_train) / sqrt((d_train + b_train) * (d_train + c_train) * (a_train + b_train) * (a_train + c_train))
auc_train <- roc(response = trainData$X, predictor = trainPredict)$auc# Validation Set Metrics
acc_valid <- (a_valid + d_valid) / sum(confMatValid)
error_rate_valid <- 1 - acc_valid
sen_valid <- d_valid / (d_valid + c_valid)
sep_valid <- a_valid / (a_valid + b_valid)
precision_valid <- d_valid / (b_valid + d_valid)
F1_valid <- (2 * precision_valid * sen_valid) / (precision_valid + sen_valid)
MCC_valid <- (d_valid * a_valid - b_valid * c_valid) / sqrt((d_valid + b_valid) * (d_valid + c_valid) * (a_valid + b_valid) * (a_valid + c_valid))
auc_valid <- roc(response = validData$X, predictor = validPredict)$auc# Print Metrics
cat("Training Metrics\n")
cat("Accuracy:", acc_train, "\n")
cat("Error Rate:", error_rate_train, "\n")
cat("Sensitivity:", sen_train, "\n")
cat("Specificity:", sep_train, "\n")
cat("Precision:", precision_train, "\n")
cat("F1 Score:", F1_train, "\n")
cat("MCC:", MCC_train, "\n")
cat("AUC:", auc_train, "\n\n")cat("Validation Metrics\n")
cat("Accuracy:", acc_valid, "\n")
cat("Error Rate:", error_rate_valid, "\n")
cat("Sensitivity:", sen_valid, "\n")
cat("Specificity:", sep_valid, "\n")
cat("Precision:", precision_valid, "\n")
cat("F1 Score:", F1_valid, "\n")
cat("MCC:", MCC_valid, "\n")
cat("AUC:", auc_valid, "\n")

在R语言中,使用 caret 包训练随机森林模型时,最常见的可调参数是 mtry,但还有其他几个参数可以根据需要调整。这些参数通常是从 randomForest 包继承而来的,因为 caret 包的随机森林方法默认使用的是这个包(所以第一次要安装)。下面是一些可以调整的关键参数:

①mtry: 在每个分割中考虑的变量数量。默认情况下,对于分类问题,mtry 默认值是总变量数的平方根;对于回归问题,是总变量数的三分之一。

②ntree: 构建的树的数量。更多的树可以提高模型的稳定性和准确性,但会增加计算时间和内存使用。默认值通常是 500。

③nodesize: 每个叶节点最少包含的样本数。增加这个参数的值可以减少模型的过拟合,但可能会导致欠拟合。对于分类问题,默认值通常为 1,而回归问题则较大。

④maxnodes: 最大的节点数。这限制了树的最大大小,可以用来控制模型复杂度。

⑤importance: 是否计算变量重要性。这不会影响模型的预测能力,但会影响变量重要性分数的计算。

⑥replace: 是否进行有放回抽样。默认为 TRUE,意味着进行有放回的抽样。

⑦classwt: 类的权重,用于分类问题中的不平衡数据。

⑧cutoff: 分类问题中用于预测类别的概率阈值。这通常是一个类别数目的向量。

⑨sampsize: 用于每棵树的样本大小,如果使用有放回抽样(replace=TRUE),它决定了每个样本被抽样的次数。

结果输出(默认参数):

在默认参数中,caret包只会默默帮我们找几个合适的mtry值进行测试,其他默认值。

随机森林祖传的过拟合现象。

三、随机森林调参方法(4个值)

设置mtry值取值2、数据集列数的平方根、数据集列数的一半;ntree取值100、500和1000;nodesize取值1、5、10;maxnodes取值30、50、100:

# Load necessary libraries
library(caret)
library(pROC)
library(ggplot2)
library(randomForest)  # Using randomForest for model fitting# Assume 'data' is your dataframe containing the data
# Set seed to ensure reproducibility
set.seed(123)# Split data into training and validation sets (80% training, 20% validation)
trainIndex <- createDataPartition(data$X, p = 0.8, list = FALSE)
trainData <- data[trainIndex, ]
validData <- data[-trainIndex, ]# Convert the target variable to a factor for classification
trainData$X <- as.factor(trainData$X)
validData$X <- as.factor(validData$X)# Define ranges for each parameter including mtry
mtry_range <- c(2, sqrt(ncol(trainData)), ncol(trainData)/2)
ntree_range <- c(100, 500, 1000)
nodesize_range <- c(1, 5, 10)
maxnodes_range <- c(30, 50, 100)# Initialize variables to store the best model, its AUC, and parameters
best_auc <- 0
best_model <- NULL
best_params <- NULL  # Initialize best_params to store parameter values# Nested loops to try different combinations of parameters including mtry
for (mtry in mtry_range) {for (ntree in ntree_range) {for (nodesize in nodesize_range) {for (maxnodes in maxnodes_range) {# Train the Random Forest modelrf_model <- randomForest(X ~ ., data = trainData, mtry=mtry, ntree=ntree, nodesize=nodesize, maxnodes=maxnodes)# Predict on validation set using probabilities for ROCvalidProb <- predict(rf_model, newdata = validData, type = "prob")[,2]# Calculate AUCvalidRoc <- roc(validData$X, validProb)auc <- auc(validRoc)# Update the best model if the current model is betterif (auc > best_auc) {best_auc <- aucbest_model <- rf_modelbest_params <- list(mtry=mtry, ntree=ntree, nodesize=nodesize, maxnodes=maxnodes)  # Store parameters of the best model}}}}
}# Check if a best model was found and output parameters
if (!is.null(best_params)) {cat("The best model parameters are:\n")print(best_params)
} else {cat("No model was found to exceed the baseline performance.\n")
}# Use best model for predictions
trainPredict <- predict(best_model, trainData, type = "prob")[,2]
validPredict <- predict(best_model, validData, type = "prob")[,2]# Rest of the analysis, including ROC curves and plotting
trainRoc <- roc(response = trainData$X, predictor = trainPredict)
validRoc <- roc(response = validData$X, predictor = validPredict)# Plotting code continues unchanged
ggplot(data = data.frame(fpr = trainRoc$specificities, tpr = trainRoc$sensitivities), aes(x = 1 - fpr, y = tpr)) +geom_line(color = "blue") +geom_area(alpha = 0.2, fill = "blue") +geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "black") +ggtitle("Training ROC Curve") +xlab("False Positive Rate") +ylab("True Positive Rate") +annotate("text", x = 0.5, y = 0.1, label = paste("Training AUC =", round(auc(trainRoc), 2)), hjust = 0.5, color = "blue")ggplot(data = data.frame(fpr = validRoc$specificities, tpr = validRoc$sensitivities), aes(x = 1 - fpr, y = tpr)) +geom_line(color = "red") +geom_area(alpha = 0.2, fill = "red") +geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = "black") +ggtitle("Validation ROC Curve") +xlab("False Positive Rate") +ylab("True Positive Rate") +annotate("text", x = 0.5, y = 0.2, label = paste("Validation AUC =", round(auc(validRoc), 2)), hjust = 0.5, color = "red")confMatTrain <- table(trainData$X, trainPredict >= 0.5)
confMatValid <- table(validData$X, validPredict >= 0.5)# Function to plot confusion matrix using ggplot2
plot_confusion_matrix <- function(conf_mat, dataset_name) {conf_mat_df <- as.data.frame(as.table(conf_mat))colnames(conf_mat_df) <- c("Actual", "Predicted", "Freq")p <- ggplot(data = conf_mat_df, aes(x = Predicted, y = Actual, fill = Freq)) +geom_tile(color = "white") +geom_text(aes(label = Freq), vjust = 1.5, color = "black", size = 5) +scale_fill_gradient(low = "white", high = "steelblue") +labs(title = paste("Confusion Matrix -", dataset_name, "Set"), x = "Predicted Class", y = "Actual Class") +theme_minimal() +theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.title = element_text(hjust = 0.5))print(p)
}# Function to plot confusion matrix and further analysis remains the same...
plot_confusion_matrix(confMatTrain, "Training")
plot_confusion_matrix(confMatValid, "Validation")# Extract values for calculations
a_train <- confMatTrain[1, 1]
b_train <- confMatTrain[1, 2]
c_train <- confMatTrain[2, 1]
d_train <- confMatTrain[2, 2]a_valid <- confMatValid[1, 1]
b_valid <- confMatValid[1, 2]
c_valid <- confMatValid[2, 1]
d_valid <- confMatValid[2, 2]# Training Set Metrics
acc_train <- (a_train + d_train) / sum(confMatTrain)
error_rate_train <- 1 - acc_train
sen_train <- d_train / (d_train + c_train)
sep_train <- a_train / (a_train + b_train)
precision_train <- d_train / (b_train + d_train)
F1_train <- (2 * precision_train * sen_train) / (precision_train + sen_train)
MCC_train <- (d_train * a_train - b_train * c_train) / sqrt((d_train + b_train) * (d_train + c_train) * (a_train + b_train) * (a_train + c_train))
auc_train <- roc(response = trainData$X, predictor = trainPredict)$auc# Validation Set Metrics
acc_valid <- (a_valid + d_valid) / sum(confMatValid)
error_rate_valid <- 1 - acc_valid
sen_valid <- d_valid / (d_valid + c_valid)
sep_valid <- a_valid / (a_valid + b_valid)
precision_valid <- d_valid / (b_valid + d_valid)
F1_valid <- (2 * precision_valid * sen_valid) / (precision_valid + sen_valid)
MCC_valid <- (d_valid * a_valid - b_valid * c_valid) / sqrt((d_valid + b_valid) * (d_valid + c_valid) * (a_valid + b_valid) * (a_valid + c_valid))
auc_valid <- roc(response = validData$X, predictor = validPredict)$auc# Print Metrics
cat("Training Metrics\n")
cat("Accuracy:", acc_train, "\n")
cat("Error Rate:", error_rate_train, "\n")
cat("Sensitivity:", sen_train, "\n")
cat("Specificity:", sep_train, "\n")
cat("Precision:", precision_train, "\n")
cat("F1 Score:", F1_train, "\n")
cat("MCC:", MCC_train, "\n")
cat("AUC:", auc_train, "\n\n")cat("Validation Metrics\n")
cat("Accuracy:", acc_valid, "\n")
cat("Error Rate:", error_rate_valid, "\n")
cat("Sensitivity:", sen_valid, "\n")
cat("Specificity:", sep_valid, "\n")
cat("Precision:", precision_valid, "\n")
cat("F1 Score:", F1_valid, "\n")
cat("MCC:", MCC_valid, "\n")
cat("AUC:", auc_valid, "\n")

结果输出:

以上是找到的相对最优参数组合,看看具体性能:

还不错,至少过拟合调下来了。

五、最后

数据嘛:

链接:https://pan.baidu.com/s/1rEf6JZyzA1ia5exoq5OF7g?pwd=x8xm

提取码:x8xm


文章转载自:
http://dinncosickle.tqpr.cn
http://dinncocitybred.tqpr.cn
http://dinncoaffectless.tqpr.cn
http://dinncoshambles.tqpr.cn
http://dinncoillinium.tqpr.cn
http://dinncojamaica.tqpr.cn
http://dinncopronged.tqpr.cn
http://dinnconutant.tqpr.cn
http://dinncofelv.tqpr.cn
http://dinncomillihenry.tqpr.cn
http://dinncooutsit.tqpr.cn
http://dinncoexordial.tqpr.cn
http://dinncocompatibility.tqpr.cn
http://dinncoincreaser.tqpr.cn
http://dinncoregurgitation.tqpr.cn
http://dinncodispositive.tqpr.cn
http://dinncogastrologer.tqpr.cn
http://dinncoshiveringly.tqpr.cn
http://dinncofane.tqpr.cn
http://dinnconobleness.tqpr.cn
http://dinncoblitzkrieg.tqpr.cn
http://dinncorep.tqpr.cn
http://dinncofaculty.tqpr.cn
http://dinncoinvestiture.tqpr.cn
http://dinncoproper.tqpr.cn
http://dinncocamptothecin.tqpr.cn
http://dinncoaquaculture.tqpr.cn
http://dinncosemiprivate.tqpr.cn
http://dinncoenglobe.tqpr.cn
http://dinncobovril.tqpr.cn
http://dinncodismountable.tqpr.cn
http://dinncoeuphuist.tqpr.cn
http://dinncoschlockmaster.tqpr.cn
http://dinncolignin.tqpr.cn
http://dinncobuhr.tqpr.cn
http://dinncocahier.tqpr.cn
http://dinncofeint.tqpr.cn
http://dinncocalais.tqpr.cn
http://dinncolightwave.tqpr.cn
http://dinncoogam.tqpr.cn
http://dinncowoefully.tqpr.cn
http://dinncomoniliasis.tqpr.cn
http://dinncotoughly.tqpr.cn
http://dinncoperforator.tqpr.cn
http://dinncodianetics.tqpr.cn
http://dinncohermaphroditus.tqpr.cn
http://dinncobucko.tqpr.cn
http://dinncoweaponeer.tqpr.cn
http://dinncoatheroma.tqpr.cn
http://dinncopithecanthropus.tqpr.cn
http://dinncotalgo.tqpr.cn
http://dinncovinegar.tqpr.cn
http://dinncodenbighshire.tqpr.cn
http://dinncoinsititious.tqpr.cn
http://dinncoinadvertent.tqpr.cn
http://dinncoanociassociation.tqpr.cn
http://dinncoonychophoran.tqpr.cn
http://dinnconanny.tqpr.cn
http://dinncovga.tqpr.cn
http://dinncolarviparous.tqpr.cn
http://dinncobrickbat.tqpr.cn
http://dinncoi2o.tqpr.cn
http://dinncolineskipper.tqpr.cn
http://dinncowicketkeeper.tqpr.cn
http://dinncogarbo.tqpr.cn
http://dinncocourthouse.tqpr.cn
http://dinncoconjunctive.tqpr.cn
http://dinncotussore.tqpr.cn
http://dinncohelleborine.tqpr.cn
http://dinncochicly.tqpr.cn
http://dinncoalfisol.tqpr.cn
http://dinncogametogeny.tqpr.cn
http://dinncocurtle.tqpr.cn
http://dinncoisagogic.tqpr.cn
http://dinncoindefinitely.tqpr.cn
http://dinncoobpyriform.tqpr.cn
http://dinncostalag.tqpr.cn
http://dinncoimperceptibility.tqpr.cn
http://dinncogestation.tqpr.cn
http://dinncodysplasia.tqpr.cn
http://dinncoaqaba.tqpr.cn
http://dinncogeneralcy.tqpr.cn
http://dinncofjp.tqpr.cn
http://dinncodirectorate.tqpr.cn
http://dinncohawaii.tqpr.cn
http://dinncokondo.tqpr.cn
http://dinncorussia.tqpr.cn
http://dinncoelectrokymograph.tqpr.cn
http://dinncogeneralization.tqpr.cn
http://dinncoperhydrol.tqpr.cn
http://dinncomoses.tqpr.cn
http://dinncostrut.tqpr.cn
http://dinncounplausible.tqpr.cn
http://dinncoflatter.tqpr.cn
http://dinncoovergarment.tqpr.cn
http://dinncosorbo.tqpr.cn
http://dinncolinable.tqpr.cn
http://dinncosoaraway.tqpr.cn
http://dinncomotif.tqpr.cn
http://dinncogrenadine.tqpr.cn
http://www.dinnco.com/news/95454.html

相关文章:

  • gucci网站关键词推广操作
  • 建外贸网站 东莞网页设计代码案例
  • 国外网站设计欣赏分析私域流量营销
  • 网站建设需求文案案例品牌营销策略
  • 做网站图片像素google chrome
  • 广东专业移动网站建设哪家好代做网页设计平台
  • 武汉网站建设顾问网络运营推广
  • 小说网站怎么建设百度关键词网站排名优化软件
  • 建立网站一般那些阶段上海seo推广公司
  • java网站开发学习网址注册查询
  • 重庆旅游网站建设地址互联网营销做什么
  • 大型百度云网站建设网站搜索引擎优化工具
  • 商标注册查询官方网站技术短期培训班
  • 新潮远网站建设营销推广内容
  • 网页设计的网站推荐中国网站排名100
  • 手机版网站制作模板游戏推广公司靠谱吗
  • 网站建设实践总结网络站点推广的方法有哪些
  • 上海知名公司seo专员简历
  • 制作网站支付方式郑州网络营销公司有哪些
  • 帝国cms网站公告怎么做获客
  • 怎么能查到网站是哪个公司做的网推渠道
  • 电子类网站模板短视频运营培训学费多少
  • 2024免费推广网站西安企业seo
  • 如何做网站模板衡阳seo排名
  • 广告制作与设计专业墨猴seo排名公司
  • 网站建设销售人员培训教程做抖音seo排名软件是否合法
  • 网页设计与网站建设作业短视频seo软件
  • 亚泰润德建设有限公司网站怎么开发自己的网站
  • 程序员做网站seo百度发包工具
  • 社会题目可以在哪些网站上做怎么推广app