Skip to content

Commit

Permalink
classifier
Browse files Browse the repository at this point in the history
  • Loading branch information
acca3003 committed Aug 22, 2021
1 parent 59e46e9 commit 6e9ec34
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 31 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ngboostR
Title: R Wrapper for NGBoost python module
Version: 0.1.5
Version: 0.1.6
Authors@R:
person(given = "Alfonso",
family = "Carabantes Alamo",
Expand Down
2 changes: 1 addition & 1 deletion R/distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ k_categorical <- function( K ){
#' @return Bernoulli Distribution Object
#' @export
Bernoulli <- function(){
ngboost$distns$k_categorical(as.integer(1))
ngboost$distns$k_categorical(as.integer(2))
}


38 changes: 22 additions & 16 deletions R/ngboostRClassifier.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,29 @@
#' @param tol numerical tolerance to be used in optimization
#' @return A NGBoostRegressor object
#' @export
create_classifier <- function(Dist=NULL,
natural_gradient=TRUE,
n_estimators=as.integer(500),
learning_rate=0.01,
minibatch_frac=1.0,
create_classifier <- function(Base=DecisionTreeRegressor(),
Dist=Bernoulli(),
col_sample=1.0,
verbose=TRUE,
verbose_eval=as.integer(100),
tol=1e-4) {
learning_rate=0.01,
minibatch_frac=1.0,
n_estimators=as.integer(500),
natural_gradient=TRUE,
random_state=NULL,
tol=0.0001,
verbose=TRUE,
verbose_eval=as.integer(100)) {

classifier <- ngboost$NGBClassifier(Dist=Dist,
natural_gradient=natural_gradient,
n_estimators=as.integer(n_estimators),
minibatch_frac=minibatch_frac,
classifier <- ngboost$NGBClassifier(Base=Base,
Dist=Dist,
col_sample=col_sample,
verbose=verbose,
verbose_eval=as.integer(verbose_eval),
tol=tol
learning_rate=learning_rate,
minibatch_frac=minibatch_frac,
n_estimators=n_estimators,
natural_gradient=natural_gradient,
random_state=random_state,
tol=tol,
verbose=verbose,
verbose_eval=verbose_eval
)
classifier
}
Expand Down Expand Up @@ -77,6 +82,7 @@ predict_classifier_prob <- function( ngbr_cla, new_data) {
#' @export
predict_classifier_dist <- function( ngbr_cla, new_data) {
pred_temp <- ngbr_cla$pred_dist(new_data)
pred <- list( "p0"=pred_temp$loc, "p1"=pred_temp$scale)
number_classes <- ngbr_cla$Dist$K_
pred <- pred_temp$params
pred
}
26 changes: 14 additions & 12 deletions man/create_classifier.Rd

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

2 changes: 1 addition & 1 deletion man/fit_regressor.Rd

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

File renamed without changes.
55 changes: 55 additions & 0 deletions vignettes/classification.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "ngboostR Classifier"
author: "Alfonso Carabantes"
date: "22/8/2021"
output: html_document
---

```{r}
#Install last ve
#library(devtools)
#install_github("acca3003/ngboostR")
library(ngboostR) # R implementation for NGBoost
library(Metrics) # Métrics
library(mlbench) # Breast Cancer dataset
library(caret)
data("BreastCancer")
set.seed(999)
BreastCancer <- na.omit(BreastCancer)
trainIndex <- createDataPartition(BreastCancer$Class, p = .8,
list = FALSE,
times = 1)
X_train <- BreastCancer[trainIndex,2:10]
Y_train <- BreastCancer[trainIndex,11]
Y_train <- as.integer(as.integer(Y_train)-1)
X_val <- BreastCancer[-trainIndex,2:10]
Y_val <- BreastCancer[-trainIndex,11]
Y_val <- as.integer(as.integer(Y_val)-1)
# Create the regressor object
# reg_ngboost <- create_regressor() # Default parameters
class_ngboost <- create_classifier()
# Train with the boston data
fit_classifier(class_ngboost, X_train, Y_train, X_val, Y_val)
# Predict the price
predictions <- predict_classifier(class_ngboost, X_val)
Metrics::accuracy(Y_val,predictions)
# Predict the price
predictions_prob <- predict_classifier_prob(class_ngboost, X_val)
predictions_prob
# Predict the price as a distribution
predictions_dist <- predict_classifier_dist(class_ngboost, X_val)
predictions_dist
```

File renamed without changes.

0 comments on commit 6e9ec34

Please sign in to comment.