r - SVM 分类 - R 中的标准化

标签 r machine-learning svm normalization libsvm

我想使用 SVM 分类。在使用 SVM 模型之前,如何标准化(或缩放)数据集中每列的特征?

train <- read.csv("train.csv")
test <- read.csv("test.csv")

svm.fit=svm(as.factor(type)~ ., data=train, core="libsvm",kernel="linear",cross=10, probability=TRUE)

最佳答案

您可以在 sapply 中使用 scale 函数:

scaleddf <- as.data.frame(sapply(train, function(i) if(is.numeric(i)) scale(i) else i))

如果您的数据包含具有 NaN 值或方差为 0 的变量,您可以在使用上述函数之前先对原始数据集进行处理和子集化。

# get a vector of variables to drop
dropVars <- sapply(train, function(i) {
              if((is.numeric(i) & !any(is.nan(i)) & sd(i) > 0) | is.factor(i) | is.character(i)) TRUE
              else FALSE
              }
# subset test dropping columns that don't fit the criteria
smallerdf <- test[, dropVars]

然后将上面原来的sapply函数应用到smallrdf

关于r - SVM 分类 - R 中的标准化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36728598/

相关文章:

python - 如何加速sklearn SVR?

r - 差异时间的分组平均值在 data.table 中失败

r - 使用 Plotly R 在 Likert 图中显示分组百分比

r - 带有 log1p 转换的annotation_logticks

c# - Numl.net 中训练数据的后备存储并添加到其中以提高准确性

opencv - 如何从CvSVM计算置信度分数

r - compileAttributes 不会将本地 header 复制到 RcppExports.cpp

c# - 使用ID3算法、Accord.Net框架进行预测

python - 未实现错误: Layer ModuleWrapper has arguments in `__init__` and therefore must override `get_config`

machine-learning - LIBSVM 分类准确率 100% - 可能出了什么问题?