Thursday 28 March 2024

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.

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.

No comments:

Post a Comment

Connect broadband

How to Perform Feature Selection With Machine Learning Data in Weka

 Raw machine learning data contains a mixture of attributes, some of which are relevant to making predictions. How do you know which featu...