How to plot XGBoost trees in R

How to plot XGBoost trees in R

Machine Learning, R
In this post, we're going to cover how to plot XGBoost trees in R. XGBoost is a very popular machine learning algorithm, which is frequently used in Kaggle competitions and has many practical use cases. Let's start by loading the packages we'll need. Note that plotting XGBoost trees requires the DiagrammeR package to be installed, so even if you have xgboost installed already, you'll need to make sure you have DiagrammeR also. [code lang="R"] # load libraries library(xgboost) library(caret) library(dplyr) library(DiagrammeR) [/code] Next, let's read in our dataset. In this post, we'll be using this customer churn dataset. The label we'll be trying to predict is called "Exited" and is a binary variable with 1 meaning the customer churned (canceled account) vs. 0 meaning the customer did not churn (did…
Read More
Faster data exploration with DataExplorer

Faster data exploration with DataExplorer

R
Data exploration is an important part of the modeling process. It can also take up a fair amount of time. The awesome DataExplorer package in R aims to make this process easier. To get started with DataExplorer, you'll need to install it like below: [code lang="R"] install.packages("DataExplorer") [/code] Let's use DataExplorer to explore a dataset on diabetes. [code lang="R"] # load DataExplorer library(DataExplorer) # read in dataset diabetes_data <- read.csv("https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.csv", header = FALSE) # fix column names names(diabetes_data) <- c("number_of_times_pregnant", "plasma_glucose_conc", "diastolic_bp", "triceps_skinfold_thickness", "two_hr_serum_insulin", "bmi", "diabetes_pedigree_function", "age", "label") # create report create_report(diabetes_data) [/code] Running the create_report line of code above will generate an HTML report file containing a collection of useful information about the data. This includes: Basic statistics, such as number of rows and columns, number of columns with…
Read More
How to solve Sudoku with R

How to solve Sudoku with R

R
In this post we discuss how to write an R script to solve any Sudoku puzzle. There are some R packages to handle this, but in our case, we'll write our own solution. For our purposes, we'll assume the input Sudoku is a 9x9 grid. At the end result, each row, column, and 3x3 box needs to contain exactly one of each integer 1 through 9. Learn more about data science by checking out the great curriculum at 365 Data Science! Step 0) Define a sample board Let's define a sample Sudoku board for testing. Empty cells will be represented as zeroes. [code lang="R"] board <- matrix( c(0,0,0,0,0,6,0,0,0, 0,9,5,7,0,0,3,0,0, 4,0,0,0,9,2,0,0,5, 7,6,4,0,0,0,0,0,3, 0,0,0,0,0,0,0,0,0, 2,0,0,0,0,0,9,7,1, 5,0,0,2,1,0,0,0,9, 0,0,7,0,0,5,4,8,0, 0,0,0,8,0,0,0,0,0), byrow = T, ncol = 9 ) [/code] Step 1) Find the empty cells…
Read More
Why you should use vapply in R

Why you should use vapply in R

R
In this post we'll cover the vapply function in R. vapply is generally lesser known than the more popular sapply, lapply, and apply functions. However, it is very useful when you know what data type you're expecting to apply a function to as it helps to prevent silent errors. Because of this, it can be more advisable to use vapply rather than sapply or lapply. See more R articles by clicking here Examples Let's take the following example. Here, we have a list of numeric vectors and we want to get the max value of each vector. That's simple enough - we can just use sapply and apply the max function for each vector. [code lang="R"] test <- list(a = c(1, 3, 5), b = c(2,4,6), c = c(9,8,7)) sapply(test,…
Read More
How to create an API for your R code

How to create an API for your R code

R
In the video linked below we discuss how to convert your R code into an API using the awesome plumber package! Learn more by clicking here or by following the links below. The plumber package allows you to convert R functions into API calls. For example, rather than launching R and executing a function, you can use plumber to turn the function into an API request that can be called from other software (e.g. Python, cURL, etc.). This is enormously useful in a lot of applications e.g. building a web application with Python that can make API calls to R model objects, retrieving R plots into other applications, and more. Check out the video below to learn more! Link to full video: https://www.youtube.com/watch?v=Z2Aofr4UIFY To skip to specific parts of the…
Read More
How to create PowerPoint reports with R

How to create PowerPoint reports with R

File Manipulation, R
In my last post, we discussed how to create and read Word files with R's officer package. This article will expand on officer by showing how we can use it to create PowerPoint reports. Getting started Let's get started by loading officer. [code lang="R"] library(officer) [/code] Next, we'll create a PowerPoint object in R using the read_pptx function. [code lang="R"] pres <- read_pptx() [/code] To add a slide, we use the add_slide function. The first slide we'll create is the title slide. We specify the type of slide in the layout parameter. There's several other possibilities here including "Title and Content", "Blank", "Title Only", "Comparison", "Two Content", and "Section Header". Secondly, we use ph_with to add the title text. [code lang="R"] # add title slide pres <- add_slide(pres, layout =…
Read More
How to read and create Word Documents in R

How to read and create Word Documents in R

File Manipulation, R
Reading and creating word documents in R In this post we'll talk about how to use R to read and create word files. We'll primarily be using R's officer package. For reading data from Word Documents with Python, click here. Creating Word reports with the officer package The first thing we need to do is to install the officer package. [code lang="R"] install.packages("officer") [/code] We'll also be using the dplyr package, so you'll need to install that the same way if you don't have it already. Next, let's load each of these packages. [code lang="R"] library(officer) library(dplyr) [/code] Now, we'll get started creating a report! First, we will use the read_docx function to create an empty Word file. [code lang="R"] # create empty Word file sample_doc <- read_docx() [/code] Adding…
Read More
How to schedule R scripts

How to schedule R scripts

R, System Administration
Running R with taskscheduleR and cronR In a previous post, we talked about how to run R from the Windows Task Scheduler. This article will talk about two additional approaches to schedule R scripts, including using the taskscheduleR package on Windows and the cronR package for Linux. For scheduling Python code, check out this post. Schedule R scripts with taskscheduleR Let's install taskscheduleR using the install.packages command. [code lang="R"] install.packages("taskscheduleR") [/code] Next, we just need to load the package to get started. [code lang="R"] library(taskscheduleR) [/code] Creating a sample R script to run automatically Before we do any scheduling, we need to first create a script. We'll save the code below in a file called "create_file.txt". This script will randomly generate a collection of integers and write them out to…
Read More
What to study if you’re under quarantine

What to study if you’re under quarantine

Python, R
If you're staying indoors more often recently because of the current COVID-19 outbreak and looking for new things to study, here's a few ideas! Free 365 Data Science Courses 365 Data Science is making all of their courses free until April 15. They have a variety of courses across R, Python, SQL, and more. Their platform also has courses that give a great mathematical foundation behind machine learning, which helps you a lot as you get deeper into data science. They also have courses on deep learning, which is a hot field right now. In addition to pure data science, 365 Data Science also covers material on Git / Github, which is essential for any data scientist nowadways. Another nice feature of 365 Data Science is that they also offer…
Read More