[New Book] Click to get The Beginner's Guide to Data Science!
Use the offer code 20offearlybird to get 20% off. Hurry, sale ends soon!

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.

Kick-start your project with my new book Machine Learning Mastery With Python, including step-by-step tutorials and the Python source code files for all examples.

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 code).

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

Assignment

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

Strings

Running the example prints:

Numbers

Running the example prints:

Boolean

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:

Functions

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:

Arithmetic

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.

Series

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:

DataFrame

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:

Summary

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.

Discover Fast Machine Learning in Python!

Master Machine Learning With Python

Develop Your Own Models in Minutes

...with just a few lines of scikit-learn code

Learn 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.

See What's Inside

24 Responses to Crash Course in Python for Machine Learning Developers

  1. Avatar
    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

    • Avatar
      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. Avatar
    Surya April 5, 2017 at 5:38 pm #

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

  3. Avatar
    anon September 22, 2017 at 6:43 am #

    Great post! Quick fix, the if-else-if is a bit wonky, ‘That is too fast’ will never print.

  4. Avatar
    Ahmed July 14, 2018 at 6:33 pm #

    How can I go to the next line without executing the code ?

    >>> value = 99
    >>> if value >= 99:
    … print (‘That is fast’)
    File “”, line 2
    print (‘That is fast’)
    ^
    IndentationError: expected an indented block
    >>>

  5. Avatar
    Ahmed July 17, 2018 at 2:16 pm #

    Thanks a lot , very clear explanation

  6. Avatar
    Carlos Morente August 4, 2018 at 8:24 pm #

    Hi Jason!

    Just writing you to say that you made a little mistake in the “running the example prints” for Strings, the first output should be ‘h’ and not ‘4’.

  7. Avatar
    Clint Laskowski December 11, 2018 at 10:58 pm #

    Really nice (and quick) introduction to Python, NumPy, Matplotlib, and Pandas. Looking into your book now 🙂

  8. Avatar
    laz65 December 6, 2019 at 10:10 am #

    many thanks jason 😉

  9. Avatar
    Robbie February 4, 2020 at 7:24 pm #

    Hi Jason,

    In the “IF-Then-Else Condition Example”, i would expect by changing the value to 300 the code should print ‘That is too fast’. However, the output is ‘That is fast’. Can you please explain?

    #Output
    That is fast

    • Avatar
      Jason Brownlee February 5, 2020 at 8:05 am #

      The first condition was executed first and passed.

      You can re-order the if-statements or use a switch/case statement.

  10. Avatar
    MoJeeza February 6, 2020 at 7:26 am #

    This should work..

  11. Avatar
    MoJeeza February 6, 2020 at 7:29 am #

    Somehow the indentation was lost during paste

Leave a Reply