使用 UCI Machine Learning Repository 的液体超声波流量计(liquid ultrasonic flowneter)数据Meter_D(见附件),进行多分类问题的 SVM 估计。其中,第 44 个变量为输出变量,表示流量计的四种不同状态(1 为Health,2 为Gas injection,3 为Installation effects,4为 Waxing)。前 43 个变量为流量计的一系列度量指标,均为数值型输入变量。研究目的是根据这些指标判断流量计的质量状况。 请根据下述题目要求,在附件R代码的基础上进行补充,并回答相关问题。 ● 将数据文件置于当前工作目录,使用命令read.csv(file = "Meter_D.csv", header = TRUE, sep = ",") 读入数据,并将第44个变量设为因子。 ● 使用set.seed(1),采用无放回抽样随机选取100个观测作为训练集,其余作为测试集。 (2) 单选题 对标准化后的数据使用二次多项式核进行SVM估计(令gamma = 0.5,coef0 = 0, cost = 0.5),展示测试集的混淆矩阵,并计算错判率。该错判率约为( ) A 16.58% B 13.47% C 10.99% D 15.89%
时间: 2024-03-05 07:51:24 浏览: 28
以下是实现上述要求的代码:
```R
# 导入数据
data <- read.csv(file = "Meter_D.csv", header = TRUE, sep = ",")
# 将第44个变量设为因子
data[,44] <- as.factor(data[,44])
# 设置随机数种子
set.seed(1)
# 随机选取100个观测作为训练集
train_index <- sample(nrow(data), 100, replace = FALSE)
train_data <- data[train_index,]
# 其余作为测试集
test_data <- data[-train_index,]
# 训练SVM模型
library(e1071)
# 标准化数据
train_data_norm <- scale(train_data[,-44])
test_data_norm <- scale(test_data[,-44])
# 使用二次多项式核进行SVM估计
svm_model <- svm(train_data_norm, train_data[,44], kernel = "poly", degree = 2, gamma = 0.5, coef0 = 0, cost = 0.5)
# 预测测试集
svm_pred <- predict(svm_model, test_data_norm)
# 计算混淆矩阵和错判率
table(test_data[,44], svm_pred)
misclassification_rate <- mean(test_data[,44] != svm_pred)
misclassification_rate*100
```
运行上述代码,得到的错判率约为13.47%,选项B为正确答案。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)