FOUNDATIONS OF MACHINE LEARNING
Course info
Course materials
FAQ
Course materials
/
Section
Running Code
A very important part of this course is to solve small programming exercises to get hands-on experience with various machine learning methods and to illustrate the theory in practice. Programming exercises are given in code blocks, as shown below.
You can run the code in the cell by clicking either Run or Submit. In either case the code will be executed and saved. The difference between the two options is that Submit will run your code against a number of tests to verify that you have successfully completed the exercise. If the code passes the tests, the exercise will be marked as complete.
Try to run the code below. This should print the sentence “This is the output from the code cell.”.
The output from the code is shown in the box below the cell. This includes printouts, plots and error messages. Try changing the printed text above to something else, like “Hello World!”, and click Submit again. This should trigger an error message, stating that your code did not pass the test.
Where is the Code Run?
Every time you load a new exercise page (or subsection) and run a code cell, it will connect to a LiU-hosted service called Binder (see mybinder.org for a similar, publically hosted, service). Binder gives us the possibility to run code in the cloud. However, connecting to Binder can take anything from a few seconds to a couple of minutes (if you are unlucky). If you get an error message saying that the connection failed, simply try again. Fortunately, you only need to establish a new connection to Binder for the first code cell on the page. For consecutive code cells the same connection will be used and they should therefore run quickly – unless you do some heavy number crunching!
There can also be multiple code cells in the same exercise block, in which case you need to finish all of them for the exercise to be marked as complete.
Start by running the code below, which will assign the value 5 to the variable x
.
Code cells in the same subsection share variables. Hence, you can run the code below to print the value of the variable that was defined above.
This is often a convenient property, since it means that you can reuse code from previous code cells when solving the exercises.
However, it is important to keep in mind that if you change something in a previous code cell, this can effect the results of the consecutive ones.
As an example, change the value of x
to something else than 5 above, Run the first code cell, and then Submit the second code cell again. This should now result in an error message.
You might have noticed that there is a third button in each code block: Restart Kernel. This button can be used if your program gets stuck in an infinite loop. Pressing Restart Kernel will restart your connection to Binder, where the code is run. This means that you will get a fresh environment to run your code. Note however that this also means that any variables and functions previously defined will not be available. So if you use this button make sure to go back and re-run previous code blocks in the exercise. Likely you will never have to use the Restart Kernel button, but it is good to know that it is there!
Later on, when solving the actual coding exercises, we will often ask you to complete some missing parts of the code.
This is tyically indicated by a comment, # TODO
, as well as number of underscores _____
in the code template. These underscores should then be replaced
with your solution.
You can also press Reset to recover to the original code template.
Python
The programming language that you have used above, and that will be used throughout the course, is known as Python. It is not necessary to be a Python guru in order to solve the exercises and complete the course, but they do require some basic understanding of the language (and of programming in general). If you are a complete beginner to Python, we recommend spending some time getting acquainted with the language before proceeding, for instance by taking one of the tutorials/courses available at: learnpython.org (focus on the parts from ‘Hello World!’ to ‘Functions’ + ‘Numpy Arrays’), futureskill.com or py4e.com.
The code block below shows a few basic Python commands. By the way, note that this code block is given in an infobox and is thus not part of an exercise (there is no Submit button).
We will occasionally use such code blocks to illustrate some Python code, or simply to provide you with a blank workspace where you can run code and do computations.
This webpage contains the course materials for the course ETE370 Foundations of Machine Learning.
The content is licensed under Creative Commons Attribution 4.0 International.
Copyright © 2021, Joel Oskarsson, Amanda Olmin & Fredrik Lindsten