Agentic AI DevOps Bootcamp · Session 5 · Summary Notes
Today’s topics
- Virtual environments: why every project needs one
requirements.txtand pinned versions- Reading and understanding data with pandas
- Features (x) and target (y)
- Training with LinearRegression, and what the model learns
- Predicting, and checking the result by hand
- Saving the trained model as a
.pklartifact - Common questions
The goal
Train our first model: predict salary from years of experience, then save it. The path is eight steps:
- Create a virtual environment
- Install the libraries
- Read the data
- Analyse the data
- Split into features (
x) and target (y) - Train the model
- Predict
- Save the model
1. Virtual environment: one per project
The problem: if you install a library normally, it goes to the system level, one common place for every project. Tomorrow’s project may need a different version of the same library, and the versions conflict.
The fix: a virtual environment is a private copy of Python and its libraries for one project folder. Whatever you install there stays there.
python3 -m venv .venv # create it (a .venv folder appears)
source .venv/bin/activate # activate it (Windows: .venv\Scripts\activate)
python3 -m pip install notebook # installs inside this environment only
python3 -m notebook # open the notebook
When it is active, the terminal prompt starts with (.venv). For a new project, create and activate a new one.
2. requirements.txt and pinned versions
Keep all your libraries in one file, requirements.txt, in the project folder:
pandas
scikit-learn
Install everything in it with one command (-r means “read from this file”):
python3 -m pip install -r requirements.txt
- Anyone who gets your project knows exactly what to install.
- Pin versions (for example
pandas==3.0.6). Without a pin, each install takes the latest release, and a newer version can change behaviour. - pip installs dependencies for you: pandas brings NumPy automatically.
- After editing the file, stop the notebook (
Ctrl + C), install, and start the notebook again.
3. Read and understand the data (pandas)
Before training, always look at the data. Each line below is one notebook cell:
import pandas as pd # "pd" is the standard alias
data = pd.read_csv("employee_salary_data.csv") # the file is now in the variable "data"
type(data) # DataFrame
data.shape # (10, 2): 10 records, 2 columns
data.head() # first 5 rows (head(3) for 3)
data.info() # column names, types, non-null counts
data.columns # just the column names
What each tells you:
- DataFrame = pandas’ table object, with many built-in analysis methods.
- shape = (rows, columns).
- head() = a quick peek (real data can have thousands of rows).
- info() = the type of each column (
int64= whole numbers) and the non-null count. If a count is lower than the number of rows, values are missing, which is a data-quality problem.
Documentation tip: when you don’t know how to use a library, open its official docs and adapt the example. Most of the time that is how code gets written.
4. Features (x) and target (y)
The data is supervised: it contains the target (salary). Which column is the target comes from the problem. Split the data into the two parts the model needs:
x = data[['experience']] # feature: the input
y = data[['salary']] # target: the answer to learn
(x and y are only conventions. The double brackets keep x a table, which scikit-learn expects.)
5. Training: LinearRegression
The algorithm comes from scikit-learn (listed in requirements.txt):
from sklearn.linear_model import LinearRegression
lg_model = LinearRegression() # a class -> creating an object gives you the ML model
lg_model.fit(x, y) # fit = TRAINING
LinearRegressionis a class. Calling it creates an object, and that object is your model, already containing the whole learning technique.fit(x, y)is the training step. It looks for the bestmandbby comparing its predictions with the real salaries.
Good to know: for linear regression, scikit-learn finds the best line with a direct maths formula (least squares) instead of repeated nudging. The result is the same best line. Other models, such as logistic regression and neural networks, do train step by step with gradient descent and epochs.
What did the model learn?
lg_model.coef_ # 5000 -> the slope, m
lg_model.intercept_ # 25000 -> the bias, b
So the trained model is: salary = 5000 × experience + 25000.
(Slope = coefficient, and bias = intercept: two names for the same things.)
6. Predicting
predictions = lg_model.predict(x) # predict = INFERENCE
You get one prediction per input record (10 here). Check one by hand: 5 years gives 5000 × 5 + 25000 = 50,000, which matches the data. Try a new value such as 20 years and the model returns 5000 × 20 + 25000.
Remember: fit = train. predict = generate predictions.
Why exactly 100% here? The data is a perfect straight line, and we predicted on the same data we trained on. Real quality checks use data the model has never seen. We do that in the next session.
7. Saving the model
The trained model only exists inside the notebook. To use it elsewhere (for example on a server), save it as a file, the same way a Java build produces a JAR. That file is the model artifact.
!python3 -m pip install joblib # "!" runs a terminal command from a notebook cell
import joblib
joblib.dump(lg_model, "model.pkl") # creates model.pkl
.pkl (“pickle”) is the usual extension. The file holds the learned coefficient and intercept. Copy it to any server and it can predict there. (joblib comes with scikit-learn, so the install may say “already satisfied”.)
Notebook tips
- A notebook keeps its variables in memory. If you stop and restart it, they are gone. Re-run the cells from the top, in order.
- A running notebook holds the terminal. Press
Ctrl + Cto stop it. - Change a cell’s type to Markdown to write notes and documentation inside the notebook.
Common questions
How do we choose the best model for our data? That is mainly the data scientists’ job. scikit-learn has hundreds of algorithms, so they study the options and give you a shortlist of 2 or 3 to train.
What if there are several features? Still one x: put all the feature columns into it. We do this next session.
Where do we get the code and data? The notebook, the data and the code will be shared through the link in the video description.
Recap
- Use a virtual environment per project, and a
requirements.txtwith pinned versions. - pandas reads the data. Look at it first:
shape,head,info. - Split into
x(features) andy(target). LinearRegression().fit(x, y)trains.coef_andintercept_aremandb.predictmakes predictions. Don’t judge a model on its own training data.joblib.dumpsaves the model as a.pklartifact.
Next session: many features, and testing properly.
