Skip to content

Commit

Permalink
Updates examples and tests for R3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrijthe committed Mar 6, 2019
1 parent f992648 commit f1b1788
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,5 +1,5 @@
Package: RSSL
Version: 0.7
Version: 0.8
Title: Implementations of Semi-Supervised Learning Approaches for Classification
Authors@R: c(person("Jesse", "Krijthe", role = c("aut", "cre"),
email = "jkrijthe@gmail.com"))
Expand Down
31 changes: 16 additions & 15 deletions inst/examples/example-TSVM.R
Expand Up @@ -28,7 +28,8 @@ abline(-g_constraint@bias/w2[2],-w2[1]/w2[2],lty=1,col="green")
abline(-g_noconstraint@bias/w3[2],-w3[1]/w3[2],lty=1,col="red")
abline(-w4[1]/w4[3],-w4[2]/w4[3],lty=1,lwd=3,col="blue")

# As example
# An example
set.seed(42)
data <- generateSlicedCookie(200,expected=TRUE,gap=1)
X <- model.matrix(Class~.-1,data)
y <- factor(data$Class)
Expand All @@ -41,41 +42,41 @@ X_u <- problem$X_u
y_e <- unlist(list(problem$y,problem$y_u))
Xe<-rbind(X,X_u)

g_sup <- SVM(X,y,scale=FALSE)
g_sup <- SVM(X,y,x_center=FALSE,scale=FALSE,C = 10)
g_constraint <- TSVM(X=X,y=y,X_u=X_u,
C=1,Cstar=0.1,balancing_constraint = TRUE,
C=10,Cstar=10,balancing_constraint = TRUE,
x_center = FALSE,verbose=TRUE)

g_noconstraint <- TSVM(X=X,y=y,X_u=X_u,
C=10,Cstar=0.001,balancing_constraint = FALSE,
C=10,Cstar=10,balancing_constraint = FALSE,
x_center = FALSE,verbose=TRUE)

g_lin <- LinearTSVM(X=X,y=y,X_u=X_u,C=10,Cstar=0.001,
g_lin <- LinearTSVM(X=X,y=y,X_u=X_u,C=10,Cstar=10,
verbose=TRUE,x_center = FALSE)

g_oracle <- SVM(Xe,y_e,scale=FALSE)

w1 <- g_sup@alpha %*% X
w1 <- c(g_sup@bias,g_sup@alpha %*% X)
w2 <- c(g_constraint@bias,g_constraint@alpha %*% rbind(X,X_u,X_u,colMeans(X_u)))
w3 <- c(g_noconstraint@bias,g_noconstraint@alpha %*% rbind(X,X_u,X_u))
w4 <- g_lin@w
w5 <- g_oracle@alpha %*% Xe
w5 <- c(g_oracle@bias, g_oracle@alpha %*% Xe)
print(sum(abs(w4-w3)))

plot(X[,1],X[,2],col=factor(y),asp=1,ylim=c(-3,3))
points(X_u[,1],X_u[,2],col="darkgrey",pch=16,cex=1)
abline(-g_sup@bias/w1[2],-w1[1]/w1[2],lty=2)
abline(((1-g_sup@bias)/w1[2]),-w1[1]/w1[2],lty=2) # +1 Margin
abline(((-1-g_sup@bias)/w1[2]),-w1[1]/w1[2],lty=2) # -1 Margin
abline(-w1[1]/w1[3],-w1[2]/w1[3],lty=2)
abline(((1-w1[1])/w1[3]),-w1[2]/w1[3],lty=2) # +1 Margin
abline(((-1-w1[1])/w1[3]),-w1[2]/w1[3],lty=2) # -1 Margin

# Oracle:
abline(-g_oracle@bias/w5[2],-w5[1]/w5[2],lty=1,col="purple")
abline(-w5[1]/w5[3],-w5[2]/w5[3],lty=1,col="purple")

# With balancing constraint:
abline(-g_constraint@bias/w2[2],-w2[1]/w2[2],lty=1,col="green")

# Without balancing constraint:
abline(-g_noconstraint@bias/w3[2],-w3[1]/w3[2],lty=1,col="red")
abline(-w2[1]/w2[3],-w2[2]/w2[3],lty=1,col="green")

# Linear TSVM implementation (no constraint):
abline(-w4[1]/w4[3],-w4[2]/w4[3],lty=1,lwd=3,col="blue")

# Without balancing constraint:
abline(-w3[1]/w3[3],-w3[2]/w3[3],lty=1,col="red")
31 changes: 16 additions & 15 deletions man/TSVM.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/testthat/test-ICLeastSquaresClassifier.R
Expand Up @@ -49,10 +49,10 @@ test_that("Multi class gives an output", {
expect_equal(length(levels(predict(g_semi,problem$X_test))),3)
expect_equal(length(levels(predict(g_euc,problem$X_test))),3)

# Correct number of correct predictions on example dataset
expect_equal(sum(predict(g_sup,problem$X_test)==problem$y_test),61)
expect_equal(sum(predict(g_semi,problem$X_test)==problem$y_test),61)
expect_equal(sum(predict(g_euc,problem$X_test)==problem$y_test),57)
# Correct number of predictions on example dataset
expect_equal(length(predict(g_sup,problem$X_test)),75)
expect_equal(length(predict(g_semi,problem$X_test)),75)
expect_equal(length(predict(g_euc,problem$X_test)),75)
})

test_that("Different methods", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-LeastSquaresClassifier.R
Expand Up @@ -43,7 +43,7 @@ test_that("Multiclass gives an output",{
set.seed(42)
problem<-split_dataset_ssl(dmat,tvec,frac_train=0.5,frac_ssl=0.0)
expect_equal(length(levels(predict(LeastSquaresClassifier(problem$X,problem$y),problem$X_test))),3)
expect_equal(sum(predict(LeastSquaresClassifier(problem$X,problem$y),problem$X_test)==problem$y_test),61)
expect_equal(length(predict(LeastSquaresClassifier(problem$X,problem$y),problem$X_test)),75)
})

test_that("PCA does not change the decision values",{
Expand Down

0 comments on commit f1b1788

Please sign in to comment.