-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from datmo/r-notebook
R notebook example
- Loading branch information
Showing
3 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
title: "Log your experiments in R with Datmo" | ||
author: "Nick Walsh" | ||
output: | ||
html_document: | ||
df_print: paged | ||
toc: yes | ||
rmarkdown::html_vignette: | ||
number_sections: yes | ||
toc: yes | ||
--- | ||
|
||
Setup | ||
===== | ||
|
||
First, we'll need to install a few packages for use today. They'll contain everything we'll need to model our data and create visualizations. | ||
|
||
```{r installPackages} | ||
install.packages("datasets") # Package that contains the Iris dataset | ||
install.packages("caret", dependencies = TRUE) # Model selection/tuning package | ||
install.packages("rpart.plot") # Visualization package | ||
``` | ||
|
||
We're going to install a python package called Datmo, which will enable us to log and track our experiments through the power of *snapshots*. | ||
If you don't already have pip, you can [find it here](https://pip.pypa.io/en/stable/installing/). | ||
|
||
```{bash} | ||
pip install datmo | ||
``` | ||
|
||
Next, we're going to want to make sure we've set the proper working directory. We can do this easily through the | ||
RStudio file finder on the right, or with the following command. | ||
|
||
This will be necessary so that Datmo knows the proper directory to perform tracking in. | ||
|
||
```{r "setup", include=FALSE} | ||
require("knitr") | ||
opts_knit$set(root.dir = "~/Dev/datmo-R-example") # Replace with whatever your root directory for the project is | ||
``` | ||
|
||
Now we're going to initialize a Datmo repository. This will enable us to create snapshots for logging our experiments. | ||
|
||
```{r initializeDatmo} | ||
system("datmo init", input=c("my new project","test description"), timeout=15) | ||
``` | ||
|
||
Example | ||
====== | ||
|
||
Ok, time to start with loading in the Fisher Iris dataset. | ||
|
||
```{r loadData} | ||
library(datasets) | ||
df <- iris # Create dataframe from the Iris dataset | ||
head(df) # View first few rows of dataset | ||
``` | ||
|
||
Now that our dataframe is loaded in, we can import the *caret* package to perform training. | ||
|
||
```{r fitModel} | ||
library(caret) | ||
modFit <- train(Species ~., method = "rpart", data=df) #Fit model | ||
print(modFit$finalModel) #Summarize model | ||
``` | ||
|
||
Our model is built, but it's kind of hard to comprehend with just the metrics. Let's create a visualization to showcase the | ||
splits in our decision tree. | ||
|
||
```{r visualizeModel} | ||
library(rpart.plot) | ||
rpart.plot(modFit$finalModel) #create decision tree visualization | ||
``` | ||
|
||
Awesome! Since we're happy with our model results, we'll want to save our model and log configuration and stats sections in a snapshot. | ||
We can do this with the following syntax, where we're creating a *char* string of format "--PROPERTY key:value" that will be passed to | ||
the snapshot create code block. | ||
|
||
```{r defineSnapshot} | ||
config<- paste(sep="", | ||
" --config method:", modFit$method, | ||
" --config modelType:", modFit$modelType) | ||
#define metrics to save from the model | ||
stats<- paste(sep="", | ||
" --stats Accuracy:", modFit$results$Accuracy[1], | ||
" --stats Kappa:", modFit$results$Kappa[1]) | ||
config | ||
stats | ||
``` | ||
|
||
```{r snapshotCreate} | ||
system2("datmo", args=paste("snapshot create", "-m 'Whoah, my first snapshot!'", config, stats)) | ||
``` | ||
|
||
```{bash} | ||
datmo snapshot ls | ||
``` |