Machine learning helps computers learn from data. Different types of learning help solve different problems.
When you have labeled data and want to predict outcomes, like predicting house prices.
When you have data without labels and want to find patterns, like grouping customers by behavior.
When you want a computer to learn by trying actions and getting rewards, like teaching a robot to walk.
Syntax
ML Python
Supervised Learning:
- Input: Data with labels
- Goal: Learn to predict labels from inputs
Unsupervised Learning:
- Input: Data without labels
- Goal: Find hidden patterns or groups
Reinforcement Learning:
- Input: Environment and actions
- Goal: Learn actions to maximize rewards
Supervised learning needs labeled examples.
Unsupervised learning finds structure without labels.
Examples
This helps in tasks like image recognition.
ML Python
Supervised Learning example:
Input: Photos labeled as'cat'or'dog'
Output: Model predicts if a new photo is a cat or dog
This helps in marketing to target groups.
ML Python
Unsupervised Learning example:
Input: Customer purchase data without labels
Output: Groups of similar customers
This helps in training AI for games or robots.
ML Python
Reinforcement Learning example:
Input: Game environment
Output: Agent learns moves to win the game
Sample Model
This code shows simple examples of each ML type: supervised classification, unsupervised clustering, and a basic reinforcement learning simulation.
ML Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.cluster import KMeans
import numpy as np
# Supervised Learning example
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=42)
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
supervised_accuracy = clf.score(X_test, y_test)
# Unsupervised Learning example
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(iris.data)
clusters = kmeans.labels_
# Reinforcement Learning example (simple simulation)# Agent learns to choose action 0 or 1 to get reward
rewards = [1, 0] # action 0 gives reward 1, action 1 gives reward 0
actions = []
for _ inrange(5):
action = 0# agent always picks action 0
actions.append(action)
reward = rewards[action]
print(f"Supervised accuracy: {supervised_accuracy:.2f}")
print(f"Unsupervised cluster labels (first 5): {clusters[:5]}")
print(f"Reinforcement actions taken: {actions}")
OutputSuccess
Important Notes
Supervised learning needs examples with answers to learn well.
Unsupervised learning helps find hidden groups or features.
Reinforcement learning learns by trying and getting feedback.
Summary
Supervised learning uses labeled data to predict outcomes.
Unsupervised learning finds patterns without labels.
Reinforcement learning learns actions to get rewards.
Practice
(1/5)
1. Which type of machine learning uses labeled data to teach the model?
easy
A. Supervised learning
B. Unsupervised learning
C. Reinforcement learning
D. Deep learning
Solution
Step 1: Understand labeled data meaning
Labeled data means each example has a known answer or outcome.
Step 2: Match learning type with labeled data
Supervised learning uses labeled data to train models to predict outcomes.
Final Answer:
Supervised learning -> Option A
Quick Check:
Supervised = labeled data [OK]
Hint: Labeled data means supervised learning [OK]
Common Mistakes:
Confusing unsupervised with supervised
Thinking reinforcement uses labeled data
Mixing deep learning as a type here
2. Which of the following is the correct description of unsupervised learning?
easy
A. Learning from labeled data to predict outcomes
B. Finding patterns in data without labels
C. Learning actions to maximize rewards
D. Training deep neural networks
Solution
Step 1: Recall unsupervised learning definition
Unsupervised learning finds hidden patterns or groups in data without labels.
Step 2: Match options with definition
Finding patterns in data without labels correctly states it finds patterns without labels.
Final Answer:
Finding patterns in data without labels -> Option B
Quick Check:
Unsupervised = no labels [OK]
Hint: No labels means unsupervised learning [OK]
Common Mistakes:
Choosing supervised for unlabeled data
Confusing reinforcement with unsupervised
Thinking deep learning is a type of ML here
3. Consider this Python code snippet for a reinforcement learning agent:
Step 1: Identify the current action and reward mapping
The current_action is 'right'. The rewards dictionary maps 'right' to 1.
Step 2: Print the reward for 'right'
reward = rewards['right'] equals 1, so print outputs 1.
Final Answer:
1 -> Option D
Quick Check:
Reward for 'right' = 1 [OK]
Hint: Check dictionary key for current action [OK]
Common Mistakes:
Confusing keys and values in dictionary
Printing variable before assignment
Assuming default reward 0
4. The following code is intended to perform supervised learning but has an error:
data = [([1, 2], 0), ([3, 4], 1), ([5, 6], 0)]
features, labels = zip(*data)
model = SomeModel()
model.train(features, labels)
What is the likely error?
medium
A. Features and labels are not separated correctly
B. Data format is incorrect for supervised learning
C. Model class SomeModel is undefined
D. train method should be fit instead
Solution
Step 1: Check variable unpacking
features and labels are correctly unpacked from data using zip(*data).
Step 2: Identify model usage error
SomeModel is not defined anywhere, so creating model = SomeModel() causes an error.
Final Answer:
Model class SomeModel is undefined -> Option C
Quick Check:
Undefined class causes error [OK]
Hint: Undefined classes cause errors at instantiation [OK]
Common Mistakes:
Assuming train method is wrong without context
Thinking data unpacking is incorrect
Ignoring missing model definition
5. You want to build a system that learns to play a game by trying moves and getting points as feedback. Which type of machine learning should you use?
hard
A. Reinforcement learning
B. Supervised learning
C. Unsupervised learning
D. Clustering
Solution
Step 1: Understand the problem context
The system learns by trying moves and receiving points (rewards) as feedback.
Step 2: Match learning type to feedback-based learning
Reinforcement learning fits because it learns actions to maximize rewards from feedback.
Final Answer:
Reinforcement learning -> Option A
Quick Check:
Learning from rewards = Reinforcement [OK]
Hint: Learning from rewards means reinforcement learning [OK]
Common Mistakes:
Choosing supervised because of 'learning'
Confusing clustering with reinforcement
Ignoring feedback as reward signal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.