Mathematics and Programming for Machine Learning with R
eBook - ePub

Mathematics and Programming for Machine Learning with R

From the Ground Up

William B. Claster

Share book
  1. 408 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mathematics and Programming for Machine Learning with R

From the Ground Up

William B. Claster

Book details
Book preview
Table of contents
Citations

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

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mathematics and Programming for Machine Learning with R an online PDF/ePUB?
Yes, you can access Mathematics and Programming for Machine Learning with R by William B. Claster in PDF and/or ePUB format, as well as other popular books in Informatique & Intelligence artificielle (IA) et sémantique. We have over one million books available in our catalogue for you to explore.

Information

Publisher
CRC Press
Year
2020
ISBN
9781000196993

Chapter 1

Functions Tutorial
  • ■ 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

Note that this text uses the following version of R: version 3.4.2 (2017-09-28). Generally later versions should provide the same results, but you may also install this version at the Comprehensive R Archive Network (CRAN, https://cran.r-project.org/).

1.1.1 Knowledge of R

It is assumed that the reader has gone through the tutorial offered through an R package called Swirl. You can find out more information at https://swirlstats.com/. The tutorial can be run inside RStudio, and it gives a very good course in R. It is expected that the reader has completed the tutorial through the section on functions.
According to the website, you can run the tutorial in RStudio by following the steps given below.
  • 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

To get things going, you should install R and install the free version of RStudio. Here are the steps to be followed.
  1. 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.
  2. Download and install the free version of RStudio from https://www.rstudio.com. The same remarks as above regarding “install as administrator”.
  3. Run RStudio.
  4. 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.
    image
    Figure 1.1 Initial configuration of RStudio.
  5. 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.
To run the tutorial, you need to install the Swirl package. The simplest way to install a package is to go to Tools>Install Packages. Then type in the name of the package you are looking for, in this case Swirl. Then click the install button. RStudio will download and install the package, and when it is finished, you will see the prompt symbol (looks like “>”) in the console. According to the website on Swirl, next type 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.
Note: We will often use RHS to stand for right-hand side of an equation and LHS to stand for the left-hand side of an equation.
Note: The author feels that there may be errors in the text. This is the cost of getting the text out in a reasonable period of time. He apologizes for any errors.

1.1.3 Functions Tutorial

It is assumed that you have gone through the RStudio Swirl tutorial at this point. Some of the following will be a review of the Swirl course.
Our tutorial will concentrate on building functions since this is a critical skill in the material that follows.

1.2 Functions

If you have used R at all, you have already worked with many functions. A function we often encounter is mean(x).
 x=1:5 mean(x) ## [1] 3 
Another easy function is the function to get the date on the computer you are using, Sys.Date().
 Sys.Date() ## [1] "2018-10-02" 
There is an important difference between these two functions. They both have names, and they both have parentheses, but there is nothing inside the parentheses when we run Sys.Date(); however, there is something inside the parentheses when we run 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 
Admittedly, R has error messages that are not so easy to understand, but if you skim them, you can sometimes figure out what kind of problem has occurred.

1.2.1 Writing Our First R Function

Let’s make this simple. To write a function is as simple as
 myFirstFunction <- function(){ print("Hi There!") } 
This shows the syntax of creating a function. We can divide the construction of this function into three parts. Before reading what these three parts are, look at the above statement and make a guess as to where the three parts might be. (I am not referring to the “<-” symbol as one of the parts, but of course that needs to be there.)
  1. It needs a name.1
     myFirstFunction 
  2. 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() 
  3. You want the function to do something, and you put that inside the curly brackets {}.
     { print("Hi There!") } 
This function will just print out a string: "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 contains
 print("Hi There!"). 
1 There are cases where it is not necessary to name a function. That topic is called anonymous functions. We will not discuss it here.

1.2.2 Defining the Function versus Running It

Although we define a function, nothing happens until we run it. Running a function is done by typing its name along with the succeeding parentheses.
 myFirstFunction() 
Note that in RStudio, if the definition is typed into the console and the enter key is hit, then the function is defined. However, if the definition is typed into the source panel, it won’t be defined until the code is run in the Source panel.
However, even after the function...

Table of contents