Classification & Training Models

From Predicting Hires to Modeling Salaries

In this chapter, we dive into the "Workhorses" of Machine Learning. We'll start with Classification—learning how to predict categorical outcomes like "Will this candidate accept our offer?" or "Which department does this skill set belong to?"

Then, we'll explore Training Models. We'll look under the hood of Linear and Logistic Regression to understand how machines "learn" the relationship between years of experience and salary, or the probability of an employee reaching retirement age.

Training a Binary Classifier

Binary classification is the bedrock of automated decision-making. It deals with Disjoint Classes—meaning an instance can only belong to one of two categories. In HR, this is the "Offer Acceptance" problem: either they join, or they don't.

To build this, we define a Decision Boundary. Imagine a graph where one axis is "Salary Increase" and the other is "Commute Time". The classifier's job is to find the mathematical line that best separates the "Yes" dots from the "No" dots. If a new candidate falls on the 'Yes' side of the line, we predict an acceptance.

Recruitment Logic: The code below shows a threshold-based classifier. In complex ML, these thresholds are not hard-coded by humans but are "learned" from thousands of historical offer letters.

Python
# Binary Classification: Will they accept the offer?def predict_acceptance(salary_bump_pct, commute_km):    # Logic: 1 = Accept, 0 = Decline    if salary_bump_pct > 15 and commute_km < 20:        return 1    elif salary_bump_pct > 5 and commute_km < 5:        return 1    return 0 print(f"Offer (20% bump, 10km): {predict_acceptance(20, 10)}") # 1print(f"Offer ( 8% bump, 45km): {predict_acceptance(8, 45)}")  # 0

Performance Measures: Beyond Accuracy

Accuracy is a dangerous metric in HR because classes are often Imbalanced. If only 2% of your employees are "Exceptional High Performers", a model that predicts "Nobody is Exceptional" is 98% accurate, yet it is a total failure for Talent Management.

  • Precision (Quality): This measures how reliable the model's "Positive" predictions are. If the model flags 10 people as "High Potential", and 8 actually are, your Precision is 80%. High precision means fewer "False Alarms".
  • Recall (Quantity): This measures the model's ability to find all the needles in the haystack. If there were actually 20 high-potential people, but the model only found 8, your Recall is 40%. You missed 12 superstars!

The Trade-off: Usually, increasing Recall makes Precision go down, and vice versa. Finding the perfect "F1 Score" (the harmonic mean) is the key to a balanced HR system.

Python
# Performance: Precision & Recall for 'Talent ID'# TP: Found 8 high-performers correctly# FP: Flagged 2 people as high-performers who weren't# FN: Missed 5 actual high-performerstp, fp, fn = 8, 2, 5 precision = tp / (tp + fp)recall = tp / (tp + fn) print(f"Precision: {precision:.2f} (When we flag someone, how often are we right?)")print(f"Recall   : {recall:.2f} (What % of the total high-performers did we find?)")

Multiclass Classification

Real life isn't always Yes/No. Often, we need to sort employees into multiple buckets, such as "Engineering", "Sales", "HR", or "Legal". This is Multiclass Classification.

One common strategy is One-vs-All (OvA). To identify a "Sales" profile, the model builds a binary classifier for "Sales vs Everyone Else". It repeats this for every department and finally picks the one with the highest confidence score. This allows us to map complex skill sets to the most appropriate business unit.

The Role Recommender: Below, we simulate this by mapping a list of skills directly to IDs. This is the logic used by internal talent marketplaces to suggest lateral career moves.

Python
# Multiclass: Categorizing Talentskills = ["coding", "sales", "hiring", "coding", "sales"]# Map: 0=Eng, 1=Sales, 2=HRmapping = {"coding": 0, "sales": 1, "hiring": 2} categorized = [mapping[s] for s in skills]print(f"Skills: {skills}")print(f"Dept IDs: {categorized}")

Training Models: Linear Regression

Regression is used when we want to predict a Continuous Numeric Value, like a salary or a bonus amount, rather than a category. Linear Regression assumes there is a straight-line relationship between your input (Experience) and your output (Salary).

The model finds the Line of Best Fit by calculating the intercept (starting salary) and the slope (pay increase per year). In statistics, we call this the "Ordinary Least Squares" method, where we try to minimize the vertical distance between every actual employee's salary and our predicted line.

The Pay Scale Equation: Below is a simplified manual version of this model. Training a model is simply the process of finding the optimal base_pay and pay_per_year values.

Python
# Linear Regression: Manual Salary Predictordef get_salary_line(exp_years):    base_pay = 40000    pay_per_year = 4500    return base_pay + (pay_per_year * exp_years) print(f"5 Years Exp: ${get_salary_line(5):,}")print(f"12 Years Exp: ${get_salary_line(12):,}")

Gradient Descent: The Learning Engine

How does a model "learn" without being told the answer? It uses Gradient Descent—a repetitive optimization algorithm. Imagine being blindfolded on a mountain and trying to find the valley. You'd feel the slope under your feet and take a step in the direction where it goes down most steeply.

In ML, the "height" of the mountain is the Error of our model. We calculate the gradient (slope) of the error and take a small step (the learning_rate) to reduce it. We repeat this thousands of times until the error is as low as possible.

Learning by Correction: Below is a simple loop that "nudges" our guess closer to the truth by looking at the error in every step.

Python
# Gradient Descent: The 'Correction' Loopactual_weight = 5.0my_guess = 1.0learning_rate = 0.1 for i in range(10):    error = actual_weight - my_guess    my_guess += error * learning_rate    print(f"Step {i+1}: Guessing {my_guess:.2f}") print(f"Final Trained Weight: {my_guess:.2f}")

Regularization: Avoiding Over-Reliance

When a model learns too much detail from its training data, it becomes Overfit. It starts seeing "patterns" in random noise—like thinking someone is a better performer just because they have a certain hobby or their name starts with 'S'.

Regularization (like Ridge or Lasso) acts as a "Complexity Tax". It adds a penalty to the model for having weights that are too large. This forces the model to ignore the small, noisy features and focus only on the strong, generalized ones (like Skill Level and Tenure). This ensures the model works just as well on NEW data as it did on our historical training data.

Python
# Regularization: Shrinking 'Noisy' Featuresweights = {"tenure": 12.5, "certifications": 8.0, "hobby_score": 45.0} # Hobby is too high! # Penalty dampens everything penalty_factor = 0.5regularized = {k: v * penalty_factor for k, v in weights.items()} print(f"Weights before : {weights}")print(f"Weights after  : {regularized} (More balanced!)")

Logistic Regression: Probability Scores

Logistic Regression is the "bridge" between Linear Regression and Classification. Instead of predicting a raw number like salary, it predicts the Probability (0 to 1) of a class. It does this by passing the linear result through a Sigmoid Function.

The Sigmoid function squashes any input into an "S-Curve". If the result is 0.85, that means an 85% probability of turnover. This allows HR managers to prioritize their outreach to "At-Risk" employees based on a ranked list of probabilities, rather than a simple Yes/No guess.

Python
# Logistic Regression: Sigmoid Risk Scoringimport math def sigmoid(x):    return 1 / (1 + math.exp(-x)) def turnover_prob(risk_score):    # risky = Positive sign, Safe = Negative sign    return sigmoid(risk_score) print(f"Prob (Safe Score -2): {turnover_prob(-2)*100:.1f}%")print(f"Prob (High Risk  2): {turnover_prob(2)*100:.1f}%")
PythonRuns entirely in your browser — nothing is sent to a server.

Practice Questions

Question 1

In a hiring model, what is a 'False Positive'?

  • Failing to hire a superstar candidate
  • Hiring a candidate who turns out to be a poor performer
  • Rejecting a candidate who was underqualified
  • Predicting someone will quit and they stay

Question 2

Which algorithm would you use to predict an employee's exact Salary based on their tenure and skill certifications?

  • Binary Classification
  • Linear Regression
  • Logistic Regression
  • Clustering

Question 3

What is the purpose of 'Regularization' in Machine Learning?

  • To make the code run faster
  • To prevent the model from over-relying on a few features (overfitting)
  • To delete biased data
  • To increase the complexity of the model