Types of Machine Learning
Machine learning (ML) is a subset of artificial intelligence that enables systems to learn patterns from data and make predictions. It is generally categorized into the following types:
Supervised Learning
- The model is trained on labeled data (i.e., data with known outputs).
- The goal is to learn the mapping from inputs to correct outputs.
- Common applications: Spam detection, Image classification, Stock price prediction
- Example:
from sklearn.linear_model import LinearRegression
import numpy as np
x_train = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y_train = np.array([30, 35, 40, 50, 60])
model = LinearRegression()
model.fit(x_train, y_train)
print("Predicted salary for 6 years of experience:", model.predict([[6]])[0])
Unsupervised Learning
- The model is trained on unlabeled data (i.e., no predefined outputs).
- The goal is to find hidden structures or patterns in data.
- Common applications: Customer segmentation, Anomaly detection, Market basket analysis
- Example:
from sklearn.cluster import KMeans
import numpy as np
data = np.array([[2, 3], [10, 15], [5, 8], [12, 14], [6, 9]])
kmeans = KMeans(n_clusters=2, random_state=42)
kmeans.fit(data)
print("Cluster assignments:", kmeans.labels_)
Reinforcement Learning (RL)
- The model learns through trial and error and receives rewards or penalties based on actions.
- Used in game AI, robotics, and autonomous systems.
- Includes Proximal Policy Optimization (PPO), Generalized Proximal Policy Optimization (GRPO).
- Example: Self-learning game AI like AlphaGo.
Value-Based vs. Rule-Based Learning
- Value-Based Learning: The AI assigns numerical values to different outcomes and chooses actions maximizing future rewards (e.g., Q-learning in RL).
- Rule-Based Learning: The AI follows predefined rules (e.g., Decision Trees, Expert Systems).
Regression & Classification Basics
Machine learning models are not only categorized into regression and classification but also include reinforcement learning and generative models. However, regression and classification are the most fundamental predictive learning approaches.
Regression (Predicting continuous values)
- Used when the output is a continuous value (e.g., stock prices, temperature prediction).
- Example:
from sklearn.linear_model import Ridge
ridge_model = Ridge(alpha=1.0)
ridge_model.fit(x_train, y_train)
print("Ridge model prediction for 6 years of experience:", ridge_model.predict([[6]])[0])
Classification (Categorizing inputs)
- Used when the output belongs to discrete categories (e.g., spam vs. not spam, dog vs. cat).
- Example:
from sklearn.neighbors import KNeighborsClassifier
X_train = np.array([[1, 2], [2, 3], [3, 3], [6, 7], [7, 8]])
y_train = np.array([0, 0, 0, 1, 1]) # Labels: 0 and 1
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
print("Predicted class:", knn.predict([[4, 5]])[0])
Training Large Language Models (LLMs)
Training a Large Language Model (LLM) involves:
- Data Collection: Gathering vast amounts of text data (e.g., books, Wikipedia, forums).
- Tokenization: Splitting text into manageable units (tokens).
- Neural Network Architecture: Using Transformer models like GPT, BERT.
- Training Process:
- Forward propagation: Input is passed through multiple layers.
- Backpropagation: Model learns by adjusting weights based on errors.
- Optimization: Using techniques like AdamW Optimizer.
- Training at Scale: Running on TPUs/GPUs across distributed servers.
Example Code for Tokenization:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
text = "Machine learning is fascinating!"
tokens = tokenizer.tokenize(text)
print(tokens)
Fine-Tuning Large Language Models
Fine-tuning is an optimization step that tailors a pre-trained model to a specific task.
Steps in Fine-Tuning:
- Dataset Preparation: Smaller, task-specific data is used (e.g., medical texts for healthcare AI).
- Transfer Learning: Pre-trained weights are adjusted instead of training from scratch.
- Hyperparameter Optimization: Learning rate, batch size, epochs are adjusted.
- Task-Specific Training: Examples:
- Chatbot Fine-Tuning: Training on customer support conversations.
- Sentiment Analysis: Fine-tuning a model to detect emotions in reviews.
Example of Fine-Tuning a Pre-Trained Model:
from transformers import BertForSequenceClassification, Trainer, TrainingArguments
model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)
training_args = TrainingArguments(output_dir="./results", num_train_epochs=3, per_device_train_batch_size=16)
trainer = Trainer(model=model, args=training_args)
trainer.train()
AI Models in Simple Applications
Here are a few common AI models used in real-world applications:
Model | Application | Example Use Case |
---|---|---|
Linear Regression | Salary prediction | Predicting employee salary based on experience |
Logistic Regression | Medical diagnosis | Predicting disease risk based on patient data |
K-Means Clustering | Customer segmentation | Grouping similar customers for targeted marketing |
K-Nearest Neighbors (KNN) | Spam detection | Classifying emails as spam or non-spam |
Decision Trees | Credit risk analysis | Predicting loan approval likelihood |
Credit Risk Analysis Example:
from sklearn.tree import DecisionTreeClassifier
X_train = np.array([[700, 1], [650, 0], [720, 1], [580, 0], [630, 1]]) # [Credit Score, Has Stable Job (0/1)]
y_train = np.array([1, 0, 1, 0, 0]) # 1: Approved, 0: Denied
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, y_train)
print("Credit approval prediction:", decision_tree.predict([[680, 1]])[0])
Summary
- Supervised, Unsupervised, and Reinforcement Learning are key ML types.
- LLMs require massive datasets and powerful computing resources.
- Fine-tuning improves AI performance for specific tasks.
- Machine learning is widely used in real-world applications like finance, healthcare, and marketing.