0
0
ML Pythonprogramming~5 mins

Types of ML (supervised, unsupervised, reinforcement) in ML Python

Choose your learning style9 modes available
Introduction

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 Program

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 _ in range(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.