Skip to content

Instantly share code, notes, and snippets.

@lgatto
Created June 8, 2012 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgatto/2898194 to your computer and use it in GitHub Desktop.
Save lgatto/2898194 to your computer and use it in GitHub Desktop.
Different ksvm models
> library(kernlab)
> data(iris)
> irismodel1 <- ksvm(Species ~ ., data = iris)
Using automatic sigma estimation (sigest) for RBF or laplace kernel
> irismodel2 <- ksvm(Species ~ ., data = iris)
Using automatic sigma estimation (sigest) for RBF or laplace kernel
> table(predict(irismodel1, iris[,-5]), predict(irismodel2, iris[,-5]))
setosa versicolor virginica
setosa 50 0 0
versicolor 0 50 0
virginica 0 1 49
> all.equal(irismodel1, irismodel2)
[1] "Attributes: < Component 1: Component 1: Numeric: lengths (18, 36) differ >"
[2] "Attributes: < Component 1: Component 2: Numeric: lengths (25, 39) differ >"
[3] "Attributes: < Component 1: Component 3: Numeric: lengths (39, 48) differ >"
[4] "Attributes: < Component 2: Component 1: Numeric: lengths (18, 36) differ >"
[5] "Attributes: < Component 2: Component 2: Numeric: lengths (25, 39) differ >"
[6] "Attributes: < Component 2: Component 3: Numeric: lengths (39, 48) differ >"
[7] "Attributes: < Component 3: Mean relative difference: 0.9913751 >"
[8] "Attributes: < Component 5: Component 1: Numeric: lengths (18, 36) differ >"
[9] "Attributes: < Component 5: Component 2: Numeric: lengths (25, 39) differ >"
[10] "Attributes: < Component 5: Component 3: Numeric: lengths (39, 48) differ >"
[11] "Attributes: < Component 7: Mean relative difference: 0.25 >"
[12] "Attributes: < Component 8: 1 string mismatch >"
[13] "Attributes: < Component 10: target, current do not match when deparsed >"
[14] "Attributes: < Component 15: Mean relative difference: 0.2280702 >"
[15] "Attributes: < Component 16: Mean relative difference: 0.2696004 >"
[16] "Attributes: < Component 21: Numeric: lengths (57, 70) differ >"
[17] "Attributes: < Component 24: Component 1: Attributes: < Component 1: Mean relative difference: 1 > >"
[18] "Attributes: < Component 24: Component 1: Attributes: < Component 2: Component 1: 18 string mismatches > >"
[19] "Attributes: < Component 24: Component 1: Numeric: lengths (72, 144) differ >"
[20] "Attributes: < Component 24: Component 2: Attributes: < Component 1: Mean relative difference: 0.56 > >"
[21] "Attributes: < Component 24: Component 2: Attributes: < Component 2: Component 1: 25 string mismatches > >"
[22] "Attributes: < Component 24: Component 2: Numeric: lengths (100, 156) differ >"
[23] "Attributes: < Component 24: Component 3: Attributes: < Component 1: Mean relative difference: 0.2307692 > >"
[24] "Attributes: < Component 24: Component 3: Attributes: < Component 2: Component 1: 39 string mismatches > >"
[25] "Attributes: < Component 24: Component 3: Numeric: lengths (156, 192) differ >"
@lgatto
Copy link
Author

lgatto commented Jun 13, 2012

This difference is the result of different sigma estimate

irismodel1@kernelf@kpar$sigma
[1] 1.217243
irismodel2@kernelf@kpar$sigma
[1] 0.5681333

Setting sigma manually

irismodel3 <- ksvm(Species ~ ., data = iris, kpar = list(sigma = 1))
irismodel4 <- ksvm(Species ~ ., data = iris, kpar = list(sigma = 1))
all.equal(irismodel3, irismodel4)
[1] TRUE

@lgatto
Copy link
Author

lgatto commented Jun 14, 2012

Looking at the variability of the automatic sigma estimations

sigmas <- replicate(500, ksvm(Species ~ ., data = iris)@kernelf@kpar$sigma)
summary(sigmas)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.3263 0.6038 0.7315 0.7772 0.8996 1.8320

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment