🛠️ Popular AI Frameworks

TensorFlow, PyTorch, and Scikit-learn

What are AI Frameworks?

AI frameworks are libraries that provide pre-built tools and functions to make machine learning and deep learning development faster and easier. They handle the complex math and computations, letting you focus on building models.

Why Use Frameworks?

  • Speed: Pre-optimized operations run faster than custom code
  • Simplicity: Complex algorithms in just a few lines
  • GPU Support: Automatic acceleration with graphics cards
  • Community: Thousands of examples and tutorials

📊 Framework Comparison

Framework Best For Learning Curve Industry Use
Scikit-learn Traditional ML, beginners Easy Data science, research
TensorFlow Production, deployment Moderate Google, enterprise
PyTorch Research, experimentation Moderate Facebook, academia
Keras Quick prototypes Easy Startups, education

🔬 Scikit-learn (sklearn)

# Install scikit-learn
# pip install scikit-learn

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import numpy as np

# Load data
iris = load_iris()
X, y = iris.data, iris.target

print("Dataset shape:", X.shape)
print("Classes:", iris.target_names)

# Split data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# Create and train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate
accuracy = accuracy_score(y_test, y_pred)
print(f"\nAccuracy: {accuracy:.2%}")

# Predict new flower
new_flower = [[5.1, 3.5, 1.4, 0.2]]  # Sepal & petal measurements
prediction = model.predict(new_flower)
print(f"Predicted class: {iris.target_names[prediction[0]]}")

Scikit-learn Features:

🔥 TensorFlow & Keras

# Install TensorFlow
# pip install tensorflow

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np

print("TensorFlow version:", tf.__version__)

# Load MNIST dataset (handwritten digits)
mnist = keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Normalize pixel values (0-255 to 0-1)
X_train = X_train / 255.0
X_test = X_test / 255.0

print(f"Training data: {X_train.shape}")
print(f"Test data: {X_test.shape}")

# Build neural network
model = keras.Sequential([
    layers.Flatten(input_shape=(28, 28)),  # Flatten 28x28 images
    layers.Dense(128, activation='relu'),   # Hidden layer
    layers.Dropout(0.2),                    # Prevent overfitting
    layers.Dense(10, activation='softmax')  # Output layer (10 digits)
])

# Compile model
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

# Display model architecture
model.summary()

# Train model
print("\nTraining...")
history = model.fit(
    X_train, y_train,
    epochs=5,
    validation_split=0.1,
    verbose=1
)

# Evaluate on test set
test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"\nTest accuracy: {test_accuracy:.2%}")

# Make prediction
sample_image = X_test[0]
prediction = model.predict(sample_image.reshape(1, 28, 28))
predicted_digit = np.argmax(prediction)
actual_digit = y_test[0]

print(f"\nPredicted: {predicted_digit}, Actual: {actual_digit}")

TensorFlow/Keras Features:

⚡ PyTorch

# Install PyTorch
# pip install torch torchvision

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader

print("PyTorch version:", torch.__version__)
print("CUDA available:", torch.cuda.is_available())

# Define neural network
class SimpleNet(nn.Module):
    def __init__(self):
        super(SimpleNet, self).__init__()
        self.flatten = nn.Flatten()
        self.fc1 = nn.Linear(28 * 28, 128)
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(0.2)
        self.fc2 = nn.Linear(128, 10)
    
    def forward(self, x):
        x = self.flatten(x)
        x = self.fc1(x)
        x = self.relu(x)
        x = self.dropout(x)
        x = self.fc2(x)
        return x

# Load MNIST dataset
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))
])

train_dataset = datasets.MNIST(
    root='./data',
    train=True,
    download=True,
    transform=transform
)

test_dataset = datasets.MNIST(
    root='./data',
    train=False,
    download=True,
    transform=transform
)

# Create data loaders
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)

# Initialize model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = SimpleNet().to(device)

# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop
def train(model, train_loader, criterion, optimizer, epochs=5):
    model.train()
    
    for epoch in range(epochs):
        running_loss = 0.0
        correct = 0
        total = 0
        
        for batch_idx, (data, target) in enumerate(train_loader):
            # Move to device
            data, target = data.to(device), target.to(device)
            
            # Zero gradients
            optimizer.zero_grad()
            
            # Forward pass
            output = model(data)
            loss = criterion(output, target)
            
            # Backward pass
            loss.backward()
            optimizer.step()
            
            # Statistics
            running_loss += loss.item()
            _, predicted = torch.max(output.data, 1)
            total += target.size(0)
            correct += (predicted == target).sum().item()
            
            if batch_idx % 200 == 0:
                print(f'Epoch [{epoch+1}/{epochs}], Step [{batch_idx}/{len(train_loader)}], '
                      f'Loss: {loss.item():.4f}')
        
        accuracy = 100 * correct / total
        print(f'Epoch [{epoch+1}/{epochs}] - Loss: {running_loss/len(train_loader):.4f}, '
              f'Accuracy: {accuracy:.2f}%')

# Test function
def test(model, test_loader):
    model.eval()
    correct = 0
    total = 0
    
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            _, predicted = torch.max(output.data, 1)
            total += target.size(0)
            correct += (predicted == target).sum().item()
    
    accuracy = 100 * correct / total
    print(f'\nTest Accuracy: {accuracy:.2f}%')
    return accuracy

# Train and test
print("\nTraining PyTorch model...")
train(model, train_loader, criterion, optimizer, epochs=3)
test(model, test_loader)

# Save model
torch.save(model.state_dict(), 'mnist_model.pth')
print("\nModel saved to mnist_model.pth")

PyTorch Features:

🎨 Choosing the Right Framework

# Decision guide

framework_guide = {
    'Scikit-learn': {
        'Use when': [
            'Traditional machine learning (not deep learning)',
            'Small to medium datasets',
            'Quick prototyping',
            'You\'re a beginner'
        ],
        'Examples': [
            'Classification problems',
            'Regression tasks',
            'Clustering analysis',
            'Data preprocessing'
        ]
    },
    
    'TensorFlow/Keras': {
        'Use when': [
            'Production deployment needed',
            'Mobile/web deployment',
            'Large-scale projects',
            'Need ecosystem tools'
        ],
        'Examples': [
            'Image recognition',
            'Natural language processing',
            'Time series forecasting',
            'Recommendation systems'
        ]
    },
    
    'PyTorch': {
        'Use when': [
            'Research and experimentation',
            'Custom architectures',
            'Need flexibility',
            'Academic projects'
        ],
        'Examples': [
            'Novel architectures',
            'Research papers',
            'Cutting-edge models',
            'Complex experiments'
        ]
    }
}

# Print guide
for framework, info in framework_guide.items():
    print(f"\n{framework}:")
    print(f"  Use when:")
    for item in info['Use when']:
        print(f"    • {item}")
    print(f"  Examples:")
    for item in info['Examples']:
        print(f"    • {item}")

🚀 Quick Start Tips

For Beginners:

  1. Start with Scikit-learn - Learn ML fundamentals without complexity
  2. Move to Keras/TensorFlow - When you need neural networks
  3. Explore PyTorch - For advanced customization

Installation Commands:

# Install all frameworks
pip install scikit-learn tensorflow torch torchvision

# Or install individually
pip install scikit-learn     # Traditional ML
pip install tensorflow        # TensorFlow + Keras
pip install torch torchvision # PyTorch + vision tools

# Verify installations
python -c "import sklearn; print('sklearn:', sklearn.__version__)"
python -c "import tensorflow as tf; print('TensorFlow:', tf.__version__)"
python -c "import torch; print('PyTorch:', torch.__version__)"

Common Workflow:

# Standard ML workflow (works with all frameworks)

# 1. Import framework
import sklearn  # or tensorflow, or torch

# 2. Load data
# X = features, y = labels

# 3. Preprocess data
# Normalize, encode, split

# 4. Create model
# model = ModelClass(parameters)

# 5. Train model
# model.fit(X_train, y_train)

# 6. Evaluate model
# accuracy = model.score(X_test, y_test)

# 7. Make predictions
# predictions = model.predict(new_data)

# 8. Save model (optional)
# Save for later use

print("This pattern works across all frameworks!")

🎯 Key Takeaways