Machine learning essentials

Sign in to save progress
0:00
0:00

On this lesson: How machines see the world in features

The discussion is for premium members.Go premium
  • Be the first to say something about this lesson.

Prefer reading? Every lesson in this course as text

Read this lesson as text: How machines see the world in features

How machines see the world in features

What exactly is a feature? Let's try to understand. In a simple way. A machine cannot look at a house, or a person, or a photo.

It does not see objects at all. All it can do is math, so everything must arrive as numbers. So we describe the thing by a few of its measurable properties. For a house, that might be its size, its number of bedrooms, and its age.

Each property turns into a single number. Size in square feet. Bedrooms, a count. Age in years.

Each of these numbers is called a feature. Line up the features for one house into a row. That row of numbers is its feature vector. One row stands for one example.

Gather many houses and you get a table. Every row is one example, and every column is one feature, the same measurement for all of them. Think of each feature as an axis. Then every example is a point in that space.

Similar things land close together, and different things land far apart. The features you pick decide what the machine can notice. Good features expose the pattern. The wall color of a house tells you little about its price.

The same trick fits everything. A photo becomes its pixel brightness values. A sentence becomes counts of words. Sound becomes numbers sampled over time.

So a feature is one measured number about a thing. A list of them describes that thing. And that list is all the machine ever sees. Quick check now.

One question is coming up. Let's see if it clicked.

Read this lesson as text: Teaching a model with labeled examples

Teaching a model with labeled examples

What exactly is a labeled example? Let's try to understand. In a simple way. Say you want a model to tell cats from dogs.

You show it a photo and tell it the answer. Cat. Photo plus answer is a labeled example. The photo is the input.

The answer, cat or dog, is the label. Picture it as the correct answer on the back of a flashcard. One example is not enough. So you gather thousands, each already tagged with the right answer.

That pile is your training data. Learning from answers like this has a name. Supervised learning. A teacher stands beside the model, holding the answer key the whole time.

The model reads example after example and finds the pattern linking pictures to labels. Pointy ears and whiskers lean toward cat. Now the real test. Show it a photo it has never seen, with no label.

Answer cat correctly, and it has learned to generalize, not memorize. Take the labels away and it all falls apart. With no answers to check against, the model has nothing to learn from. Labels make teaching possible.

So where do labels come from? Usually people, tagging each photo by hand. That work is slow and costly, so good labels are precious. And the model trusts them completely.

Label enough dogs as cats, and it will confidently call a dog a cat. Bad answers in, bad answers out. So, a labeled example is an input with its correct answer. Gather many, and the model learns to answer on its own.

Quick check now. One question is coming up. Let's see if it clicked.

Read this lesson as text: Predicting a number by fitting a line

Predicting a number by fitting a line

How does fitting a line to predict a number work? Let's try to understand. In a simple way. Some questions have a number as the answer.

A house price. Tomorrow's temperature. Not a yes or no, but an amount on a sliding scale. That is called regression.

Start with examples you already know. Each one becomes a dot on a chart. The feature runs along the bottom, the number you want up the side. Now lay one straight line through the whole cloud of dots.

That single line is the entire model. Nothing else is stored. To predict, give it a new house size. Go up from that point until you hit the line, then across to read the price.

The line turns any input into a number. The line carries two numbers of its own. The slope, how steeply it climbs, how much price rises for each extra step. And the intercept, its value when the feature is zero.

Real problems have many features. Size, rooms, age. Each gets its own slope, its own weight. The prediction is every feature times its weight, all added up.

Still one flat rule, in more dimensions. But which line? The one that sits as close as possible to all the dots. The best fit.

It will never touch every point, and that leftover gap is the story of coming lessons. So, fitting a line means choosing slopes and an intercept so one straight rule maps features to a number. Plug in the features, read off the answer, done. Quick check now.

One question is coming up. Let's see if it clicked.

Read this lesson as text: How a model measures its own error

How a model measures its own error

How does a model measure its own error? Let's try to understand. In a simple way. A model makes a prediction.

For your training examples, you already know the true answer. Lay the two side by side, the guess and the truth. For a single example, the error is the gap between guess and truth. Predict two hundred, the truth was two hundred twenty; you missed by twenty.

That gap is the residual. But be careful. Some guesses land too high, some too low. Plus twenty here, minus twenty there.

Add them and they cancel to zero, as if nothing was wrong. The fix is to square each gap. Squaring throws away the sign, so every miss counts as positive. Big misses get punished harder.

A gap of two becomes four, a gap of ten becomes a hundred. Now do that for every example: square each gap, add them all up, then divide by the count. That average is the mean squared error. The whole model, thousands of predictions, collapses to one number.

A single score for how wrong it is. Zero means a perfect fit; the bigger it grows, the worse the model. This score has a name. The loss, or the cost.

Squared error is the classic choice, but not the only one. You could average the plain distances. Each is a different yardstick for wrongness. So, measuring error turns every miss into one honest number.

Square the gaps so they cannot cancel, then average. That loss is what the model will try to shrink. Quick check now. One question is coming up.

Let's see if it clicked.

Read this lesson as text: How a model learns from its mistakes

How a model learns from its mistakes

How does a model learn from its mistakes? Let's try to understand. In a simple way. Inside the model is a set of adjustable dials, called weights.

Turn the dials one way or another, and its prediction changes. At the start the dials sit at random, so the very first guess is usually wrong. It predicts dog when the answer is cat. Line the guess up against the true answer.

The distance between them is the error, one number saying how far off it was. Now the key question. For each dial, would nudging it up or down make that error smaller? Answer that, and you know which way to turn.

That direction has a name. The gradient. It is the slope of the error. It points uphill, so to shrink the mistake we step the other way.

So we turn every dial a small step down that slope. Small, on purpose. One example should nudge the model gently, never yank it. Then repeat.

Next example, guess, measure the error, nudge the dials again. Over thousands of rounds, the error slowly shrinks. Picture the error as a landscape of hills and valleys. Each little step rolls the model downhill, toward the lowest point, the best dials.

Step size matters. Too big, and it leaps over the valley and bounces around. Too small, and it crawls. This dial is the learning rate.

So, a model learns by turning its dials a little against each mistake. Guess, measure the error, step downhill, and repeat until it settles. Quick check now. One question is coming up.

Let's see if it clicked.

Read this lesson as text: Predicting a simple yes or no answer

Predicting a simple yes or no answer

How does predicting a simple yes or no answer work? Let's try to understand. In a simple way. Some questions do not want a number.

Is this email spam, or not? Is a photo a cat, or not? The answer is one of two buckets. That job is called classification.

It starts just like fitting a line. Multiply each feature by its weight, add them up. That gives one raw score, and it can be any number at all. But a raw score is not a yes or no.

Minus four, or plus twelve. We need to turn that loose number into something bounded. So we push the score through an S shaped curve, the sigmoid. It squashes any number, however big or small, into the range zero to one.

Now read that value as a probability. The chance the answer is yes. Zero point nine is very likely yes. Zero point one, very likely no.

Then pick a cutoff, usually one half. Above the cutoff, call it yes. Below it, call it no. The soft probability becomes a hard decision.

The exact place where the score tips from no to yes is the decision boundary. On one side, everything is a yes. On the other, a no. And the weights?

Learned the same way as before. Nudge them so the predicted chances line up with the true labels, closer each round. So, a yes or no model scores the features, squashes that score into a probability, then cuts at a threshold. That is logistic regression.

Quick check now. One question is coming up. Let's see if it clicked.

Read this lesson as text: Making decisions one question at a time

Making decisions one question at a time

How does a decision tree work? Let's try to understand. In a simple way. You have played twenty questions.

You narrow things down by asking one yes or no question at a time. A decision tree does exactly that. It starts at the very top, in a box called the root. It asks one question about one feature.

For a fruit, maybe, is it heavier than one hundred grams? That question splits everything into two. Answer yes, follow one branch. Answer no, follow the other.

Each branch leads to a new box. The new box asks another question, maybe about color. Is it yellow? Each question carves the group down smaller.

Eventually a box stops asking and just gives an answer, apple or lemon. That final box is a leaf, and its answer is the prediction. But how does it pick each question? It tries many splits and keeps the one that best separates the classes, so each side is as pure as possible.

It builds the tree top down. Pick the best question, split, then repeat inside each branch. One greedy choice after another. Let it grow too deep and it memorizes every quirk of the training data.

So we stop early, or prune branches back. The nice part, you can read it. Follow the path of questions to see exactly why it chose that answer. Few models explain themselves so clearly.

So, a decision tree is a chain of yes or no questions. Follow the branches down to a leaf, and the leaf hands you the prediction. Quick check now. One question is coming up.

Let's see if it clicked.

Read this lesson as text: Why you hide data from your model

Why you hide data from your model

What exactly is a held-out test set? Let's try to understand. In a simple way. Here is what you actually want.

A model that works on new data it has never seen, not just the examples it trained on. But if you test it on the very same data it learned from, a model that only memorized the answers scores perfectly. You learn nothing real. So before training, you split your data into two piles.

A large training set, and a smaller test set held aside. That test set is locked away. The model never sees it while it learns. Those examples stay hidden the whole time.

Training happens only on the first pile. The model tunes its dials to fit those examples, exactly as before. Once training is finished, you bring out the hidden pile. You ask the model to predict on examples it has never met.

How well it does on that fresh pile is your honest estimate of real world performance. Memorizing cannot fake this score. One firm rule. Never let a test example slip into training.

Even a peek leaks the answer, and the test stops being fair. How big is each pile? Often most of the data trains the model, and a slice is held back. Eighty and twenty is common.

So, you hide a test set to see if the model truly learned. Train on one pile, grade on the pile it never saw. Quick check now. One question is coming up.

Let's see if it clicked.

Read this lesson as text: When a model memorizes instead of learns

When a model memorizes instead of learns

What exactly is overfitting? Let's try to understand. In a simple way. A model can score well on its practice data in two different ways.

It can truly learn the pattern behind the examples. Or it can simply memorize the answers. Real data carries two things. A true pattern, the signal.

And random quirks, the noise. Learning catches the pattern. Memorizing traces every quirk as if it mattered. Picture dots on a chart.

A smooth line through their overall trend has learned the pattern. A wiggly line that bends through every single dot has memorized them instead. On the training data, the wiggly line looks flawless. It passes through every point, so its error is zero.

That perfect score is the trap. Now show it one fresh point it never saw. The smooth line lands close. The wiggly line shoots far off, because it learned the quirks, not the trend.

Here is the tell. As training goes on, error on the practice data keeps falling. But error on new data drops, then turns back up. That widening gap is overfitting.

Why does it happen? A model with too many dials and too few examples has room to memorize. Training too long lets it keep bending to fit noise. So you rein it in.

Give it more examples. Use a simpler model with fewer dials. Or stop training early, before it starts chasing the noise. So, overfitting is memorizing the noise instead of learning the pattern.

It shines on old data and stumbles on new. Watch the gap. Quick check now. One question is coming up.

Let's see if it clicked.

Read this lesson as text: Judging a classifier's real performance

Judging a classifier's real performance

How does judging a classifier's real performance work? Let's try to understand. In a simple way. The simplest score is accuracy.

Of every prediction the model made, what fraction did it get right. Easy to compute, easy to trust too much. Here is the trap. Say one in a hundred emails is spam.

Label every message not spam and you are right ninety nine percent of the time, yet catch zero spam. So look closer. Every prediction lands in one of four boxes. A caught spam is a true positive.

A flagged good email, a false positive. A missed spam, a false negative. That grid is the confusion matrix. From those boxes come two honest numbers.

Precision asks, of the emails you flagged as spam, how many really were. It punishes false alarms. Recall asks the opposite. Of all the spam that truly existed, how many did you catch.

It punishes the ones you let slip. These two pull against each other. Flag more aggressively and you catch more spam, but more good mail gets flagged. Precision and recall trade off.

Which matters more depends on the cost. Missing a cancer is far worse than a false alarm, so there you push recall high, even if precision drops. When you want a single number for both, people use the F one score. It blends precision and recall, and stays low unless both are strong.

So, accuracy alone can fool you. Split the mistakes into false alarms and misses, then read precision and recall to see how the model truly does. Quick check now. One question is coming up.

Let's see if it clicked.

Read this lesson as text: Why many weak models beat one strong one

Why many weak models beat one strong one

What exactly is an ensemble? Let's try to understand. In a simple way. Ask one person to guess an ox's weight, and they miss.

Average a whole crowd's guesses, and it lands surprisingly close. A weak model is just like one of those guessers. On its own, it is only a little better than a coin flip. The trick is not to fix that one model.

Instead, we train a whole team, hundreds of these simple models together. Then, for a new example, every model casts its vote. We tally them and take the majority. That answer is the ensemble's prediction.

Here is why it works. Each model makes different mistakes. Their wrong guesses scatter and cancel out. Their right guesses agree, and that signal adds up.

But there is a catch. The models must actually differ. If they all make the same mistake, voting changes nothing. Diversity is the whole point.

So how do we make them differ? One way is bagging: give each model its own random slice of the data. Many such trees form a random forest. Another way is boosting.

The models learn in sequence, and each new one focuses on the examples the last one got wrong. One clever model tries to be right by itself, and can lean too hard on quirks in the data. A crowd of simple ones leans on nobody, staying steadier. So, an ensemble combines many weak models into one strong answer.

Different mistakes cancel, shared signal adds up, and the crowd beats the expert. Quick check now. One question is coming up. Let's see if it clicked.

Read this lesson as text: Finding hidden groups in unlabeled data

Finding hidden groups in unlabeled data

What exactly is clustering? Let's try to understand. In a simple way. In supervised learning, every example came with a label, the right answer.

Here the data arrives bare. Just points, with nobody telling us what they are. But plot those points, and your eye catches something. Some of them huddle close together.

Others sit far away. Those natural clumps are the hidden groups. The whole idea rests on distance. Points that sit close together are treated as similar.

Points far apart are treated as different. A popular method is called k-means. It begins by dropping a few group centers at random. Say we want three groups, so three centers.

Then comes the assign step. Every point looks around and joins the center nearest to it. That splits the data into rough groups. Next, the update step.

Each center slides to the middle of the points it owns, their average position. Now repeat. Reassign every point, move every center, again and again, until the centers stop shifting. The groups have settled.

Notice that no labels were ever used. Nothing told the method which grouping is correct. It found the structure on its own. That is unsupervised learning.

Why bother? These hidden groups become customer segments, batches of similar documents, or odd points that fit no group at all. So, clustering sorts unlabeled points into groups by closeness. Drop centers, assign, update, repeat, and the hidden structure appears.

Quick check now. One question is coming up. Let's see if it clicked.

Read this lesson as text: How to pick the right number of groups

How to pick the right number of groups

How does choosing the number of groups work? Let's try to understand. In a simple way. Clustering finds groups on its own.

But it never decides how many. You hand it that number. We call it k. Pick k too small, and separate groups get crushed together.

Two real clusters share one center, and their difference disappears. Pick k too large, and one natural group gets chopped into shreds that mean nothing. At the limit, every point becomes its own tiny group. So how do we judge?

Measure how tight the groups are. Add up every point's distance to its own center. That total is the spread. But here is the trap.

More groups always shrink the spread. At one group per point, it hits zero. So the smallest spread is a bad guide. The fix is to plot the spread against k.

The curve drops steeply at first, then bends and flattens into a gentle tail. That bend is the elbow. It marks where adding another group stops helping much. That k is the sweet spot.

A second gauge asks: does each point sit closer to its own group than to the next nearest one? The cleanest fit wins. That score is the silhouette. And often the best answer comes from outside the data.

If the team needs four customer segments, then four is your k. The purpose decides. So, k is yours to choose. Too few blurs the groups, too many shatters them.

Watch the spread, find the elbow, let the goal settle it. Quick check now. One question is coming up. Let's see if it clicked.

Read this lesson as text: Putting your data on a level playing field

Putting your data on a level playing field

What exactly is feature scaling? Let's try to understand. In a simple way. Picture two features of a house.

Its size, in square feet, runs into the thousands. Its bedroom count is a single digit. Same table, wildly different scales. Now suppose the model judges examples by distance, by how close their numbers are.

The size feature is so large it swamps the total. Bedrooms barely register. That is unfair. Size is not truly more important, it just has bigger numbers.

So we rescale each feature onto a shared range, giving every one an equal say. One way is min-max scaling. For each feature, subtract its smallest value, then divide by its range. Every value now lands between zero and one.

Another way is standardization. Subtract the feature's average, then divide by its spread. Now each feature is centered at zero, with a typical spread of one. Scaling also speeds up learning.

With lopsided scales, the error surface is a stretched valley, and gradient descent zigzags. Rescaled, it becomes a round bowl, and steps head straight down. One catch. Measure the minimum, maximum, or average from the training data only.

Then apply those same numbers to new data. Peeking at the test set leaks information. Does every model need this? Distance based models and gradient descent really do.

Decision trees do not, since they split one feature at a time. So, feature scaling puts every feature on a level playing field. Rescale each to a shared range, and no single big-numbered feature can dominate the rest. Quick check now.

One question is coming up. Let's see if it clicked.

Read this lesson as text: Creating smarter features from raw data

Creating smarter features from raw data

What exactly is feature engineering? Let's try to understand. In a simple way. Your raw data arrives in plain columns.

A price, a floor area, a timestamp. The pattern you want often hides between them, not inside any single column. A model only uses the features you give it. It will not invent new ones.

Hand it price and area separately, and it never builds price per square foot by itself. So we build that feature ourselves. Divide price by area, and one number now captures value per square foot. That ratio can predict better than either column alone.

We can also pull hidden pieces out. A raw timestamp becomes the day of week, the hour, and whether it is a weekend. Now Friday nights can stand out. We can reshape a single feature too.

Take the logarithm of a skewed value like income, so a few huge earners stop drowning out everyone else. How do you know which features to build? Domain knowledge. What you understand about the problem points to the combinations that actually matter.

This is where real gains hide. A simple model with smart features often beats a fancy model fed raw columns. The effort moves to the data. One warning.

Never build a feature from information you would not have at prediction time. Slip the answer in, and the model shines in training, then fails for real. So, feature engineering means creating better inputs from raw data. Combine columns, extract pieces, reshape values.

Expose the pattern, and the model can finally learn it. Quick check now. One question is coming up. Let's see if it clicked.