Support Vector Machine example with Simple Python
1. We’ll simulate a Support Vector Machine (SVM) with 2 features:
- Income (X-axis)
- Credit Card Debt (Y-axis)
The idea is to:
- Create a few example customers (with labels: good = +1, bad = -1).
- Manually define a decision boundary (a line).
- Predict if new applicants are approved or denied loans based on which side of the line they fall on.
Step-by-Step Plan:
- We’ll define a few points and label them.
- We’ll assume a decision line: w1 * income + w2 * debt + b = 0
- Use the sign of the result to predict whether the person is “Good” or “Bad”.
What is Our Classifier Doing?
If the output of w1 * income + w2 * debt + b is:
- > 0 → predict +1 (Good)
- < 0 → predict -1 (Bad)
Python Simulation (Without Libraries)
# === 1. TRAINING DATA === # Each customer has: [income, debt] # Label: +1 = Good (approve loan), -1 = Bad (deny loan) data = [ {'income': 60, 'debt': 10, 'label': +1}, # Good customer {'income': 80, 'debt': 15, 'label': +1}, # Good customer {'income': 30, 'debt': 60, 'label': -1}, # Bad customer {'income': 20, 'debt': 80, 'label': -1}, # Bad customer ] # === 2. HYPOTHETICAL LINE WE CHOSE === # Decision boundary: w1 * income + w2 * debt + b = 0 w1 = 1 w2 = -1 b = -10 # Adjust b to move the line up/down # === 3. PREDICTION FUNCTION === def svm_predict(income, debt): result = w1 * income + w2 * debt + b if result >= 0: return +1 # Good customer else: return -1 # Bad customer # === 4. TESTING ON TRAINING DATA === print("Evaluating known customers:") for customer in data: pred = svm_predict(customer['income'], customer['debt']) print(f"Income: {customer['income']}, Debt: {customer['debt']} → Prediction: {'Good' if pred == 1 else 'Bad'}") # === 5. PREDICTING NEW CUSTOMERS === print("\nEvaluating new applicants:") new_applicants = [ {'income': 50, 'debt': 20}, # Should be Good {'income': 25, 'debt': 70}, # Should be Bad {'income': 70, 'debt': 40}, # Close call ] for applicant in new_applicants: pred = svm_predict(applicant['income'], applicant['debt']) print(f"Income: {applicant['income']}, Debt: {applicant['debt']} → Prediction: {'Good' if pred == 1 else 'Bad'}")
Output Example
Evaluating known customers:
Income: 60, Debt: 10 → Prediction: Good
Income: 80, Debt: 15 → Prediction: Good
Income: 30, Debt: 60 → Prediction: Bad
Income: 20, Debt: 80 → Prediction: BadEvaluating new applicants:
Income: 50, Debt: 20 → Prediction: Good
Income: 25, Debt: 70 → Prediction: Bad
Income: 70, Debt: 40 → Prediction: Good
What This Taught us?
- The decision line was manually crafted — in real SVM, it’s learned using optimization (by maximizing margin).
- The function w1*x + w2*y + b is the core logic of SVM.
- It shows how close the point is to the boundary and on which side it falls.
Support Vector Machine – Basic Math Concepts