Concepts / Introduction to Python for Machine Learning

Introduction to Python for Machine Learning

Python is the language used to call it, so it tells you what your everyday coding will look like.

  • Programming

Who This Book Is For

Before you open any technical book, it helps to ask a simple question: is this book written for someone like me? A prerequisites section answers that question by describing the reader the author has in mind and the starting point that reader should have. This book is aimed at people who already work with machine learning, or who want to start, and who want to build working solutions to real-world machine learning problems.

What do you think happens?

Does this book expect you to already know machine learning before you begin?

  • Yes, you must already have trained models

  • No, it introduces machine learning from the beginning

Reveal answer
Answer:

No, it introduces machine learning from the beginning

The book is introductory and requires no previous knowledge of machine learning or artificial intelligence. It builds these ideas from the ground up rather than assuming them.

The phrase current and aspiring matters. A current practitioner might already be building models at work and want a structured, end-to-end walkthrough. An aspiring practitioner might be just starting out and looking for a path from idea to finished application. The same book is meant to serve both, which tells you the material is designed to be followed from the ground up rather than assumed to be familiar territory.

The source also names scientists and researchers, as well as data scientists who build commercial applications, as people who will find these methods useful. Read this as a statement about breadth. The techniques are presented as tools that fit several kinds of work, not as something tied to a single job title.

Required Versus Recommended Background

The most reassuring part of the prerequisites is what the book does not ask for. It states that the book requires no previous knowledge of machine learning or artificial intelligence. If you have never trained a model, never studied the vocabulary of the field, and never encountered the term AI outside of news coverage, you are not locked out. The book does not expect you to arrive already fluent in the theory. Building that fluency is part of what the book offers, so when a concept appears, your job is to follow the reasoning rather than recall an earlier course on the topic.

The practical toolkit is Python, with scikit-learn as the central library. Two details are easy to blur together. Scikit-learn is the library at the center of the book's examples, so it tells you where the machine learning content lives. Python is the language used to call it, so it tells you what your everyday coding will look like. Beyond these, the source recommends NumPy and matplotlib. NumPy is the numerical library the book leans on for working with arrays of numbers, and matplotlib is the plotting library.

Background itemWhat the source saysStatus for this book
Machine learning or AI knowledgeNo previous knowledge is required; the book introduces these ideas from the beginningNot required
PythonThe book focuses on Python; familiarity is helpful rather than strictly requiredHelpful, not strictly required
NumPySomewhat familiar knowledge gets the most out of the bookRecommended at a comfort level
matplotlibSomewhat familiar knowledge gets the most out of the bookRecommended at a comfort level
scikit-learnThe central library of the book's examplesThe book's own focus

Required and recommended background for this book

Checking Your Readiness Before You Start

The source describes the NumPy and matplotlib requirement as a comfort level. A useful self-check is to look at a short piece of NumPy or matplotlib code and ask whether you can read every line and predict what it will do. If you can, you are at roughly the level the source describes. If several lines look unfamiliar, the source suggests spending time with NumPy and matplotlib before starting the main chapters. This check is a guide, not a required exam.

What Machine Learning Means

Definition

Machine learning is a field of study that trains algorithms to make predictions or decisions based on data.

The central activity of the field is training. An algorithm is trained so that it can later make predictions or decisions from data. Nearly every idea in this course is a refinement of that definition, so it helps to hold it in mind before looking at any code. The introduction is organized around two goals: understanding the fundamental concepts and applications of machine learning, and learning the advantages and shortcomings of widely used machine learning algorithms. This article focuses on the first goal. The second is only previewed here, since the advantages and shortcomings are not spelled out in this introduction.

Example datacases with known answersTrainingalgorithm adjusted usingdataTrained modelholds what was learnedPrediction ordecisionNew caseanswer not yet knownfeedsproducesapplied to new caseinput
Read from left to right: example data goes into training, training produces a trained model, and the model is applied to a new case to produce a prediction or decision. The new case matters most, because the algorithm was not trained on it.

Training Versus Prediction in a Threshold Example

Training and prediction are two different moments in the life of a machine learning system. Training uses data that already has answers to shape the algorithm. Prediction uses that shaped algorithm on a case whose answer is unknown. Mixing these two moments up is one of the most common early confusions.

The following example is an illustration written for this article, not material taken from the source. It uses a deliberately simple algorithm that learns a single number, a threshold, from labeled examples. Each labeled example is a value paired with a yes or no answer.

Worked example

Learning a Threshold and Predicting New Values

Training examples: 2 is labeled no, 4 is labeled no, 6 is labeled yes, and 8 is labeled yes. The algorithm learns a threshold as the smallest value labeled yes in the training examples. After training, what does it predict for 5 and for 7?

  1. 1
    Training step

    The algorithm reads the labeled examples. The yes-labeled values are 6 and 8, and the smallest of them is 6, so the learned threshold is 6.

  2. 2
    Store the learned value

    The threshold variable now holds the number 6. The training examples are no longer needed to hold this value.

  3. 3
    Prediction for 5

    The new case 5 is below the threshold of 6, so the prediction is no.

  4. 4
    Prediction for 7

    The new case 7 is at or above the threshold of 6, so the prediction is yes.

Result

After training, the algorithm predicts no for 5 and yes for 7, using only the learned threshold of 6.

How Training Changes Program State

The before and after states show what training actually does in this example. Before training, the program holds the labeled examples and a threshold variable that has no value yet. During training, the algorithm reads the examples and sets the threshold variable to the learned number. The examples themselves never change. What changes is the threshold, which goes from having no value to holding a learned number.

Thresholdno value yetThreshold6Labeled examples2, 4, 6, 8 with answersLabeled examples2, 4, 6, 8 with answersset by trainingtraining input
The labeled examples stay the same. Only the threshold variable changes, from having no value to holding the learned number 6.

Once the number exists, the prediction step can use it on any new value, such as 5 or 7, without looking at the original examples again. The state of the program after training is therefore different in one specific way: it holds a learned value that the prediction step relies on. Tracing this change is the key to understanding what a trained model holds.

Mistakes Beginners Make About Learning From Data

  • Common mistake

    Treating training and prediction as the same step

    What it looks like

    Assuming that every time the program classifies a new value, it retrains on the original examples.

    Why it fails

    Training uses labeled data to shape the algorithm once. Prediction applies the shaped algorithm to a case whose answer is unknown.

    Do this instead

    Name the two moments separately: training produces a trained model, and prediction applies that model to a new case.

  • Common mistake

    Believing the trained model is a stored copy of the examples

    What it looks like

    Thinking that after training, the program still needs the list of 2, 4, 6, and 8 to answer 5 or 7.

    Why it fails

    In the threshold example, the prediction step uses only the learned number 6 and does not look at the original examples again.

    Do this instead

    Look for the learned value the algorithm stores, since that is what the prediction step depends on.

  • Common mistake

    Assuming you need prior machine learning knowledge before starting

    Why it fails

    The source states that no previous knowledge of machine learning or AI is required and that the book introduces these ideas from the beginning.

    Do this instead

    Follow the reasoning of each concept as it appears rather than trying to recall an earlier course.

  • Common mistake

    Thinking you must master NumPy and matplotlib first

    Why it fails

    The source describes the needed level as somewhat familiar, which it calls a comfort level rather than expert mastery.

    Do this instead

    Review syntax where you feel unsure, and keep going.

Trace a Threshold Through a New Dataset

EASY

A threshold algorithm was trained on labeled examples and learned the threshold 10. Using only that learned value, predict the answer for the new cases 9 and 12. Then state what the program holds before and after training.

Hints
  • Compare each new case to the learned threshold, not to the original training examples.
  • Before training, the threshold variable has no value. After training, it holds 10.

Key Takeaways

  • The book is introductory and is aimed at current and aspiring machine learning practitioners who want to build real-world solutions, with no prior machine learning or AI knowledge required.
  • Python is helpful, and somewhat familiar knowledge of NumPy and matplotlib is recommended as a comfort level; scikit-learn is the central library of the book's examples.
  • Machine learning is a field of study that trains algorithms to make predictions or decisions based on data.
  • Training uses labeled data to produce a trained model that holds what was learned; prediction applies that model to a new case whose answer is unknown.
  • After training, the program's state changes: a learned value is stored and used by the prediction step without reexamining the original examples.

Related concepts

More concepts starting with I