Sub topic 1
What Machine Learning Is
Prerequisites
This chapter assumes you can read basic Python, work with arrays or tables of numbers, and recall high-school algebra, including the idea of a function. You should also know the mean and variance. No machine learning experience is needed.
A working definition
Machine learning (ML) is a way of building programs that get their behavior from data instead of from hand-written rules. You give an algorithm many examples. It adjusts an internal set of numbers until its outputs fit those examples. The result is a model, which is a function you can apply to new inputs.
A common short definition says a program learns from experience E on a task T if its performance P on T improves with E. Here E is the data, T is the prediction job, and P is a score you choose, such as accuracy.
Rules versus learning
In rule-based programming, you write the logic yourself. A spam filter might check for the phrase "free money" and flag the message. This works when you can state the rules, and it breaks when the rules are too many or too fuzzy to write down.
In machine learning, you supply examples and let the algorithm find the rule. For spam, you would give it thousands of messages marked "spam" or "not spam". The learned rule may weigh hundreds of word patterns that you would never list by hand.
| Aspect | Rule-based program | Machine learning |
|---|---|---|
| Input to the developer | Rules | Data |
| Where the logic comes from | Written by a person | Fitted by an algorithm |
| Handles fuzzy patterns | Poorly | Well, given enough data |
| Main risk | Missing cases | Learning the wrong pattern from the data |
Examples, features, and labels
Three terms appear throughout this tutorial.
- An example is one row of data, such as one flower or one email.
- A feature is one measurable property of an example, such as petal length.
- A label is the answer you want the model to predict, such as the flower species.
If a dataset has 150 flowers with four measurements each, it has 150 examples and four features. If each flower is also tagged with its species, those tags are the labels. Data without tags is unlabeled. Whether labels exist largely decides which kind of learning you can use.
Two main paradigms
The tutorial focuses on two paradigms, and later chapters cover each in detail.
- Supervised learning learns from labeled examples and predicts the label for new inputs. Example: predicting a house price from its size and location.
- Unsupervised learning has no labels. It looks for structure, such as groups of similar customers.
A first model
The code below trains a supervised classifier on the iris dataset. It holds back 25% of the examples as a test set, so the score reflects data the model has not seen.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0, stratify=y
)
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print(accuracy_score(y_test, predictions))X holds the features and y holds the labels. fit is the learning step, and predict applies what was learned. accuracy_score reports the fraction of test examples classified correctly.
Common mistakes
- Judging a model on its training data. A model can memorize its training examples, so only held-out data gives an honest score.
- Assuming more data fixes everything. If the features do not carry information about the label, no algorithm can recover it.
- Treating a learned model as correct by construction. It reproduces the patterns in its data, including biases and errors.
Exercise
A team wants to predict whether a customer will cancel a subscription. They have a table with one row per customer, columns for usage statistics, and a column recording whether the customer cancelled. Name the examples, the features, and the label. Is this supervised or unsupervised?
Show answer
Each customer row is an example. The usage statistic columns are the features. The "cancelled" column is the label. Because labels are available, this is supervised learning.
Exercise
Why does the code in this chapter score the model on X_test instead of X_train?
Show answer
The model was fitted to X_train, so it may have memorized those examples. The test set was never used for fitting, so its score estimates how the model performs on new data.