Bird
Raised Fist0
ML Pythonml~20 mins

ML vs traditional programming in ML Python - Experiment Comparison

Choose your learning style10 modes available
Experiment - ML vs traditional programming
Problem:Understand the difference between traditional programming and machine learning by comparing how each solves a simple task: classifying if a number is even or odd.
Current Metrics:Traditional programming: 100% accuracy on all inputs. ML model: 85% accuracy on test data after training.
Issue:The ML model is less accurate than traditional programming and sometimes makes mistakes on simple even/odd classification.
Your Task
Improve the ML model's accuracy on even/odd classification to at least 95% while keeping the model simple and explainable.
Do not use complex deep learning models.
Use only basic features (the number itself).
Keep the training data small and simple.
Hint 1
Hint 2
Hint 3
Solution
ML Python
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np

# Prepare data: numbers 0 to 99
X = np.array([[i] for i in range(100)])
y = np.array([0 if i % 2 == 0 else 1 for i in range(100)])  # 0=even, 1=odd

# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train decision tree model
model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train)

# Predict on test data
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)

print(f"Test accuracy: {accuracy * 100:.2f}%")
Used decision tree classifier instead of a complex model to keep it simple.
Increased training data size to 80 examples from numbers 0 to 99.
Used clear labels: 0 for even, 1 for odd.
Split data properly to evaluate generalization.
Results Interpretation

Traditional programming: Always 100% accurate because it uses a fixed rule (number % 2 == 0).

Initial ML model: 85% accuracy, sometimes wrong because of limited data or model choice.

Improved ML model: 100% accuracy after training on more data with decision tree classifier.

Traditional programming uses explicit rules and is perfect for simple tasks with clear logic. Machine learning learns patterns from data and can match or exceed traditional methods if given enough good data and the right model.
Bonus Experiment
Try using a decision tree classifier instead of logistic regression and compare accuracy and model simplicity.
💡 Hint
Decision trees split data by conditions and can be visualized easily to understand how decisions are made.

Practice

(1/5)
1. What is the main difference between traditional programming and machine learning?
easy
A. Traditional programming uses fixed rules; machine learning learns from data.
B. Traditional programming learns from data; machine learning uses fixed rules.
C. Both use fixed rules but in different languages.
D. Machine learning does not require any data.

Solution

  1. Step 1: Understand traditional programming

    Traditional programming uses explicit instructions or fixed rules written by humans to solve problems.
  2. Step 2: Understand machine learning

    Machine learning creates models that learn patterns from data examples instead of fixed rules.
  3. Final Answer:

    Traditional programming uses fixed rules; machine learning learns from data. -> Option A
  4. Quick Check:

    Difference = Fixed rules vs learning from data [OK]
Hint: Traditional = rules, ML = learns from examples [OK]
Common Mistakes:
  • Confusing which method uses fixed rules
  • Thinking ML does not need data
  • Assuming both methods are the same
2. Which of the following is the correct way to describe traditional programming?
easy
A. It uses fixed rules written by programmers.
B. It predicts outcomes without any rules.
C. It learns rules from data automatically.
D. It requires no human input at all.

Solution

  1. Step 1: Recall traditional programming definition

    Traditional programming relies on fixed rules explicitly coded by humans.
  2. Step 2: Evaluate options

    Only It uses fixed rules written by programmers. correctly states this; others describe machine learning or incorrect ideas.
  3. Final Answer:

    It uses fixed rules written by programmers. -> Option A
  4. Quick Check:

    Traditional programming = fixed rules [OK]
Hint: Traditional = fixed rules by humans [OK]
Common Mistakes:
  • Mixing up learning from data with fixed rules
  • Thinking traditional programming predicts without rules
  • Believing no human input is needed
3. Consider this code snippet for a traditional program and a machine learning model:
def predict_traditional(x):
    if x > 10:
        return 'High'
    else:
        return 'Low'

# Machine learning model trained on data
model = train_model(data)
prediction = model.predict(15)
What is the key difference in how these two make predictions?
medium
A. Both predict randomly without rules.
B. Both use fixed if-else rules to predict.
C. ML model uses if-else rules; traditional learns from data.
D. Traditional uses fixed if-else rules; ML model predicts based on learned patterns.

Solution

  1. Step 1: Analyze traditional code

    The traditional function uses a fixed if-else rule to decide output based on input.
  2. Step 2: Analyze ML model prediction

    The ML model predicts based on patterns learned from training data, not fixed rules.
  3. Final Answer:

    Traditional uses fixed if-else rules; ML model predicts based on learned patterns. -> Option D
  4. Quick Check:

    Traditional = rules, ML = learned patterns [OK]
Hint: Traditional = if-else rules; ML = learned from data [OK]
Common Mistakes:
  • Assuming ML uses fixed if-else rules
  • Thinking traditional learns from data
  • Believing both predict randomly
4. A programmer wrote this code to classify emails as spam or not:
def classify_email(email):
    if 'free' in email:
        return 'spam'
    else:
        return 'not spam'

# Later, they try to use machine learning but wrote:
model = train_model(data)
result = model.predict('free money')
What is the main problem with this approach?
medium
A. The traditional code is better because it uses fixed rules.
B. The traditional code uses fixed rules; ML needs training data and proper model setup.
C. ML models do not work with text data.
D. The code will run without any issues.

Solution

  1. Step 1: Identify traditional code limitation

    The traditional function uses a simple fixed rule that may miss many spam cases.
  2. Step 2: Understand ML model needs

    ML requires proper training data and model setup before predicting; just calling predict without training causes errors or wrong results.
  3. Final Answer:

    The traditional code uses fixed rules; ML needs training data and proper model setup. -> Option B
  4. Quick Check:

    ML requires training, not just predict call [OK]
Hint: ML needs training data and setup before prediction [OK]
Common Mistakes:
  • Thinking ML works without training
  • Believing fixed rules are always better
  • Ignoring model setup requirements
5. You have a complex problem where rules are hard to write, but you have lots of labeled data. Which approach is best and why? Options: A) Use traditional programming because rules are clearer. B) Use machine learning because it can learn from data patterns. C) Use traditional programming because it needs no data. D) Use neither because both require manual rules.
hard
A. Use traditional programming because rules are clearer.
B. Use traditional programming because it needs no data.
C. Use machine learning because it can learn from data patterns.
D. Use neither because both require manual rules.

Solution

  1. Step 1: Analyze problem complexity and data availability

    When rules are hard to write but labeled data is available, machine learning can find patterns automatically.
  2. Step 2: Compare approaches

    Traditional programming struggles with complex rules; ML excels with data-driven learning.
  3. Final Answer:

    Use machine learning because it can learn from data patterns. -> Option C
  4. Quick Check:

    Complex problem + data = machine learning [OK]
Hint: Complex rules + data = choose machine learning [OK]
Common Mistakes:
  • Choosing traditional programming despite complex rules
  • Ignoring data availability
  • Thinking both require manual rules
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard