Mathematics and Programming for Machine Learning with R
From the Ground Up
William B. Claster
- 408 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Mathematics and Programming for Machine Learning with R
From the Ground Up
William B. Claster
About This Book
Based on the author's experience in teaching data science for more than 10 years, Mathematics and Programming for Machine Learning with R: From the Ground Up reveals how machine learning algorithms do their magic and explains how these algorithms can be implemented in code. It is designed to provide readers with an understanding of the reasoning behind machine learning algorithms as well as how to program them. Written for novice programmers, the book progresses step-by-step, providing the coding skills needed to implement machine learning algorithms in R.
The book begins with simple implementations and fundamental concepts of logic, sets, and probability before moving to the coverage of powerful deep learning algorithms. The first eight chapters deal with probability-based machine learning algorithms, and the last eight chapters deal with machine learning based on artificial neural networks. The first half of the book does not require mathematical sophistication, although familiarity with probability and statistics would be helpful. The second half assumes the reader is familiar with at least one semester of calculus. The text guides novice R programmers through algorithms and their application and along the way; the reader gains programming confidence in tackling advanced R programming challenges.
Highlights of the book include:
-
- More than 400 exercises
-
- A strong emphasis on improving programming skills and guiding beginners to the implementation of full-fledged algorithms
-
- Coverage of fundamental computer and mathematical concepts including logic, sets, and probability
-
- In-depth explanations of machine learning algorithms
-
Frequently asked questions
Information
Chapter 1
- ■ Functions with and without arguments
- ■ Defining versus running
- ■ Arguments
- ■ Parameter versus argument
- ■ Argument order
- ■ Environments
- ■ Scope
Functions Coded in This Chapter |
myFirstFunction |
addFive |
addThem |
divid |
outerFunction |
embeddedFunction |
1.1 Replicating the Results Shown Here
1.1.1 Knowledge of R
- Step 1: Get R. In order to run swirl, you must have R 3.1.0 or later installed on your computer. …
- Step 2 (recommended): Get RStudio. …
- Step 3: Install swirl. …
- Step 4: Start swirl. …
- Step 5: Install an interactive course. …
- Step 6: Have fun!
1.1.2 Getting Set Up
- Download and install R from https://cran.r-project.org/. On Windows, you may choose to use the “install as administrator” option since on occasion R may try to create folders; for example, when new packages are installed, and if the installation is not installed and run as administrator, Windows may block the creation of those folders.
- Download and install the free version of RStudio from https://www.rstudio.com. The same remarks as above regarding “install as administrator”.
- Run RStudio.
- Configure RStudio to look the way you want. You can adjust the appearance in the Global Options menu which is under Tools. When you open RStudio, it may look as shown in Figure 1.1.
- The so-called console window is the large window on the left in Figure 1.1. On the right are two other windows. Each window has various tabs. The first tab on the top right-hand window is the Environment tab, and it will show any variables you have created, functions you have created, datasets that have been loaded, and so on. The bottom window currently is showing the first tab which is just a file browser for the current directory that you are working in. You can see that the current directory that is open is Home>Book RSTUDIO Tutorial. There is one window which is not shown now but is important. It is called the source window. The console allows you to type single commands into R, and when you hit the enter key, R will run that code. However, most of the time, when I am writing code, I don’t want to evaluate each time I write a new line of code. I want to be able to write several lines of code first and then run it. This can’t be done in the console window. The source window is used for this. To show the source window, you can either click on the icon showing two small boxes (see just below the arrow in Figure 1.1) or go to Tools<Global Options<Pane Layout. I prefer to have the console window above and the source below, but you can arrange as you wish. Also, as you can see, my background is black. This can also be adjusted in Global Options like many other options. Once you have the source window visible, you can type code in it, and then if you want to run a particular set of lines, select those lines, and then press the button on the upper right-hand side of the source window that says Run. Instead of pressing the button, you can also select the code to be run and (on Windows) press the control key and enter key simultaneously.
library(
"swirl
")
, and hit the enter key. Then type swirl()
, and hit the enter key. Next the website says, “the first time you start Swirl, you’ll be prompted to install a course. You can either install one of the recommended courses or visit our course repository for more options. There are even more courses available from the Swirl Course Network. If you follow the instructions it will bring you to a screen where you can choose the course you want to study”. The recommended course for this text is called R Programming: The basics of programming in R, and it is expected that you have completed up to and including the section on functions.1.1.3 Functions Tutorial
1.2 Functions
mean
(x).
x=1:5 mean(x) ## [1] 3
Sys.Date
().
Sys.Date() ## [1] "2018-10-02"
mean(x)
. The mean()
function requires an input. In fact, if we run it without one, we get an error message.mean() Error in mean.default() : argument "x" is missing, with no default
1.2.1 Writing Our First R Function
myFirstFunction <- function(){ print("Hi There!") }
- It needs a name.1
myFirstFunction
- You need to tell R that this name is the name of a function (as opposed to a text (character) variable, or a vector, or a list, or whatever else it might be).
function()
- You want the function to do something, and you put that inside the curly brackets
{}.
{ print("Hi There!") }
Hi There
!". Note that what is written between the curly brackets is referred to as the body of the function. The body of our function containsprint("Hi There!").
1.2.2 Defining the Function versus Running It
myFirstFunction()