Setting Up Your Programming Assignment Environment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Setting Up Your Programming

Assignment Environment
The Machine Learning course includes several programming assignments which youll need to
finish to complete the course. The assignments require the Octave or MATLAB scientific
computing languages.
Octave is a free, open-source application available for many platforms. It has a text

interface and an experimental graphical one.


MATLAB is proprietary software but a free, limited license is being offered for the

completion of this course.

FAQ
Does it cost money?
While youre taking the course, both software packages are available free of charge. Octave is
distributed under the GNU MATLAB Public License, which means that it is always free to
download and distribute. MATLAB licenses are available for completing the programming
assignments in the course only. For any other purposes (like your own work after you complete
the course), MATLAB can be licensed to individuals or companies from Mathworks directly.

Is there a difference in quality?


There are several subtle differences between the two software packages. MATLAB may offer a
smoother experience (especially for Mac users), contains a larger number of functions, and can
be more robust to failure. However, the functions used in this course are available in both
packages, and many students have successfully completed the course using either.

How do I install one of them?


See installation instructions for Windows, Mac OS X (10.10 Yosemite and 10.9 Mavericks), other
Mac OS X, or GNU/Linux.

Installing Octave/MATLAB on Windows


Installing Octave on Windows
Octave on Windows cannot be used to submit programming assignments in this
course. The official installers do not support HTTPS, which we use to secure your submissions.

We are working with the Octave team to resolve this issue. In the meantime, please use
MATLAB.

Installing MATLAB on Windows


MathWorks is providing you access to MATLAB for use in your coursework. When planning your
activity, please note that access is valid for 120 days upon activating.
Use the link provided to download and install. You will need to log-in to your MathWorks account,
or create one if you do not have one. After starting the installer, accept all defaults and log-in to
your MathWorks account when prompted.

Installing Octave/MATLAB on Mac OS


X (10.10 Yosemite and 10.9 Mavericks)
Installing Octave on Mac OS X (10.10 Yosemite and 10.9 Mavericks)
Mac OS X has a feature called Gatekeeper that may only let you install applications from the Mac
App Store. You may need to configure it to allow the Octave installer. Visit your System
Preferences, click Security & Privacy, and check the setting to allow apps downloaded from
Anywhere. You may need to enter your password to unlock the settings page.

2. Download the Octave 3.8.0 installer. The file is large so this may take some time.
3. Open the downloaded image, probably named GNU_Octave_3.8.0-6.dmg on your computer,
and then open Octave-3.8.0-6.mpkg inside.
4. Follow the installers instructions. You may need to enter the administrator password for your
computer.
5. After the installer completes, Octave should be installed on your computer. You can
find Octave-cli in your Macs Applications, which is a text interface for Octave that you can use to
complete Machine Learnings programming assignments.
Octave also includes an experimental graphical interface which is called Octave-gui, also in your
Macs Applications, but we recommend using Octave-cli because its more stable.
Note: If you use a package manager (like MacPorts or Homebrew), we recommend you
follow the package manager installation instructions.

Installing MATLAB on Mac OS X (10.10 Yosemite and 10.9 Mavericks)

MathWorks is providing you access to MATLAB for use in your coursework. When planning your
activity, please note that access is valid for 120 days upon activating.
Use the link provided to download and install. You will need to log-in to your MathWorks account,
or create one if you do not have one. After starting the installer, accept all defaults and log-in to
your MathWorks account when prompted.

Installing Octave/MATLAB on Mac OS


X (10.8 Mountain Lion and Earlier)
Installing Octave on Mac OS X (10.8 Mountain Lion and Earlier)
If you use Mac OS X 10.9, we recommend following the instructions above. For other Mac OS X
versions, the Octave project doesnt distribute installers. We recommend installing Homebrew, a
package manager, usingtheir instructions.

Installing MATLAB on Mac OS X (10.8 Mountain Lion and Earlier)


MathWorks is providing you access to MATLAB for use in your coursework. When planning your
activity, please note that access is valid for 120 days upon activating.
Use the link provided to download and install. You will need to log-in to your MathWorks account,
or create one if you do not have one. After starting the installer, accept all defaults and log-in to
your MathWorks account when prompted.

Installing Octave/MATLAB on
GNU/Linux
Installing Octave on GNU/Linux
We recommend using your system package manager to install Octave.
On Ubuntu, you can use:

sudo apt-get update && sudo apt-get install octave


On Fedora, you can use:

sudo yum install octave-forge

Please consult the Octave maintainers instructions for other GNU/Linux systems.

Installing MATLAB on GNU/Linux


MathWorks is providing you access to MATLAB for use in your coursework. When planning your
activity, please note that access is valid for 120 days upon activating.
Use the link provided to download and install. You will need to log-in to your MathWorks account,
or create one if you do not have one. After starting the installer, accept all defaults and log-in to
your MathWorks account when prompted.

More Octave/MATLAB resources


Octave Resources
At the Octave command line, typing help followed by a function name displays documentation for
a built-in function. For example, help plot will bring up help information for plotting. Further
documentation can be found at the Octave documentation pages.

MATLAB Resources
At the MATLAB command line, typing help followed by a function name displays documentation
for a built-in function. For example, help plot will bring up help information for plotting. Further
documentation can be found at the MATLAB documentation pages.
MathWorks also has a series of videos about various MATLAB features:

Introduction to MATLAB
Learning Module

Learning Goals

What is MATLAB?

Introduce MATLAB

The MATLAB
Environment

Navigate the command line, workspace, directory, and editor

MATLAB Variables

Use the assignment operator to define scalar variables

MATLAB as a
Calculator

Perform arithmetic calculations with scalars and functions using MATLAB


syntax and order of operations.

Mathematical

Use MATLAB variables for input and output to functions. Examples include:

Functions

COS, SIN, EXP, and NTHROOT.

Vectors
Learning Module

Learning Goals

Creating Vectors via Concatenation

Create vectors by entering individual elements

Accessing Elements of a Vector

Access specific elements of a vector

Perform arithmetic calculations with vectors including

Vector Arithmetic

element-wise operations

Vector Transpose

Use the transpose operator to convert between row and


column vectors

Creating Uniformly Spaced Vectors

Use the colon operator syntax to create vectors given the

(The Colon Operator)

starting and ending values and the size of the interval

Creating Uniformly Spaced Vectors


(The LINSPACE Function)

Use the LINSPACE function to create a vector.

Visualization
Learning Module

Learning Goals

Line Plots

Create a line plot of a vector and customize plot markers and colors

Annotating Graphs

Label axes, add a title, and add a legend to a plot

Matrices and Arrays

Learning Module

Learning Goals

Creating Matrices

Create matrices by directly entering scalars

Array Creation Functions

Create larger matrices and vectors with built in MATLAB functions


such as ZEROS and EYE

Accessing Elements of and

Access elements of an array including entire columns or rows using

Array

row-column indexing.

Array Size and Length

Use built-in functions to determine array dimensions

Concatenating Arrays

Build larger arrays from smaller ones

Matrix Multiplication

Perform matrix multiplication and interpret error messages related to


incompatible dimensions.

Programming
Learning Module

Learning Goals

Using the MATLAB


Editor

Write a script in the MATLAB Editor, break code into sections to execute,
and find help on functions

Logical Operators

Use relational and logical operators to create logical variables for program
control

Conditional Data
Selection

Access and change elements for a vector the meet a specified criteria

If-Else Statements

Use if-else statements to control which lines of code are evaluated

For Loops

Repeat a sequence of commands a specified number of times

While Loops

Repeat a sequence of commands while a specified condition is true

Machine Learning Honor Code


We strongly encourage students to form study groups, and discuss the lecture videos
(including in-video questions). We also encourage you to get together with friends to watch the

videos together as a group. However, the answers that you submit for the review
questions should be your own work. For theprogramming exercises, you are welcome to
discuss them with other students, discuss specific algorithms, properties of algorithms, etc.; we
ask only that you not look at any source code written by a different student, nor show your
solution code to other students.

Guidelines for Posting Code in


Discussion Forums
Scenario 1: Code to delete
Learner Question/Comment: "Here is the code I have so far, but it fails the grader. Please help
me fix it."
Why Delete?: The reason is that if there is a simple fix provided by a student, a quick copy and
paste with a small edit will provide credit without individual effort.
Learner Question: A student substitutes words for the math operators, but includes the variable
names (or substitutes the equivalent greek letters ( for 'theta', etc). This student also provides a
sentence-by-sentence, line by line, description of exactly what their code implements. "The first
line of my script has the equation "hypothesis equals theta times X", but I get the following error
message...".
Why Delete?: This should be deleted. Spelling out the code in English is the same as using the
regular code.

Scenario 2: Code not to delete


Learner Question: How do I subset a matrix to eliminate the intercept?
Mentor Response: This probably would be okay, especially if the poster makes an effort not to
use familiar variable names, or to use a context which has nothing to do with the contexts in the
assignments.
It is clearly ok to show examples of Octave code to demonstrate a technique. Even if the
technique itself is directly applicable to a programming problem at hand. As long as what is typed
is cannot be "cut and pasted" into the program at hand.
E.g. how do I set column 1 of a matrix to zero? Try this in your Octave work area:
>> A = magic(3)

>> A(:,1) = 0
The above is always acceptable (in my understanding). Demonstrating techniques and learning
the language/syntax are important Forum activities.
Thank you to Machine Learning Mentor, Tom Mosher, for compiling this list

Subject: Confused about "h(x) = theta' * x" vs. "h(x) = X * theta?"

Text:
The lectures and exercise PDF files are based on Prof. Ng's feeling that novice programmers will
adapt to for-loop techniques more readily than vectorized methods. So the videos (and PDF files)
are organized toward processing one training example at a time. The course uses column
vectors (in most cases), so h (a scalar for one training example) is theta' * x.
Lower-case x typically indicates a single training example.
The more efficient vectorized techniques always use X as a matrix of all training examples, with
each example as a row, and the features as columns. That makes X have dimensions of (m x n).
where m is the number of training examples. This leaves us with h (a vector of all the hypothesis
values for the entire training set) as X * theta, with dimensions of (m x 1).
X (as a matrix of all training examples) is denoted as upper-case X.
Throughout this course, dimensional analysis is your friend.

Subject: Tips from the Mentors: submit problems and fixing program errors

Text:
This post contains some frequently-used tips about the course, and to help get your programs
working correctly.

The Most Important Tip:


Search the forum before posting a new question. If you've got a question, the chances are that
someone else has already posted it, and received an answer. Save time for yourself and the
Forum users by searching for topics before posting a new one.

Running your scripts:

At the Octave/Matlab command line, you do not need to include the ".m" portion of the script file
name. If you include the ".m", you'll get an error message about an invalid indexing operation. So,
run the Exercise 1 scrip by typing just "ex1" at the command line.
You also do not need to include parenthesis () when using the submit script. Just type "submit".
You cannot execute your functions by simply typing the name. All of the functions you will work
on require a set of parameter values, enter between a set of parenthesis. Your three methods of
testing your code are:
1 - use an exercise script, such as "ex1"
2 - use a Unit Test (see below) where you type-in the entire command line including the
parameters.
3 - use the submit script.

Making the grader happy:


The submit grader uses a different test case than what is in the PDF file. Your functions must
handle the general case. This means:
- You should avoid using hard-coded array indexes.
- You should avoid having fixed-length arrays and matrices.
It is very common for students to think that getting the same answer as listed in the PDF file
means they should get full credit from the grader. This is a false hope. The PDF file is just one
test case. The grader uses a different test case.
Also, the grader does not like your code to send any additional outputs to the workspace. So,
every line of code should end with a semicolon.

Getting Help:
When you want help from the Forum community, please use this two-step procedure:
1 - Search the Forum for keywords that relate to your problem. Searching by the function name is
a good start.
2 - If you don't find a suitable thread, then do this:
2a - Find the unit tests for that exercise (see below), and run the appropriate test. Attempt to
debug your code.

2b - Take a screen capture of your whole console workspace (including the command line), and
post it to the forum, along with any other useful information (computer type, Octave/Matlab
version, other tests you've tried, etc).

Debugging:
If your code runs but gives the wrong answers, you can insert a "keyboard" command in your
script, just before the function ends. This will cause the program to exit to the debugger, so you
can inspect all your variables from the command line. This often is very helpful in analysing math
errors, or trying out what commands to use to implement your function.

Unit Tests:
Each programming assignment has a "Discussions" area in the Forum. In this section you can
often find "unit tests". These are additional test cases, which give you a command to type, and
provides the expected results. It is always a good idea to test your functions using the unit tests
before submitting to the grader.
If you run a unit test and do not get the correct results, you can most easily get help on the
forums by posting a screen capture of your workspace - including the command line you entered,
and the results.

Having trouble submitting your work to the grader?:


- This section will need to be supplemented with info appropriate to the new submission system.
If you run the submit script and get a message that your identity can't be verified, be sure that
you have logged-in using your Coursera account email and your Programming Assignment
submission password.
- If you get the message "submit undefined", first check that you are in the working directory
where you extracted the files from the ZIP archive. Use "cd" to get there if necessary.
- If the "submit undefined" error persists, or any other "function undefined" messages appear, try
using the "addpath(pwd)" command to add your present working directory (pwd) to the Octave
execution path.

Found some errata in the course materials?


This course material has been used for many previous sessions. Most likely all of the errata has
been discovered, and it's all documented in the "Lecture Notes : Errata" section of the Course
Wiki. Please check there before posting errata to the Forum.
Error messages with fmincg()

The "short-circuit" warnings are due to use a change in the syntax for conditional expressions (|
and & vs || and &&) in the newer versions of Matlab. You can edit the fmincg.m file and the
warnings may be resolved.

Warning messages about "automatic broadcasting"?


See this link for info.

Warnings about "divide by zero"


These are normal in some of the exercises, and do not represent a problem in your function. You
can ignore them - Octave senses the issue and substitutes a +Inf or -Inf value so your program
continues to execute.

You might also like