跳到正文
DEV Community· Lavkush Kumar·· 1 小時前AI 評分37

甚麼是機器學習?初學者簡明指南

What Is Machine Learning? Explained Simply for Beginners

AI 導讀

機器學習透過例子教電腦辨識模式,而非由程式員逐條編寫規則,並以數據、模型和訓練作為基本要素。文章説明監督式、非監督式和強化學習,並用 Python 最近鄰分類器示範如何預測新例子的標籤。模型受訓練數據限制,亦可能過度擬合或學到偏見。

正文

Originally published on CodingCush.

Machine learning is a way of teaching computers to find patterns in examples, instead of giving them step by step rules for everything. You show the computer thousands of labelled examples, it learns the pattern, and then it can guess the answer for new examples it has never seen. In this guide you will learn how that works, the three main types, and run a tiny working example.

The Simple Idea: Learn from Examples, Not Rules

Imagine teaching a child to spot dogs. You would not hand them a rulebook about ear shapes and nose sizes. You would point at dogs and say "dog", point at cats and say "cat", and after enough examples the child gets it. Machine learning works the same way.

A traditional program is a list of rules written by a programmer: if the email contains the word "lottery", mark it spam. A machine learning program is different: you feed it thousands of emails already marked spam or not spam, it finds the patterns itself, and then it judges new emails on its own. The rules are learned, not written. That is the whole trick, and everything else in this guide is detail around it. ChatGPT, the tool most people meet first, is built on exactly this idea, and our guide to what ChatGPT is, explained simply shows what it does with it.

The Three Ingredients: Data, a Model and Training

Every machine learning project has the same three parts. Data is the examples, with the right answer attached, like photos labelled cat or dog. The model is the program that learns, a flexible set of numbers that starts out random and useless. Training is the process of showing the model the data again and again, nudging those numbers a little each time it guesses wrong, until its guesses get good.

Think of the model as a student and training as homework: answer practice questions, get marked, adjust. After enough rounds, the student faces the real test, new data it has never seen. If it only memorised the practice questions, it fails. That failure has a name, overfitting, and it is the most common beginner mistake in the field.

The Three Main Types, in Plain Words

Machine learning splits into three types, and the difference is what kind of examples you give it:

Type What you give it What it learns to do Everyday example
Supervised learning Examples with the right answers Predict the answer for new examples Spam filter, house price guess
Unsupervised learning Examples with no answers Find groups and patterns on its own Grouping customers by shopping habits
Reinforcement learning Rewards for good moves, penalties for bad ones Choose the best action by trial and error Game playing, robot walking

Supervised learning is the workhorse and the easiest to start with: teach with answers, then predict. Unsupervised learning is for exploring data when you do not know what you are looking for. Reinforcement learning is how computers learned to beat humans at chess and Go, by playing millions of games against themselves. Google's free Machine Learning Crash Course teaches supervised learning first, with videos and hands on exercises, and it is a good next step after this guide.

Screenshot of Google's Machine Learning Crash Course landing page on developers.google.com, a free practical introduction to machine learning

Google's Machine Learning Crash Course landing page on developers.google.com, a free practical introduction to machine learning.

Try It Yourself: A Tiny Machine That Learns

You do not need a library or a GPU to see the idea working. This small Python program is a nearest neighbour classifier: it remembers a few labelled examples, and when it sees a new house, it copies the label of the closest example it has seen. That is real learning, just the simplest kind:

# Training data: [size in sq ft, rooms] -> label
train = [
    ([120, 2], "small flat"),
    ([90, 1], "small flat"),
    ([250, 4], "big house"),
    ([300, 5], "big house"),
]

def predict(size, rooms):
    best, best_d = None, float("inf")
    for (s, r), label in train:
        d = ((size - s) ** 2 + (rooms - r) ** 2) ** 0.5
        if d < best_d:
            best, best_d = label, d
    return best

print(predict(110, 2))
print(predict(280, 4))
print(predict(200, 3))

Output, run on Python 3.12.3:

small flat
big house
big house

Here is what happened. A 110 sq ft flat with 2 rooms is closest to the 120 sq ft example, so it gets "small flat". A 280 sq ft home with 4 rooms is closest to the 250 sq ft example, so it gets "big house". The 200 sq ft case sits between the groups, and the maths picks "big house" because it is slightly nearer. Real models use thousands of examples and smarter maths, but the shape is identical. If this kind of hands on learning appeals to you, our guide to debugging code with AI prompts shows the other side of the same coin: using a trained model to fix your own code.

Where You Already Meet Machine Learning Every Day

You do not have to go looking for machine learning. Your phone unlocks with your face because a model learned what your face looks like. Your keyboard suggests the next word because a model learned how you type. Your music app builds a playlist because a model learned what you skip.

The pattern is always the same: lots of examples, a model that learns from them, and a prediction you use. Spam filters, photo search, voice assistants and fraud detection are the same recipe with different data. The other well known assistant, Claude, works the same way under the hood, and our guide to what Claude AI is, explained simply covers what makes it different from the others.

What Machine Learning Cannot Do (Yet)

Machine learning is powerful, but it has honest limits, and knowing them keeps you from being fooled by hype. A model only knows what its training data taught it. Show it a kind of photo it never saw, and it will guess confidently and wrongly. Feed it biased data, and it learns the bias: a hiring model trained on past hiring decisions can quietly learn to prefer the same people the company always preferred.

Models also cannot explain themselves well: a model can refuse a loan without saying why in plain English. And they need a lot of data: four examples taught our tiny classifier almost nothing, and real models need thousands or millions. If you want to build real models instead of just understanding them, TensorFlow Learn is the free starting point from Google, with tutorials that run in your browser.

Common Mistakes and Troubleshooting

Training on too little data. Four examples make a toy, not a tool. Small data gives confident nonsense, so gather more examples before trusting any result.

Testing on the same data you trained on. A model that memorised its homework looks perfect on the test and fails in real life. Always keep some examples aside, unseen, for the final check.

Trusting accuracy alone. A model that says "not spam" for every email is 95 percent accurate if only 5 percent of mail is spam, and completely useless. Look at what it gets wrong, not just the headline number.

Forgetting the data can be biased. If the examples carry a bias, the model will carry it too. Check who is missing from your data before you ship anything.

Expecting the model to understand. A model finds patterns in numbers, it does not understand meaning. It can write a convincing sentence about a topic it knows nothing about, which is exactly why its answers need checking.

Frequently Asked Questions

Is machine learning the same as AI?

No. AI is the big umbrella: any machine doing something smart. Machine learning is one way to get there, by learning from examples. Not all AI uses machine learning, but most of the AI you meet daily does.

Do I need advanced maths to learn machine learning?

Not to start: the ideas need basic algebra and a willingness to experiment. The deeper maths matters later, when you build models from scratch instead of using libraries.

How much data do I need?

More than you think. A simple classifier can start with hundreds of examples. A photo recogniser needs thousands. A chatbot needs millions.

Can I learn machine learning with just Python?

Yes. Python is the standard language for it, and libraries like scikit-learn let you train your first real model in a few lines. Start here, then try one guided tutorial.

Will machine learning replace programmers?

No. It changes what programmers do: less writing rigid rules, more gathering good data, checking models and fixing their mistakes. Somebody still has to decide what the model should learn.

Conclusion

Machine learning is not magic: show a computer enough labelled examples, let it find the pattern, and it can guess the answer for new ones. Supervised learning predicts, unsupervised learning finds groups, reinforcement learning learns by trial and error. Run the tiny classifier above, change the numbers, and watch it change its mind.

來源:DEV Community · dev.to