Bird
Raised Fist0
ML Pythonml~20 mins

Types of ML (supervised, unsupervised, reinforcement) in ML Python - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Types of ML (supervised, unsupervised, reinforcement)
Problem:You want to understand how different types of machine learning work by trying simple examples of supervised, unsupervised, and reinforcement learning.
Current Metrics:No model trained yet; no accuracy or reward scores available.
Issue:Lack of practical experience with how each type of ML learns and performs.
Your Task
Build and run simple examples for each ML type: supervised learning classification, unsupervised clustering, and reinforcement learning with a basic environment. Observe and compare their outputs.
Use small datasets or environments for quick training.
Keep models simple to focus on understanding, not performance.
Use Python with common ML libraries (scikit-learn, gym, etc.).
Hint 1
Hint 2
Hint 3
Solution
ML Python
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score
import gym

# Supervised Learning: Iris classification
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
supervised_accuracy = accuracy_score(y_test, y_pred)

# Unsupervised Learning: Iris clustering
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(X)
clusters = kmeans.labels_

# Reinforcement Learning: CartPole with random actions
env = gym.make('CartPole-v1')
state, info = env.reset(seed=42)
total_reward = 0
for _ in range(100):
    action = env.action_space.sample()  # random action
    state, reward, done, truncated, info = env.step(action)
    total_reward += reward
    if done or truncated:
        break
env.close()

print(f"Supervised Learning Accuracy: {supervised_accuracy:.2f}")
print(f"Unsupervised Learning Cluster labels (first 10): {clusters[:10]}")
print(f"Reinforcement Learning Total Reward (random policy): {total_reward}")
Added a supervised learning example using Decision Tree on iris dataset to classify flowers.
Added an unsupervised learning example using KMeans clustering on iris data without labels.
Added a reinforcement learning example running a random policy on CartPole environment to collect total reward.
Updated gym environment reset and step calls to match latest API (reset returns state and info; step returns state, reward, done, truncated, info).
Results Interpretation

Before: No models trained, no results.

After: Supervised model classifies iris flowers with about 95% accuracy. Unsupervised model groups data into clusters without labels. Reinforcement learning agent with random actions gets low total reward in CartPole.

Supervised learning uses labeled data to predict outcomes accurately. Unsupervised learning finds hidden patterns without labels. Reinforcement learning learns by trial and error to maximize rewards in an environment.
Bonus Experiment
Try improving the reinforcement learning example by implementing a simple Q-learning agent instead of random actions.
💡 Hint
Use a Q-table to store action values and update it based on rewards received after each action.

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

  1. Step 1: Understand labeled data meaning

    Labeled data means each example has a known answer or outcome.
  2. Step 2: Match learning type with labeled data

    Supervised learning uses labeled data to train models to predict outcomes.
  3. Final Answer:

    Supervised learning -> Option A
  4. 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

  1. Step 1: Recall unsupervised learning definition

    Unsupervised learning finds hidden patterns or groups in data without labels.
  2. Step 2: Match options with definition

    Finding patterns in data without labels correctly states it finds patterns without labels.
  3. Final Answer:

    Finding patterns in data without labels -> Option B
  4. 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:
actions = ['left', 'right', 'up', 'down']
rewards = {'left': -1, 'right': 1, 'up': 0, 'down': 0}
current_action = 'right'
reward = rewards[current_action]
print(reward)
What will be the output?
medium
A. 0
B. -1
C. null
D. 1

Solution

  1. Step 1: Identify the current action and reward mapping

    The current_action is 'right'. The rewards dictionary maps 'right' to 1.
  2. Step 2: Print the reward for 'right'

    reward = rewards['right'] equals 1, so print outputs 1.
  3. Final Answer:

    1 -> Option D
  4. 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

  1. Step 1: Check variable unpacking

    features and labels are correctly unpacked from data using zip(*data).
  2. Step 2: Identify model usage error

    SomeModel is not defined anywhere, so creating model = SomeModel() causes an error.
  3. Final Answer:

    Model class SomeModel is undefined -> Option C
  4. 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

  1. Step 1: Understand the problem context

    The system learns by trying moves and receiving points (rewards) as feedback.
  2. Step 2: Match learning type to feedback-based learning

    Reinforcement learning fits because it learns actions to maximize rewards from feedback.
  3. Final Answer:

    Reinforcement learning -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard