Welcome to the fourth chapter! In the previous chapter, 1-Introduction, we learned the history, definitions, and fairness concepts of Machine Learning. We set the stage for what we want to do.
Now, we need to figure out where to do it.
If you have done programming before, you might be used to writing a file called script.py and running it all at once. In Data Science, we do things differently. We use a special file format called notebook.ipynb.
Imagine you are a scientist in a chemistry lab.
In Machine Learning, training a model can take a long time. We don't want to run the whole script every time we change one line of code.
The Solution: The Jupyter Notebook (.ipynb).
This file acts like a Lab Journal. It allows you to:
The notebook is built around one simple concept: The Cell.
Instead of one long page of text, a notebook is a list of boxes. There are two main types of boxes:
Let's say you are loading a dataset of 1,000 dog photos.
In a standard script, you'd run both steps at once. In a notebook, you run Step 1, wait for it to finish, and then look at the data before moving to Step 2.
To use a notebook, you need a program like VS Code or Jupyter Lab.
You type your Python code into a cell.
# A simple Code Cell
name = "Robot Intern"
print(f"Hello, {name}!")
Output:
Hello, Robot Intern!
Explanation: When you press the "Play" button (or Shift+Enter), the computer runs only these two lines.
This is the magic part. Because we ran the cell above, the computer now remembers who name is. We can use it in a new cell without typing it again.
# A new cell, later in the notebook
# We don't need to define 'name' again!
print(f"{name} is ready to learn.")
Output:
Robot Intern is ready to learn.
Explanation: The notebook keeps a "State" (memory) alive as long as the kernel (the brain) is running. This allows us to work step-by-step.
How does a web page (the notebook) talk to Python?
When you run a cell, your browser sends the text to a background process called a Kernel. The Kernel executes it and sends the answer back.
The file extension is .ipynb, which stands for Interactive Python Notebook.
If you try to open this file in a regular text editor (like Notepad), you won't see a pretty interface. You will see raw text formatted as JSON (JavaScript Object Notation).
Here is what the actual file looks like inside:
{
"cells": [
{
"cell_type": "code",
"source": [
"print('Hello World')"
],
"outputs": [
{
"output_type": "stream",
"text": [
"Hello World\n"
]
}
]
}
]
}
Explanation:
"cells": A list of all the boxes in your notebook."source": The code you wrote (print...)."outputs": The result the computer gave back (Hello World).
Why does this matter?
Because the output is saved inside the file! If you send your .ipynb file to a friend, they can see your graphs and results without even running the code. It is a portable report.
Starting with notebook.ipynb removes the fear of breaking things.
In this chapter, we learned that notebook.ipynb is our interactive canvas. It allows us to mix Code, Text, and Results into one document.
Now that we have our digital lab journal ready, it helps to understand concepts visually before we code them. Sometimes, a drawing is worth a thousand lines of code.
Generated by Code IQ