About this course
<p>Machine learning can sound much more complicated than it actually is. You hear words like <em>models</em>, <em>training</em>, <em>features</em>, <em>datasets</em>, <em>predictions</em>, and <em>algorithms</em>, and it can feel like you need a PhD in mathematics before you're allowed to write your first machine learning program.</p>
<p>But at its core, machine learning is about getting a computer to learn patterns from examples and then use those patterns to make predictions about new examples. If you've ever learned to recognize a cat after seeing lots of cats, you already understand the basic idea.</p>
<p>In this tutorial, we're going to build a real machine learning model in Python. We'll start with a tiny dataset, train a model to predict whether a student might pass an exam based on the number of hours they studied, and then use the trained model to make predictions about new students.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>You don't need any previous machine learning experience to follow this tutorial. We'll introduce each machine learning concept as we go.</p>
<p>But having a basic understanding of Python will make the tutorial easier to follow. You should be comfortable with:</p>
<ul>
<li><p>Creating and using variables</p>
</li>
<li><p>Working with Python lists</p>
</li>
<li><p>Writing basic <code>if</code>/<code>else</code> statements</p>
</li>
<li><p>Calling functions</p>
</li>
<li><p>Reading and running a Python program</p>
</li>
<li><p>Using a terminal or command prompt to run commands</p>
</li>
</ul>
<p>You should also have:</p>
<ul>
<li><p><strong>Python</strong> installed on your computer</p>
</li>
<li><p>A text editor or code editor, such as VS Code</p>
</li>
<li><p>A terminal or command prompt</p>
</li>
<li><p>An internet connection to install the required Python library</p>
</li>
</ul>
<p>You <strong>do not</strong> need prior knowledge of machine learning, scikit-learn, statistics, or advanced mathematics. I'll explain the machine learning concepts and code step by step.</p>
<h2 id="heading-what-you-will-learn">What You Will Learn</h2>
<ul>
<li><p><a href="#heading-what-is-a-machine-learning-model">What Is a Machine Learning Model?</a></p>
</li>
<li><p><a href="#heading-machine-learning-vs-traditional-programming">Machine Learning vs Traditional Programming</a></p>
</li>
<li><p><a href="#heading-what-does-training-mean">What Does "Training" Mean?</a></p>
</li>
<li><p><a href="#heading-what-is-a-dataset">What Is a Dataset?</a></p>
</li>
<li><p><a href="#heading-what-are-features-and-labels">What Are Features and Labels?</a></p>
</li>
<li><p><a href="#heading-what-kind-of-machine-learning-are-we-using">What Kind of Machine Learning Are We Using?</a></p>
</li>
<li><p><a href="#heading-what-are-we-actually-going-to-build">What Are We Actually Going to Build?</a></p>
</li>
<li><p><a href="#heading-step-1-install-python">Step 1: Install Python</a></p>
</li>
<li><p><a href="#heading-step-2-create-a-project-folder">Step 2: Create a Project Folder</a></p>
</li>
<li><p><a href="#heading-step-3-install-scikit-learn">Step 3: Install scikit-learn</a></p>
</li>
<li><p><a href="#heading-step-4-import-the-model">Step 4: Import the Model</a></p>
</li>
<li><p><a href="#heading-step-5-create-our-dataset">Step 5: Create Our Dataset</a></p>
</li>
<li><p><a href="#heading-step-6-understand-why-the-data-structure-matters">Step 6: Understand Why the Data Structure Matters</a></p>
</li>
<li><p><a href="#heading-step-7-split-the-data">Step 7: Split the Data</a></p>
</li>
<li><p><a href="#heading-step-8-create-the-model">Step 8: Create the Model</a></p>
</li>
<li><p><a href="#heading-step-9-train-the-model">Step 9: Train the Model</a></p>
</li>
<li><p><a href="#heading-step-10-make-predictions">Step 10: Make Predictions</a></p>
</li>
<li><p><a href="#heading-step-11-convert-the-prediction-into-human-friendly-text">Step 11: Convert the Prediction Into Human-Friendly Text</a></p>
</li>
<li><p><a href="#heading-step-12-test-the-model">Step 12: Test the Model</a></p>
<ul>
<li><a href="#heading-a-very-important-warning-about-accuracy">A Very Important Warning About Accuracy</a></li>
</ul>
</li>
<li><p><a href="#heading-step-13-put-everything-together">Step 13: Put Everything Together</a></p>
<ul>
<li><p><a href="#heading-reading-the-complete-code-from-top-to-bottom">Reading the Complete Code From Top to Bottom</a></p>
</li>
<li><p><a href="#heading-what-is-actually-happening-inside-the-model">What Is Actually Happening Inside the Model?</a></p>
</li>
<li><p><a href="#heading-what-does-learning-actually-mean">What Does "Learning" Actually Mean?</a></p>
</li>
<li><p><a href="#heading-what-is-a-parameter">What Is a Parameter?</a></p>
<ul>
<li><a href="#heading-parameters-vs-hyperparameters">Parameters vs Hyperparameters</a></li>
</ul>
</li>
<li><p><a href="#heading-why-do-we-need-training-and-testing-data">Why Do We Need Training and Testing Data?</a></p>
<ul>
<li><p><a href="#heading-what-is-overfitting">What Is Overfitting?</a></p>
</li>
<li><p><a href="#heading-what-is-underfitting">What Is Underfitting?</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-why-our-dataset-is-not-a-real-machine-learning-dataset">Why Our Dataset Is Not a Real Machine Learning Dataset</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-step-14-add-more-features">Step 14: Add More Features</a></p>
</li>
<li><p><a href="#heading-step-15-make-a-prediction-with-multiple-features">Step 15: Make a Prediction With Multiple Features</a></p>
<ul>
<li><p><a href="#heading-what-happens-when-you-have-hundreds-of-features">What Happens When You Have Hundreds of Features?</a></p>
</li>
<li><p><a href="#heading-what-is-regression">What Is Regression?</a></p>
</li>
<li><p><a href="#heading-a-simple-regression-example">A Simple Regression Example</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-the-general-machine-learning-workflow">The General Machine Learning Workflow</a></p>
</li>
<li><p><a href="#heading-how-machine-learning-fits-into-real-applications">How Machine Learning Fits Into Real Applications</a></p>
</li>
<li><p><a href="#heading-what-should-you-learn-after-this">What Should You Learn After This?</a></p>
</li>
<li><p><a href="#heading-the-mental-model-to-keep">The Mental Model to Keep</a></p>
</li>
<li><p><a href="#heading-final-thoughts">Final Thoughts</a></p>
</li>
</ul>
<p>The goal isn't just to get the code working. We're going to understand what each important line does, why we need it, and what's actually happening behind the scenes.</p>
<p>By the end, you'll have a much clearer mental model of what machine learning actually is and how you can start building models yourself.</p>
<h2 id="heading-what-is-a-machine-learning-model">What Is a Machine Learning Model?</h2>
<p>A machine learning model is a program that has learned a pattern from data.</p>
<p>That definition is intentionally simple.</p>
<p>Suppose you show a child several animals and tell them which ones are cats. After seeing enough examples, the child might notice that cats usually have certain characteristics: whiskers, four legs, fur, a particular face shape, and so on. When they see a new animal, they can use what they learned to make a guess about whether it is a cat.</p>
<p>A machine learning model works in a similar way, except instead of looking at animals, it works with numbers and data.</p>
<p>For example, suppose we give a model information about students:</p>
<table>
<thead>
<tr>
<th>Hours Studied</th>
<th>Exam Result</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>Fail</td>
</tr>
<tr>
<td>2</td>
<td>Fail</td>
</tr>
<tr>
<td>3</td>
<td>Fail</td>
</tr>
<tr>
<td>4</td>
<td>Pass</td>
</tr>
<tr>
<td>5</td>
<td>Pass</td>
</tr>
<tr>
<td>6</td>
<td>Pass</td>
</tr>
</tbody></table>
<p>The model can look at these examples and discover a relationship between studying time and exam results. It might learn that students who study more tend to have a higher chance of passing.</p>
<p>We aren't explicitly writing that rule into the program. The model learns the relationship from the examples.</p>
<p>That's the key idea behind machine learning.</p>
<h2 id="heading-machine-learning-vs-traditional-programming">Machine Learning vs Traditional Programming</h2>
<p>This becomes much clearer when you compare machine learning with traditional programming.</p>
<p>In traditional programming, you give the computer rules and data, and it produces an answer.</p>
<p>For example:</p>
<pre><code class="language-text">Data + Rules → Answer
</code></pre>
<p>You might write:</p>
<pre><code class="language-python">hours = 5
if hours >= 4:
print("Likely to pass")
else:
print("Likely to fail")
</code></pre>
<p>Here, you explicitly created the rule:</p>
<pre><code class="language-python">hours >= 4
</code></pre>
<p>The computer isn't learning anything. You told it exactly what to do.</p>
<p>Machine learning flips this around. Instead of manually writing the rule, you give the computer examples:</p>
<pre><code class="language-text">Examples + Correct Answers → Machine Learning Model
</code></pre>
<p>The model figures out a useful pattern from those examples.</p>
<p>Then you can give the trained model new data:</p>
<pre><code class="language-text">New Data + Trained Model → Prediction
</code></pre>
<p>That difference is one of the most important concepts to understand.</p>
<h2 id="heading-what-does-training-mean">What Does "Training" Mean?</h2>
<p>Training is simply the process of teaching a machine learning model using examples.</p>
<p>Imagine that you're teaching someone to recognize whether a student is likely to pass an exam.</p>
<p>You give them examples:</p>
<pre><code class="language-text">1 hour → Fail
2 hours → Fail
3 hours → Fail
5 hours → Pass
6 hours → Pass
</code></pre>
<p>After looking at enough examples, they start noticing a pattern.</p>
<p>Machine learning training works similarly.</p>
<p>We give the algorithm data, and the algorithm adjusts the model so that its predictions become better at matching the examples it's been given.</p>
<p>The word <em>training</em> sounds fancy, but the basic idea is just to give the model examples and let it learn a useful pattern.</p>
<h2 id="heading-what-is-a-dataset">What Is a Dataset?</h2>
<p>A dataset is simply a collection of data.</p>
<p>For our project, we can represent our dataset using Python lists.</p>
<p>Suppose we have:</p>
<pre><code class="language-python">hours = [1, 2, 3, 4, 5, 6, 7, 8]
</code></pre>
<p>and:</p>
<pre><code class="language-python">results = [0, 0, 0, 1, 1, 1, 1, 1]
</code></pre>
<p>Here, we're using numbers to represent the exam results.</p>
<p>We'll use:</p>
<pre><code class="language-text">0 = Fail
1 = Pass
</code></pre>
<p>So our data means:</p>
<pre><code class="language-text">1 hour → Fail
2 hours → Fail
3 hours → Fail
4 hours → Pass
5 hours → Pass
6 hours → Pass
7 hours → Pass
8 hours → Pass
</code></pre>
<p>The first list contains our input information. The second list contains the answers we want the model to learn from.</p>
<h2 id="heading-what-are-features-and-labels">What Are Features and Labels?</h2>
<p>Machine learning uses a few words that sound more complicated than they really are.</p>
<p>A <strong>feature</strong> is information that we use to make a prediction.</p>
<p>A <strong>label</strong> is the answer we want the model to predict.</p>
<p>In our example:</p>
<pre><code class="language-text">Hours studied → Feature
Pass/fail → Label
</code></pre>
<p>If we had more information about each student, we could have multiple features, such as:</p>
<pre><code class="language-text">Hours studied
Previous exam score
Homework completion rate
Attendance
</code></pre>
<p>Then the model could use all of those features to predict:</p>
<pre><code class="language-text">Pass or fail
</code></pre>
<p>So you can think of it like this: Features are the clues. The label is the answer.</p>
<h2 id="heading-what-kind-of-machine-learning-are-we-using">What Kind of Machine Learning Are We Using?</h2>
<p>Our example uses <strong>supervised learning</strong>. Supervised learning means we train the model using examples where we already know the correct answer.</p>
<p>For example:</p>
<pre><code class="language-text">Hours studied: 2
Correct answer: Fail
</code></pre>
<p>and:</p>
<pre><code class="language-text">Hours studied: 6
Correct answer: Pass
</code></pre>
<p>The model sees both the input and the correct output during training.</p>
<p>This is different from <strong>unsupervised learning</strong>, where the model receives data without being given the correct answers and tries to find patterns or groups on its own.</p>
<p>There are other types of machine learning too, including reinforcement learning, but supervised learning is a great place to start because the basic workflow is easy to understand.</p>
<h2 id="heading-what-are-we-actually-going-to-build">What Are We Actually Going to Build?</h2>
<p>We're going to create a Python program that:</p>
<ol>
<li><p>Creates a small dataset.</p>
</li>
<li><p>Separates the inputs from the answers.</p>
</li>
<li><p>Splits the data into training and testing data.</p>
</li>
<li><p>Creates a machine learning model.</p>
</li>
<li><p>Trains the model.</p>
</li>
<li><p>Tests how well it performs.</p>
</li>
<li><p>Gives the model new information.</p>
</li>
<li><p>Uses the model to make a prediction.</p>
</li>
</ol>
<p>Our final program will use a <strong>decision tree classifier</strong> from the <code>scikit-learn</code> library.</p>
<p>A decision tree is a machine learning algorithm that makes decisions by asking a series of questions about the data.</p>
<p>For our simple example, the model might learn a pattern similar to:</p>
<pre><code class="language-text">Did the student study enough hours?
↓
Yes → Pass
No → Fail
</code></pre>
<p>Real decision trees can become much more complicated, but this gives you the basic idea.</p>
<p>Now let's get started building!</p>
<h2 id="heading-step-1-install-python">Step 1: Install Python</h2>
<p>To follow along here, you'll need Python installed on your computer.</p>
<p>You can check whether Python is already installed by running:</p>
<pre><code class="language-bash">python --version
</code></pre>
<p>You should see something similar to:</p>
<pre><code class="language-text">Python 3.12.0
</code></pre>
<p>The exact version doesn't have to match that example.</p>
<h2 id="heading-step-2-create-a-project-folder">Step 2: Create a Project Folder</h2>
<p>Create a folder called:</p>
<pre><code class="language-text">machine-learning-model
</code></pre>
<p>Inside that folder, create a file called:</p>
<pre><code class="language-text">model.py
</code></pre>
<p>Our project will eventually look like:</p>
<pre><code class="language-text">machine-learning-model/
└── model.py
</code></pre>
<h2 id="heading-step-3-install-scikit-learn">Step 3: Install scikit-learn</h2>
<p>We're going to use a Python library called <strong>scikit-learn</strong>.</p>
<p>scikit-learn provides many machine learning algorithms and tools, so we don't have to implement everything from mathematical equations ourselves.</p>
<p>Install it with:</p>
<pre><code class="language-bash">pip install scikit-learn
</code></pre>
<p>We could technically build a simple machine learning algorithm ourselves, and doing that can be useful for learning the mathematics later. For our first practical model, however, using a machine learning library lets us focus on understanding the workflow.</p>
<h2 id="heading-step-4-import-the-model">Step 4: Import the Model</h2>
<p>Open <code>model.py</code> and write:</p>
<pre><code class="language-python">from sklearn.tree import DecisionTreeClassifier
</code></pre>
<p>This line imports the <code>DecisionTreeClassifier</code> class from scikit-learn.</p>
<p>This structure:</p>
<pre><code class="language-python">from sklearn.tree
</code></pre>
<p>means we're getting something from scikit-learn's tree module.</p>
<p>Then:</p>
<pre><code class="language-python">import DecisionTreeClassifier
</code></pre>
<p>means we want to use the decision tree classifier.</p>
<p>After importing it, we can create a machine learning model with:</p>
<pre><code class="language-python">model = DecisionTreeClassifier()
</code></pre>
<p>The variable:</p>
<pre><code class="language-python">model
</code></pre>
<p>will represent our machine learning model.</p>
<p>At this point, the model hasn't learned anything. It's basically an empty model waiting for training data.</p>
<h2 id="heading-step-5-create-our-dataset">Step 5: Create Our Dataset</h2>
<p>Now let's create the examples our model will learn from.</p>
<p>Add:</p>
<pre><code class="language-python">hours = [1, 2, 3, 4, 5, 6, 7, 8]
</code></pre>
<p>This list represents how many hours each student studied.</p>
<p>Then:</p>
<pre><code class="language-python">results = [0, 0, 0, 1, 1, 1, 1, 1]
</code></pre>
<p>This list represents whether each student passed.</p>
<p>Remember:</p>
<pre><code class="language-text">0 = Fail
1 = Pass
</code></pre>
<p>So the first student studied for one hour and failed.</p>
<p>The fourth student studied for four hours and passed.</p>
<p>The eighth student studied for eight hours and passed.</p>
<p>We now have examples that the model can learn from.</p>
<h2 id="heading-step-6-understand-why-the-data-structure-matters">Step 6: Understand Why the Data Structure Matters</h2>
<p>There's an important detail here. Machine learning libraries usually expect the input data to be structured in a particular way.</p>
<p>Our <code>hours</code> list looks like this:</p>
<pre><code class="language-python">[1, 2, 3, 4, 5, 6, 7, 8]
</code></pre>
<p>But scikit-learn expects features to be represented as a two-dimensional structure.</p>
<p>Why?</p>
<p>Because a machine learning dataset can contain multiple features.</p>
<p>Imagine this dataset:</p>
<pre><code class="language-text">Hours Studied | Attendance | Previous Score
2 | 80% | 65
5 | 95% | 82
7 | 98% | 91
</code></pre>
<p>Each row represents one example.</p>
<p>Each column represents one feature.</p>
<p>So even though our current model only has one feature, we still need to represent it as a two-dimensional dataset.</p>
<p>We can do this using nested lists:</p>
<pre><code class="language-python">X = [
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8]
]
</code></pre>
<p>Each inner list represents one student.</p>
<p>The first student has:</p>
<pre><code class="language-python">[1]
</code></pre>
<p>meaning they studied one hour.</p>
<p>The second has:</p>
<pre><code class="language-python">[2]
</code></pre>
<p>and so on.</p>
<p>The uppercase <code>X</code> is a common convention for the feature data.</p>
<p>Now create the labels:</p>
<pre><code class="language-python">y = [0, 0, 0, 1, 1, 1, 1, 1]
</code></pre>
<p>The lowercase <code>y</code> is commonly used for the target or label values.</p>
<p>So we now have:</p>
<pre><code class="language-python">X = [
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8]
]
y = [0, 0, 0, 1, 1, 1, 1, 1]
</code></pre>
<p>You can think of <code>X</code> as:</p>
<blockquote>
<p>Here are the clues.</p>
</blockquote>
<p>And <code>y</code> as:</p>
<blockquote>
<p>Here are the correct answers.</p>
</blockquote>
<h2 id="heading-step-7-split-the-data">Step 7: Split the Data</h2>
<p>We don't want to train and test the model using exactly the same examples.</p>
<p>That would be a bit like giving a student the exact questions they'll see on an exam and then saying:</p>
<blockquote>
<p>“Wow, you got 100%. Great job.”</p>
</blockquote>
<p>We haven't really tested whether they learned anything.</p>
<p>Instead, we'll separate our dataset into:</p>
<ul>
<li><p>Training data</p>
</li>
<li><p>Testing data</p>
</li>
</ul>
<p>The training data teaches the model, while the testing data checks whether the model can make predictions on examples it wasn't trained on.</p>
<p>Import the splitting function:</p>
<pre><code class="language-python">from sklearn.model_selection import train_test_split
</code></pre>
<p>Now we can write:</p>
<pre><code class="language-python">X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.25,
random_state=42
)
</code></pre>
<p>There is a lot happening in this one line, so let's unpack it.</p>
<h4 id="heading-traintestsplit"><code>train_test_split()</code></h4>
<p>This function randomly divides our data into training and testing portions.</p>
<p>We pass it:</p>
<pre><code class="language-python">X
</code></pre>
<p>which contains our features.</p>
<p>Then:</p>
<pre><code class="language-python">y
</code></pre>
<p>which contains our labels.</p>
<p>The argument:</p>
<pre><code class="language-python">test_size=0.25
</code></pre>
<p>means we want approximately 25% of our data for testing.</p>
<p>The remaining 75% is used for training.</p>
<h4 id="heading-randomstate42"><code>random_state=42</code></h4>
<p>The data is randomly split.</p>
<p>If you run the program multiple times without controlling the randomness, you might get a different split each time.</p>
<p>Setting:</p>
<pre><code class="language-python">random_state=42
</code></pre>
<p>makes the random split reproducible.</p>
<p>The number <code>42</code> isn't magical. You could use another integer.</p>
<p>For example:</p>
<pre><code class="language-python">random_state=10
</code></pre>
<p>would also work.</p>
<p>We use <code>42</code> simply because it's a common example value.</p>
<h3 id="heading-the-four-variables">The Four Variables</h3>
<p>The function returns four pieces of data:</p>
<pre><code class="language-python">X_train
X_test
y_train
y_test
</code></pre>
<p><code>X_train</code> contains the features used to train the model.</p>
<p><code>y_train</code> contains the correct answers for those training examples.</p>
<p><code>X_test</code> contains the features used to test the model.</p>
<p><code>y_test</code> contains the correct answers so we can compare them with the model's predictions.</p>
<h2 id="heading-step-8-create-the-model">Step 8: Create the Model</h2>
<p>Now create our decision tree:</p>
<pre><code class="language-python">model = DecisionTreeClassifier()
</code></pre>
<p>This creates the model object.</p>
<p>Again, nothing has been learned yet. Think of it like buying a blank notebook: the notebook exists, but it doesn't contain your notes yet.</p>
<h2 id="heading-step-9-train-the-model">Step 9: Train the Model</h2>
<p>Now we get to the line that actually teaches the model:</p>
<pre><code class="language-python">model.fit(X_train, y_train)
</code></pre>
<p>This is one of the most important lines in machine learning.</p>
<p>The <code>.fit()</code> method trains the model using the data we provide.</p>
<p>We give it:</p>
<pre><code class="language-python">X_train
</code></pre>
<p>which contains the examples.</p>
<p>Then:</p>
<pre><code class="language-python">y_train
</code></pre>
<p>which contains the correct answers.</p>
<p>The model looks for patterns connecting the features to the labels.</p>
<p>In our case, it's trying to discover a relationship between:</p>
<pre><code class="language-text">Hours studied
</code></pre>
<p>and:</p>
<pre><code class="language-text">Pass/fail
</code></pre>
<p>The exact internal process depends on the algorithm. A decision tree learns decision rules that split the training data into groups that become increasingly useful for predicting the target.</p>
<p>The important thing to understand right now is:</p>
<pre><code class="language-python">model.fit(X_train, y_train)
</code></pre>
<p>means:</p>
<blockquote>
<p>Learn from these examples and their correct answers.</p>
</blockquote>
<h2 id="heading-step-10-make-predictions">Step 10: Make Predictions</h2>
<p>After training, we can give the model new data.</p>
<p>Suppose a student studied for five hours.</p>
<p>We can write:</p>
<pre><code class="language-python">prediction = model.predict([[5]])
</code></pre>
<p>Notice that we used:</p>
<pre><code class="language-python">[[5]]
</code></pre>
<p>instead of:</p>
<pre><code class="language-python">[5]
</code></pre>
<p>The outer list represents the collection of examples. The inner list represents the features for one example.</p>
<p>Since our model has one feature, that example contains one value:</p>
<pre><code class="language-python">[5]
</code></pre>
<p>So:</p>
<pre><code class="language-python">[[5]]
</code></pre>
<p>means:</p>
<blockquote>
<p>Predict the result for one student whose feature value is five hours.</p>
</blockquote>
<p>The model returns a prediction.</p>
<p>We can print it:</p>
<pre><code class="language-python">print(prediction)
</code></pre>
<p>You might see:</p>
<pre><code class="language-text">[1]
</code></pre>
<p>Remember:</p>
<pre><code class="language-text">1 = Pass
0 = Fail
</code></pre>
<p>So the model predicted that the student would pass.</p>
<h2 id="heading-step-11-convert-the-prediction-into-human-friendly-text">Step 11: Convert the Prediction Into Human-Friendly Text</h2>
<p>A prediction of:</p>
<pre><code class="language-text">1
</code></pre>
<p>isn't particularly friendly.</p>
<p>We can write:</p>
<pre><code class="language-python">if prediction[0] == 1:
print("The model predicts: Pass")
else:
print("The model predicts: Fail")
</code></pre>
<p>Let's look at:</p>
<pre><code class="language-python">prediction[0]
</code></pre>
<p>The model returns a list containing the prediction:</p>
<pre><code class="language-python">[1]
</code></pre>
<p>The <code>[0]</code> gets the first item.</p>
<p>Python starts counting list positions at zero.</p>
<p>So:</p>
<pre><code class="language-python">prediction[0]
</code></pre>
<p>means:</p>
<blockquote>
<p>Give me the first prediction.</p>
</blockquote>
<p>Then:</p>
<pre><code class="language-python">if prediction[0] == 1:
</code></pre>
<p>checks whether the model predicted <code>1</code>.</p>
<p>If it did, we print:</p>
<pre><code class="language-text">The model predicts: Pass
</code></pre>
<p>Otherwise, we print:</p>
<pre><code class="language-text">The model predicts: Fail
</code></pre>
<h2 id="heading-step-12-test-the-model">Step 12: Test the Model</h2>
<p>We shouldn't just make one prediction and assume the model is good.</p>
<p>We need to evaluate it.</p>
<p>First, make predictions for the test dataset:</p>
<pre><code class="language-python">predictions = model.predict(X_test)
</code></pre>
<p>Now:</p>
<pre><code class="language-python">predictions
</code></pre>
<p>contains the model's predictions for the examples it didn't see during training.</p>
<p>We can compare these predictions with:</p>
<pre><code class="language-python">y_test
</code></pre>
<p>which contains the actual answers.</p>
<p>scikit-learn provides an accuracy function:</p>
<pre><code class="language-python">from sklearn.metrics import accuracy_score
</code></pre>
<p>Then:</p>
<pre><code class="language-python">accuracy = accuracy_score(y_test, predictions)
</code></pre>
<p>The function compares the correct answers with the model's predictions.</p>
<p>If the model gets:</p>
<pre><code class="language-text">8 out of 10
</code></pre>
<p>correct, the accuracy would be:</p>
<pre><code class="language-text">0.8
</code></pre>
<p>We can turn that into a percentage:</p>
<pre><code class="language-python">print(f"Model accuracy: {accuracy * 100:.2f}%")
</code></pre>
<p>The <code>* 100</code> converts:</p>
<pre><code class="language-text">0.8
</code></pre>
<p>into:</p>
<pre><code class="language-text">80
</code></pre>
<p>The:</p>
<pre><code class="language-python">:.2f
</code></pre>
<p>means we want two decimal places.</p>
<p>So the output could look like:</p>
<pre><code class="language-text">Model accuracy: 80.00%
</code></pre>
<h3 id="heading-a-very-important-warning-about-accuracy">A Very Important Warning About Accuracy</h3>
<p>Accuracy is useful, but it doesn't tell you everything about a model.</p>
<p>Imagine you're trying to detect a rare disease.</p>
<p>Suppose:</p>
<pre><code class="language-text">99 people are healthy
1 person is sick
</code></pre>
<p>A terrible model could simply predict:</p>
<pre><code class="language-text">Everyone is healthy.
</code></pre>
<p>It would be 99% accurate.</p>
<p>But it completely failed at the thing we actually care about: identifying the sick person.</p>
<p>This is why machine learning developers use other evaluation metrics depending on the problem, including precision, recall, F1 score, mean squared error, and others.</p>
<p>For our beginner example, accuracy is enough to understand the basic workflow.</p>
<h2 id="heading-step-13-put-everything-together">Step 13: Put Everything Together</h2>
<p>Our complete beginner machine learning program looks like this:</p>
<pre><code class="language-python">from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Dataset
X = [
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8]
]
y = [
0,
0,
0,
1,
1,
1,
1,
1
]
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.25,
random_state=42
)
# Create the machine learning model
model = DecisionTreeClassifier()
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test data
predictions = model.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)
print(f"Model accuracy: {accuracy * 100:.2f}%")
# Make a prediction for a new student
hours_studied = [[5]]
prediction = model.predict(hours_studied)
# Display the prediction
if prediction[0] == 1:
print("The model predicts: Pass")
else:
print("The model predicts: Fail")
</code></pre>
<h3 id="heading-reading-the-complete-code-from-top-to-bottom">Reading the Complete Code From Top to Bottom</h3>
<p>The first three lines:</p>
<pre><code class="language-python">from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
</code></pre>
<p>import the tools we need.</p>
<p>Then:</p>
<pre><code class="language-python">X = [
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8]
]
</code></pre>
<p>creates the feature data.</p>
<p>Then:</p>
<pre><code class="language-python">y = [
0,
0,
0,
1,
1,
1,
1,
1
]
</code></pre>
<p>creates the labels.</p>
<p>Next:</p>
<pre><code class="language-python">X_train, X_test, y_train, y_test = train_test_split(...)
</code></pre>
<p>divides the dataset into training and testing data.</p>
<p>Then:</p>
<pre><code class="language-python">model = DecisionTreeClassifier()
</code></pre>
<p>creates the model.</p>
<p>Next:</p>
<pre><code class="language-python">model.fit(X_train, y_train)
</code></pre>
<p>trains it.</p>
<p>Then:</p>
<pre><code class="language-python">predictions = model.predict(X_test)
</code></pre>
<p>asks the trained model to make predictions about the testing examples.</p>
<p>Next:</p>
<pre><code class="language-python">accuracy = accuracy_score(y_test, predictions)
</code></pre>
<p>measures how many of those predictions were correct.</p>
<p>Finally:</p>
<pre><code class="language-python">prediction = model.predict([[5]])
</code></pre>
<p>asks the model to predict the result for a new student who studied for five hours.</p>
<p>That's the entire machine learning workflow.</p>
<h3 id="heading-what-is-actually-happening-inside-the-model">What Is Actually Happening Inside the Model?</h3>
<p>This is where machine learning gets more interesting.</p>
<p>When we run:</p>
<pre><code class="language-python">model.fit(X_train, y_train)
</code></pre>
<p>the decision tree doesn't simply memorize the phrase:</p>
<pre><code class="language-text">4 hours = Pass
</code></pre>
<p>It analyzes the training examples and looks for useful ways to split them.</p>
<p>For example, it might discover a rule similar to:</p>
<pre><code class="language-text">Is hours studied <= 3.5?
</code></pre>
<p>If yes:</p>
<pre><code class="language-text">Predict Fail
</code></pre>
<p>If no:</p>
<pre><code class="language-text">Predict Pass
</code></pre>
<p>The exact tree depends on the training data and algorithm settings.</p>
<p>If we added more features, the tree could make decisions using several pieces of information.</p>
<p>For example:</p>
<pre><code class="language-text">Is study time <= 3.5?
Yes
↓
Predict Fail
No
↓
Is attendance <= 80%?
Yes
↓
Predict Fail
No
↓
Predict Pass
</code></pre>
<p>Again, our actual code doesn't manually create these rules.</p>
<p>The algorithm learns them from the training data.</p>
<h3 id="heading-what-does-learning-actually-mean">What Does "Learning" Actually Mean?</h3>
<p>This is one of the most misunderstood parts of machine learning.</p>
<p>The computer isn't learning in exactly the same way a human does. A machine learning algorithm uses mathematical procedures to adjust a model based on data.</p>
<p>Different algorithms learn in different ways. A decision tree searches for useful splits. A linear regression model learns numerical parameters that describe a relationship. A neural network adjusts many parameters using optimization algorithms. And da clustering algorithm groups similar examples together.</p>
<p>So "learning" is a convenient word for:</p>
<blockquote>
<p>Using an algorithm to adjust a model so that it captures useful patterns in data.</p>
</blockquote>
<h3 id="heading-what-is-a-parameter">What Is a Parameter?</h3>
<p>A parameter is a value inside a machine learning model that is learned from data.</p>
<p>For example, in a simple linear model:</p>
<pre><code class="language-text">y = mx + b
</code></pre>
<p>the model might learn values for:</p>
<pre><code class="language-text">m
b
</code></pre>
<p>Those values determine the relationship between the input and output.</p>
<p>Neural networks can have millions or billions of learned parameters.</p>
<p>The important idea is that the model's behavior is controlled by values that are learned or adjusted during training.</p>
<h4 id="heading-parameters-vs-hyperparameters">Parameters vs Hyperparameters</h4>
<p>These two terms are easy to confuse.</p>
<p>A <strong>parameter</strong> is generally learned from the training data, while a <strong>hyperparameter</strong> is something you configure before or during training.</p>
<p>For our decision tree, we could specify:</p>
<pre><code class="language-python">model = DecisionTreeClassifier(
max_depth=3
)
</code></pre>
<p>Here:</p>
<pre><code class="language-python">max_depth=3
</code></pre>
<p>is a hyperparameter.</p>
<p>We're telling the algorithm:</p>
<blockquote>
<p>Don't allow the decision tree to grow beyond a depth of three.</p>
</blockquote>
<p>The model learns its internal decision rules from the data, while we choose the hyperparameter.</p>
<p>This distinction becomes increasingly important as you build more advanced models.</p>
<h3 id="heading-why-do-we-need-training-and-testing-data">Why Do We Need Training and Testing Data?</h3>
<p>Imagine you're studying for a math exam.</p>
<p>Your teacher gives you ten practice questions, and you memorize all ten answers.</p>
<p>Then the exam contains those exact ten questions, so you get everything correct.</p>
<p>Does that prove you understand mathematics? Not really. You might simply have memorized the examples.</p>
<p>Machine learning has a similar problem called <strong>overfitting</strong>. A model can become extremely good at the training data without becoming good at handling new data.</p>
<p>That's why we keep some examples separate. The model doesn't see the test examples during training. Then we can ask:</p>
<blockquote>
<p>Can the model generalize what it learned to examples it hasn't seen before?</p>
</blockquote>
<p>That ability to work on new data is one of the most important goals of machine learning.</p>
<h4 id="heading-what-is-overfitting">What Is Overfitting?</h4>
<p>Overfitting happens when a model learns the training data too specifically.</p>
<p>Imagine we give the model a very small dataset. Instead of learning the general pattern:</p>
<pre><code class="language-text">More studying tends to increase the chance of passing.
</code></pre>
<p>it might effectively memorize the specific examples.</p>
<p>That can make training performance look excellent while performance on new data is poor.</p>
<p>A model that performs well on training data but poorly on unseen data is often overfitting.</p>
<h4 id="heading-what-is-underfitting">What Is Underfitting?</h4>
<p>Underfitting is basically the opposite. The model is too simple to capture the important patterns in the data.</p>
<p>Imagine trying to predict someone's exam result using only one or two results.</p>
<p>That doesn't give the model enough useful information, and it might perform poorly on both training and testing data.</p>
<p>Good machine learning involves finding a model that's complex enough to learn useful patterns but not so complex that it simply memorizes the training examples.</p>
<h3 id="heading-why-our-dataset-is-not-a-real-machine-learning-dataset">Why Our Dataset Is Not a Real Machine Learning Dataset</h3>
<p>Our eight examples are intentionally tiny.</p>
<p>A real machine learning project would usually use much more data.</p>
<p>For example, you might collect:</p>
<pre><code class="language-text">10,000 students
</code></pre>
<p>with features such as:</p>
<pre><code class="language-text">Hours studied
Attendance
Homework completion
Previous scores
Sleep duration
</code></pre>
<p>and a label such as:</p>
<pre><code class="language-text">Passed
</code></pre>
<p>Then the model could learn from thousands of examples.</p>
<p>Our tiny dataset is useful because we can understand every part of the process.</p>
<h2 id="heading-step-14-add-more-features">Step 14: Add More Features</h2>
<p>Let's make our example slightly more realistic.</p>
<p>Instead of only using hours studied, suppose we have:</p>
<pre><code class="language-text">Hours studied
Attendance
</code></pre>
<p>We can represent each student like this:</p>
<pre><code class="language-python">X = [
[2, 70],
[3, 75],
[4, 80],
[5, 85],
[6, 90],
[7, 95]
]
</code></pre>
<p>Now each row contains two features.</p>
<p>For example:</p>
<pre><code class="language-python">[5, 85]
</code></pre>
<p>means:</p>
<pre><code class="language-text">5 hours studied
85% attendance
</code></pre>
<p>Our labels could still be:</p>
<pre><code class="language-python">y = [0, 0, 1, 1, 1, 1]
</code></pre>
<p>Now the model has more information to work with.</p>
<p>We could train it exactly the same way:</p>
<pre><code class="language-python">model.fit(X_train, y_train)
</code></pre>
<p>The difference is that the model now has two features instead of one.</p>
<h2 id="heading-step-15-make-a-prediction-with-multiple-features">Step 15: Make a Prediction With Multiple Features</h2>
<p>Suppose we want to predict the result of a student who:</p>
<pre><code class="language-text">Studied for 5 hours
Had 90% attendance
</code></pre>
<p>We represent that as:</p>
<pre><code class="language-python">new_student = [[5, 90]]
</code></pre>
<p>Then:</p>
<pre><code class="language-python">prediction = model.predict(new_student)
</code></pre>
<p>The model uses both features to make the prediction.</p>
<p>This is how machine learning scales from simple examples to datasets with many columns.</p>
<h3 id="heading-what-happens-when-you-have-hundreds-of-features">What Happens When You Have Hundreds of Features?</h3>
<p>The exact same basic concept applies.</p>
<p>Imagine predicting house prices using:</p>
<pre><code class="language-text">Number of bedrooms
Square footage
Number of bathrooms
Location
Age of house
Garage size
Lot size
Distance to school
</code></pre>
<p>Each one can become a feature. Then the model uses those features to predict a target:</p>
<pre><code class="language-text">House price
</code></pre>
<p>The basic structure remains:</p>
<pre><code class="language-text">Features → Model → Prediction
</code></pre>
<p>The difficult part becomes choosing useful data, selecting an appropriate algorithm, cleaning the data, evaluating the model, and making sure the model works well outside the training dataset.</p>
<h3 id="heading-what-is-regression">What Is Regression?</h3>
<p>So far, our model predicts categories:</p>
<pre><code class="language-text">Pass
Fail
</code></pre>
<p>This is a <strong>classification</strong> problem. Classification means predicting a category.</p>
<p>Examples include:</p>
<pre><code class="language-text">Spam / Not Spam
Cat / Dog
Fraud / Not Fraud
Pass / Fail
</code></pre>
<p>Regression is different. It predicts a numerical value.</p>
<p>For example:</p>
<pre><code class="language-text">House price = $425,000
</code></pre>
<p>or:</p>
<pre><code class="language-text">Temperature = 82.4°F
</code></pre>
<p>or:</p>
<pre><code class="language-text">Sales = $17,500
</code></pre>
<p>So a useful distinction is:</p>
<pre><code class="language-text">Classification → Predict a category
Regression → Predict a number
</code></pre>
<h3 id="heading-a-simple-regression-example">A Simple Regression Example</h3>
<p>scikit-learn provides a model called <code>LinearRegression</code>.</p>
<p>Import it:</p>
<pre><code class="language-python">from sklearn.linear_model import LinearRegression
</code></pre>
<p>Create the model:</p>
<pre><code class="language-python">model = LinearRegression()
</code></pre>
<p>Then train it:</p>
<pre><code class="language-python">model.fit(X_train, y_train)
</code></pre>
<p>And make a prediction:</p>
<pre><code class="language-python">prediction = model.predict([[5]])
</code></pre>
<p>The workflow is almost identical.</p>
<p>That's one reason machine learning libraries are useful: once you understand the general workflow, learning new algorithms becomes much easier.</p>
<h2 id="heading-the-general-machine-learning-workflow">The General Machine Learning Workflow</h2>
<p>Most beginner machine learning projects can be thought about using this sequence:</p>
<h3 id="heading-1-collect-data">1. Collect Data</h3>
<p>Get examples related to the problem you want to solve.</p>
<h3 id="heading-2-clean-the-data">2. Clean the Data</h3>
<p>Fix missing, incorrect, duplicated, or inconsistent information.</p>
<h3 id="heading-3-select-features">3. Select Features</h3>
<p>Choose the information you want the model to use.</p>
<h3 id="heading-4-choose-a-model">4. Choose a Model</h3>
<p>Select an algorithm appropriate for the problem.</p>
<h3 id="heading-5-split-the-data">5. Split the Data</h3>
<p>Separate training and testing examples.</p>
<h3 id="heading-6-train">6. Train</h3>
<p>Use the training data to fit the model.</p>
<h3 id="heading-7-evaluate">7. Evaluate</h3>
<p>Measure how well the model performs.</p>
<h3 id="heading-8-improve">8. Improve</h3>
<p>Change the data, features, model, or hyperparameters.</p>
<h3 id="heading-9-make-predictions">9. Make Predictions</h3>
<p>Use the trained model on new data.</p>
<h3 id="heading-10-deploy">10. Deploy</h3>
<p>If the model is useful, integrate it into an application.</p>
<p>This workflow is much more important than memorizing the name of a particular algorithm.</p>
<h2 id="heading-how-machine-learning-fits-into-real-applications">How Machine Learning Fits Into Real Applications</h2>
<p>A trained model is usually not the entire application.</p>
<p>Imagine you build a model that predicts whether an email is spam. You might eventually create:</p>
<pre><code class="language-text">Email
↓
Backend
↓
Machine Learning Model
↓
Prediction
↓
User Interface
</code></pre>
<p>The model is one component inside a larger software system.</p>
<p>The same idea applies to:</p>
<pre><code class="language-text">Recommendation systems
Fraud detection
Search engines
AI assistants
Image classification
Demand forecasting
Customer analytics
</code></pre>
<p>This is important for developers because machine learning engineering isn't only about training models. You also need to know how to build software around those models.</p>
<h2 id="heading-what-should-you-learn-after-this">What Should You Learn After This?</h2>
<p>Once you understand this basic project, there are several useful directions to explore.</p>
<h3 id="heading-learn-numpy">Learn NumPy</h3>
<p><a href="https://www.freecodecamp.org/news/numpy-crash-course-build-powerful-n-d-arrays-with-numpy/">NumPy is one of the fundamental Python libraries</a> for numerical computing. You'll encounter arrays everywhere in machine learning.</p>
<h3 id="heading-learn-pandas">Learn pandas</h3>
<p><a href="https://www.freecodecamp.org/news/learn-pandas-for-data-science/">pandas is extremely useful</a> for working with datasets.</p>
<p>For example:</p>
<pre><code class="language-python">import pandas as pd
</code></pre>
<p>You can load a CSV file:</p>
<pre><code class="language-python">data = pd.read_csv("students.csv")
</code></pre>
<p>and inspect it:</p>
<pre><code class="language-python">print(data.head())
</code></pre>
<p>This becomes much more useful once you start working with real datasets.</p>
<h3 id="heading-learn-data-visualization">Learn Data Visualization</h3>
<p>Libraries such as <a href="https://www.freecodecamp.org/news/getting-started-with-matplotlib/">Matplotlib</a> can help you visualize your data. For example, you might want to see whether exam scores increase as study hours increase.</p>
<p><a href="https://www.freecodecamp.org/news/learn-interactive-data-visualization-with-svelte-and-d3/">Visualizing data</a> can help you understand patterns before you even train a model.</p>
<h3 id="heading-learn-more-algorithms">Learn More Algorithms</h3>
<p>Once decision trees make sense, explore:</p>
<pre><code class="language-text">Linear Regression
Logistic Regression
Random Forests
K-Nearest Neighbors
Support Vector Machines
Gradient Boosting
Neural Networks
</code></pre>
<p>You don't need to memorize all of them.</p>
<p>Focus on understanding what kind of problem each algorithm is designed to solve and what assumptions or tradeoffs come with it.</p>
<h3 id="heading-learn-the-mathematics">Learn the Mathematics</h3>
<p>You can build useful machine learning applications without deriving every equation from scratch.</p>
<p>But if you want to understand machine learning deeply, <a href="https://www.freecodecamp.org/news/linear-algebra-crash-course-mathematics-for-machine-learning-and-generative-ai/">mathematics becomes increasingly valuable</a>.</p>
<p>Start with:</p>
<pre><code class="language-text">Algebra
Functions
Probability
Statistics
Linear Algebra
Calculus
</code></pre>
<p>Concepts such as derivatives and gradients become especially important when you start learning how neural networks train.</p>
<p>Here's a <a href="https://www.freecodecamp.org/news/learn-college-calculus-and-implement-with-python/">calculus course</a> and a <a href="https://www.freecodecamp.org/news/statistics-for-data-scientce-machine-learning-and-ai-handbook/">statistics handbook</a> as well to get you started.</p>
<h2 id="heading-the-mental-model-to-keep">The Mental Model to Keep</h2>
<p>When you're learning machine learning, don't let the terminology make everything feel more complicated than it is.</p>
<p>At the simplest level, think about machine learning like this:</p>
<p>You have examples, and each example contains information called <strong>features</strong>. Some examples also have known answers called <strong>labels</strong>.</p>
<p>You give those examples to a learning algorithm. The algorithm creates a model that captures patterns in the examples.</p>
<p>Then you give the trained model new information. The model uses the patterns it learned to make a prediction.</p>
<p>In code, the basic workflow looks like:</p>
<pre><code class="language-python">model = SomeMachineLearningModel()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
</code></pre>
<p>That three-part structure is worth remembering.</p>
<pre><code class="language-python">model = ...
</code></pre>
<p>creates the model.</p>
<pre><code class="language-python">model.fit(...)
</code></pre>
<p>trains the model.</p>
<pre><code class="language-python">model.predict(...)
</code></pre>
<p>uses the trained model.</p>
<p>Everything else you learn about machine learning builds on this foundation.</p>
<h2 id="heading-final-thoughts">Final Thoughts</h2>
<p>A machine learning model isn't a magical brain sitting inside your computer. It's a mathematical model created by an algorithm that has learned patterns from data.</p>
<p>The most important shift in thinking is understanding that you don't always need to program every rule yourself.</p>
<p>With traditional programming, you might explicitly write:</p>
<pre><code class="language-python">if hours >= 4:
result = "Pass"
</code></pre>
<p>With machine learning, you provide examples:</p>
<pre><code class="language-text">1 hour → Fail
2 hours → Fail
3 hours → Fail
4 hours → Pass
5 hours → Pass
</code></pre>
<p>and let the learning algorithm find a useful pattern.</p>
<p>Our project was intentionally small, but the same basic ideas appear in much larger systems. A recommendation engine, fraud detector, image classifier, and many other machine learning applications still have to deal with data, features, training, evaluation, and predictions.</p>
<p>Once you understand those fundamentals, terms like <em>training</em>, <em>features</em>, <em>labels</em>, <em>classification</em>, <em>regression</em>, <em>overfitting</em>, and <em>models</em> stop sounding like a collection of random AI vocabulary and start fitting into one connected idea.</p>
<p>You don't need to start by building the next giant AI system. Start with a tiny dataset, train one model, inspect its predictions, change something, and see what happens. That hands-on process is where machine learning starts becoming much easier to understand.</p>
<p>Happy coding!</p>