Crash Course in Python for Machine Learning Developers

You do not need to be a Python developer to get started using the Python ecosystem for machine learning.

As a developer who already knows how to program in one or more programming languages, you are able to pick up a new language like Python very quickly. You just need to know a few properties of the language to transfer what you already know to the new language.

In this post, you will get a crash course in Python and the core libraries needed for machine learning. Namely: NumPy, MatPlotLib and Pandas.

This will be just enough information to help you read and understand code Python code examples for machine learning and start developing your own scripts. If you already know a little Python, this post will be a friendly reminder for you.

Let’s get started.

  • Update Mar/2017: Updated all print statements to work with Python 2 and Python 3.
Crash Course in Python for Machine Learning Developers

Crash Course in Python for Machine Learning Developers
Photo by John Clouston, some rights reserved.

Python Crash Course

When getting started in Python you need to know a few key details about the language syntax to be able to read and understand Python code. This includes:

  • Assignment
  • Flow Control
  • Data Structures
  • Functions

We will cover each of these topics in turn with small standalone examples that you can type and run.

Remember, whitespace has meaning in Python.

Need help with Machine Learning in Python?

Take my free 2-week email course and discover data prep, algorithms and more (with sample code).

Click to sign-up now and also get a free PDF Ebook version of the course.

Start Your FREE Mini-Course Now!


As a programmer, assignment and types should not be surprising to you.


Running the example prints:


Running the example prints:


Running the example prints:

Multiple Assignment

Running the example prints:

No Value

Running the example prints:

Flow Control

There are three main types of flow control that you need to learn: If-Then-Else conditions, For-Loops and While-Loops.

If-Then-Else Condition Example

Running this example prints:

For-Loop Example

Running this example prints:

While-Loop Example

Running this example prints:

Data Structures

There are three data structures in Python that you will find the most used and useful. They are tuples, lists and dictionaries.

Tuple Example

Tuples are read-only collections of items.

Running the example prints:

List Example

Lists use the square bracket notation and can be index using array notation.

Running the example prints:

Dictionary Example

Dictionaries are mappings of names to values, like a map. Note the use of the curly bracket notation.

Running the example prints:


The biggest gotcha with Python is the whitespace. Ensure that you have an empty new line after indented code.

The example below defines a new function to calculate the sum of two values and calls the function with two arguments.

Running the example prints:

NumPy Crash Course

NumPy provides the foundation data structures and operations for SciPy. These are arrays (ndarrays) that are efficient to define and manipulate.

Create Array

Running the example prints:

Access Data

Array notation and ranges can be used to efficiently access data in a NumPy array.

Running the example prints:


NumPy arrays can be used directly in arithmetic.

Running the example prints:

There is a lot more to NumPy arrays but these examples give you a flavor of the efficiencies they provide when working with lots of numerical data.

Matplotlib Crash Course

Matplotlib can be used for creating plots and charts.

The library is generally used as follows:

  1. Call a plotting function with some data (e.g. plot()).
  2. Call many functions to setup the properties of the plot (e.g. labels and colors).
  3. Make the plot visible (e.g. show()).

Line Plot

The example below creates a simple line plot from one-dimensional data.

Running the example produces:

Simple Line Plot in Matplotlib

Simple Line Plot in Matplotlib

Scatter Plot

Below is a simple example of creating a scatter plot from two-dimensional data.

Running the example produces:

Simple Scatter Plot in Matplotlib

Simple Scatter Plot in Matplotlib

There are many more plot types and many more properties that can be set on a plot to configure it.

Pandas Crash Course

Pandas provides data structures and functionality to quickly manipulate and analyze data. The key to understanding Pandas for machine learning is understanding the Series and DataFrame data structures.


A series is a one-dimensional array where the rows and columns can be labeled.

Running the example prints:

You can access the data in a series like a NumPy array and like dictionary, for example:

Running the example prints:


A data frame is a multi-dimensional array where the rows and the columns can be labeled.

Running the example prints:

Data can be index using column names.

Running the example prints:


You have covered a lot of ground in this post. You discovered basic syntax and usage of Python and four key Python libraries used for machine learning:

  • NumPy
  • Matplotlib
  • Pandas

You now know enough syntax and usage information to read and understand Python code for machine learning and to start creating your own scripts.

Do you have any questions about the examples in this post? Ask your questions in the comments and I will do my best to answer.

Frustrated With Python Machine Learning?

Master Machine Learning With Python

Develop Your Own Models in Minutes

…with just a few lines of scikit-learn code

Discover how in my new Ebook:
Machine Learning Mastery With Python

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, modeling, tuning, and much more…

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.

4 Responses to Crash Course in Python for Machine Learning Developers

  1. MouChenghao September 9, 2016 at 9:51 pm #

    I guess there is a wrong output in the last example, where the correct result should be like this:

    one column: a 1
    b 4
    Name: one, dtype: int64
    one column: a 1
    b 4
    Name: one, dtype: int64

    • Jason Brownlee September 10, 2016 at 7:14 am #

      You are right, fixed. I have edited it for readability and broken the output. Sorry.

  2. Surya April 5, 2017 at 5:38 pm #

    Hi Jason, on first string example printing data[0] would be “h” and not 4.

Leave a Reply