AI vs ML: Same Problem, Two Approaches
Artificial Intelligence is the goal: software that handles tasks which normally need human judgment. Machine Learning is one way to reach that goal: instead of writing the rules yourself, you give the program examples an
Artificial Intelligence is the goal: software that handles tasks which normally need human judgment. Machine Learning is one way to reach that goal: instead of writing the rules yourself, you give the program examples and it learns the pattern. All ML is AI, but not all AI is ML.
Definitions only get you so far, though. The quickest way to feel the difference is to solve one small problem both ways, so that's what this post does.
I work with the R&D team at Solitaire Infosystems, a software development company in Mohali that also runs IT training and internship programs. This is one of the first exercises we give interns in our machine learning training in Chandigarh and Mohali, because it shows the trade-off better than any slide.
The short version
- Artificial Intelligence (AI) is the broad goal: making software do things that normally need human judgment.
- Machine Learning (ML) is one way to get there: the program learns rules from examples instead of you writing them.
- Deep Learning is a subset of ML that uses neural networks with many layers.
A chess program that only searches moves, or a hand-written expert system, is AI without any learning at all.
The project: an enquiry sorter
Imagine an internship page that gets messages like "what will it cost me?". We want to sort each message into one of three labels: fees, duration or eligibility.
Approach 1: Rule-based (AI, no ML)
RULES = {
"fees": ["fee", "fees"],
"duration": ["duration", "long"],
"eligibility": ["eligible", "eligibility"],
}
def rule_based(msg):
words = msg.lower().split()
for label, keywords in RULES.items():
if any(k in words for k in keywords):
return label
return "unknown"
You wrote every rule. It's predictable and easy to debug, but it only knows what you thought of in advance.
Approach 2: Machine Learning
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
train = [
("what is the fee for the internship", "fees"),
("how much do I need to pay", "fees"),
("is there any registration fee", "fees"),
("can I pay in installments", "fees"),
("what is the total cost of the program", "fees"),
("do you charge anything for the course", "fees"),
("how long is the internship", "duration"),
("what is the duration of the training", "duration"),
("how many months does it run", "duration"),
("when does the next batch start and end", "duration"),
("is it six weeks or six months", "duration"),
("how many hours per day are the classes", "duration"),
("who can apply for this internship", "eligibility"),
("am I eligible if I am in final year", "eligibility"),
("do I need prior coding knowledge", "eligibility"),
("can a BCA student join", "eligibility"),
("is this open for diploma students", "eligibility"),
("what qualification is required", "eligibility"),
]
X, y = zip(*train)
model = make_pipeline(
TfidfVectorizer(ngram_range=(1, 2)),
LogisticRegression(max_iter=1000),
)
model.fit(X, y)
print(model.predict(["what will it cost me"])[0]) # fees
Here you wrote no keyword rules. TfidfVectorizer turns each sentence into numbers based on which words and word pairs it contains, and LogisticRegression learns which of those numbers point to which label.
Head to head
I tested both on six messages worded differently from the training data:
| Message | Truth | Rule-based | ML |
|---|---|---|---|
| what will it cost me | fees | unknown | fees |
| how long will this take | duration | duration | duration |
| can I join if I just finished 12th | eligibility | unknown | eligibility |
| is the program paid or do I pay | fees | unknown | fees |
| what is the fee | fees | fees | fees |
| do I have to know python already | eligibility | unknown | fees |
Rule-based: 2 of 6. ML: 5 of 6.
What this shows
-
ML handles wording you never listed. "Cost" and "pay" both appear in the fee examples, so the model linked "what will it cost me" and "do I pay" to
fees. The rule-based version only fires on the exact words inRULES. -
ML fails too, and quietly. The last row is wrong. The rule-based bot said
unknown, which at least admits it doesn't know. The model gave a confident wrong answer, because 18 examples aren't enough to separate "know python" from the fee questions. - Data quality sets the ceiling. The fix isn't a fancier algorithm. It's more varied examples of eligibility questions.
A caveat: six test messages is a demo, not a benchmark. In real projects you hold out a proper test set with train_test_split and measure on hundreds of examples.
When should you use rules, and when ML?
- Rules are fine when the logic is small, fixed and must be explainable, such as validating a form field.
- ML is worth it when the patterns are too varied to write down, such as free-text messages or images.
- Many production systems combine both: ML for the fuzzy part, rules for the hard constraints.
Try it yourself
Don't stop at running my code. Write three messages you think will fool the model, run them, then add them to the training set with the right labels. Retrain and check whether it learned. That loop of break it, label it, retrain is most of what junior ML work looks like.
If you'd like developers reviewing your work while you practise this, that's what interns do in the AI and ML program at Solitaire Infosystems, Mohali, offline or online through live sessions.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.