machine learning - SVM classification - normalization in R -
i want work svm
classification. how possible normalize (or scale) features per column in dataset before use svm
model?
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)
you can use scale
function in sapply
:
scaleddf <- as.data.frame(sapply(train, function(i) if(is.numeric(i)) scale(i) else i))
if data contains variables nan
values or 0 variance, first process , subset original dataset before using function above.
# vector of variables 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 don't fit criteria smallerdf <- test[, dropvars]
then apply original sapply
function above smallerdf
Comments
Post a Comment