Machine learning models learn from data using different training methods. The three primary approaches are:
Each of these methods has different use cases, advantages, and limitations. Understanding them is crucial for selecting the right AI model for a given problem.
In supervised learning, models learn from labeled data, where each input is paired with a correct output.
Example:
A model is trained on thousands of emails labeled as spam or not spam. It learns to classify new emails as spam or not.

Example:
Self-driving cars require massive labeled datasets of road signs, pedestrians, and vehicles for training.
In unsupervised learning, the model learns from unlabeled data by identifying patterns and structures.
Example:
A model analyzes customer purchase history and groups customers with similar buying behavior.Unlike supervised learning, there are no predefined categories.

Example:
Netflix groups similar viewers into clusters to recommend movies without explicitly labeled user categories.
Self-supervised learning is a hybrid approach where the model creates its own labels from raw data rather than relying on human-labeled datasets.
Example:
GPT-4 was trained using self-supervised learning on massive text datasets, predicting the next word in a sentence as its training objective.

Example:
GPT-4 and BERT require billions of training examples and high-end GPUs for self-supervised learning.
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score
# Load dataset
digits = load_digits()
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, random_state=42)
# Train model
model = LogisticRegression(max_iter=10000)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Evaluate accuracy
print("Accuracy:", accuracy_score(y_test, y_pred))
Output:
Accuracy: 0.9722222222222222
This code trains a Logistic Regression classifier on the digits dataset and evaluates its accuracy. It follows these key steps:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
# Function to generate and plot K-Means clustering
def kmeans_clustering(n_samples=300, n_clusters=4, cluster_std=0.6, random_state=42):
# Generate synthetic data
X, _ = make_blobs(n_samples=n_samples, centers=n_clusters, cluster_std=cluster_std, random_state=random_state)
# Apply K-Means Clustering
kmeans = KMeans(n_clusters=n_clusters, random_state=random_state)
kmeans.fit(X)
labels = kmeans.labels_
centers = kmeans.cluster_centers_
# Plotting the clusters and their centers
plt.figure(figsize=(10, 6))
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', marker='o', edgecolor='k', s=50, alpha=0.7)
plt.scatter(centers[:, 0], centers[:, 1], c='red', marker='X', s=200, label='Centroids')
plt.title('K-Means Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.grid(True)
plt.show()
# Run the K-Means clustering function
kmeans_clustering()Output:

sklearn → to group dataWe create 300 random points spread across 4 groups (clusters).
Calls kmeans_clustering() to see the clusters in action.
A scatter plot where:
Each color represents a different group
Red “X” marks the center of each group
This is a simple way to see how K-Means automatically finds patterns in data! 🎉
from transformers import pipeline
# Load the pretrained BERT model for masked language modeling
fill_mask = pipeline("fill-mask", model="bert-base-uncased")
# Define the sentence with a masked token
sentence = "The AI model is trained to [MASK] text."
# Get predictions for the masked word
predictions = fill_mask(sentence)
# Display the predictions in a readable format
print("Predicted words for the masked token:")
for prediction in predictions:
print(f"Word: {prediction['token_str']}, Score: {prediction['score']:.4f}")
Output:
Predicted words for the masked token: Word: read, Score: 0.1645 Word: understand, Score: 0.0723 Word: recognize, Score: 0.0501 Word: produce, Score: 0.0404 Word: translate, Score: 0.0337