I cannot predict binary classification from svm() from 'e1071' package in R

30 Views Asked by At

I cannot predict the binary classification in target variable, because I can not output the probability of each record in test set, I got NULL when I tried to transfer into probability. The code and result is shown below:

Input:

target_name <- names(train_data)[160]
formula_str <- paste(target_name, "~ .")
formula <- as.formula(formula_str)
svm_model <- svm(formula, data = train_data[, c(features,160)], 
probability = TRUE)
predictions <- predict(svm_model, newdata = test_data[, features], probability = TRUE)
predictions

Output:

1         2         3         4         5         6
1.4978286 0.9500268 0.9483237 1.8486790 1.4958919 1.0135074
8         9        10        11        12        13
1.7213668 0.9839955 1.6171267 0.9615482 1.0362087 0.8663245
......

Input:

probs <- attr(predictions, "probabilities")
probs

Output:

> probs <- attr(predictions, "probabilities")
> probs
NULL

For checking the predictions, I used:

Input:

attributes(predictions)

Output:

> attributes(predictions)
> $names
> [1] "1"   "2"   "3"   "4"   "5"   "6"   "8"   "9"   "10"
> [10] "11"  "12"  "13"  "18"  "21"  "22"  "23"  "24"  "25"
> [19] "29"  "30"  "32"  "33"  "34"  "35"  "36"  "37"  "38"

I wanna assign the probability to target variable of each observations, then assign the negative(0) or positive(1) based on the threshold I set (for example 0.5). How can I achieve the objective? If I can not do that in 'e1071' package, can I achieve the goal by other packages?

0

There are 0 best solutions below