Python Machine Learning & Deep Learning

Python has become the dominant language for machine learning and deep learning, offering a rich ecosystem of libraries and frameworks. This guide covers the essential concepts, libraries, and techniques for implementing machine learning and deep learning solutions in Python.

Machine Learning Fundamentals

Getting Started with Scikit-learn

Scikit-learn is Python's most popular machine learning library, offering implementations of many algorithms with a consistent, easy-to-use API.

# Install scikit-learn
# pip install scikit-learn

# Basic workflow with scikit-learn
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

# Sample data (X: features, y: target labels)
X = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]])
y = np.array([0, 0, 0, 1, 1, 1])

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)

# Standardize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Create and train the model
model = LogisticRegression()
model.fit(X_train_scaled, y_train)

# Make predictions
y_pred = model.predict(X_test_scaled)

# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))

Common Machine Learning Algorithms

Algorithm Type Use Case scikit-learn Class
Linear Regression Supervised / Regression Predicting numeric values LinearRegression
Logistic Regression Supervised / Classification Binary classification LogisticRegression
Decision Trees Supervised / Both Classification/Regression DecisionTreeClassifier
Random Forest Supervised / Both Ensemble learning RandomForestClassifier
SVM Supervised / Both High-dimensional data SVC, SVR
K-Means Unsupervised / Clustering Grouping similar data KMeans
PCA Unsupervised / Dim. Reduction Feature reduction PCA
# Examples of different algorithms in scikit-learn

# Decision Tree example
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets

# Load a built-in dataset
iris = datasets.load_iris()
X, y = iris.data, iris.target

# Train the model
tree_model = DecisionTreeClassifier(max_depth=3, random_state=42)
tree_model.fit(X, y)

# Random Forest example
from sklearn.ensemble import RandomForestClassifier

rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
rf_pred = rf_model.predict(X_test)

# K-Means Clustering example
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

# Create a model with 3 clusters
kmeans = KMeans(n_clusters=3, random_state=42)
clusters = kmeans.fit_predict(X)

# Visualize the clusters (if 2D data)
# plt.scatter(X[:, 0], X[:, 1], c=clusters)
# plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='red')
# plt.show()

Deep Learning with TensorFlow/Keras

TensorFlow is a powerful deep learning framework, and Keras is the high-level API that makes it easy to build and train neural networks.

# Install TensorFlow
# pip install tensorflow

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

# Example: Building a simple neural network
# 1. Define the model
model = keras.Sequential([
    layers.Dense(128, activation='relu', input_shape=(784,)),
    layers.Dropout(0.2),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(10, activation='softmax')
])

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

# 3. Load and prepare data (e.g., MNIST)
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.reshape(-1, 784).astype('float32') / 255
x_test = x_test.reshape(-1, 784).astype('float32') / 255

# 4. Train the model
model.fit(
    x_train, y_train,
    batch_size=64,
    epochs=5,
    validation_split=0.2
)

# 5. Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_acc:.3f}')

Convolutional Neural Networks (CNNs)

CNNs are specialized neural networks designed for processing grid-like data, such as images.

# Building a CNN for image classification
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import cifar10

# Load dataset
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()

# Normalize pixel values to be between 0 and 1
train_images = train_images.astype('float32') / 255
test_images = test_images.astype('float32') / 255

# Define the model architecture
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

# Compile the model
model.compile(
    optimizer='adam',
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy']
)

# Train the model
history = model.fit(
    train_images, train_labels, 
    epochs=10, 
    validation_data=(test_images, test_labels)
)

# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f'Test accuracy: {test_acc:.3f}')

NLP and Transfer Learning

Transfer learning leverages pre-trained models to achieve excellent results with less data and training time.

# Text classification with pre-trained BERT
# pip install transformers tensorflow

from transformers import BertTokenizer, TFBertForSequenceClassification
import tensorflow as tf

# Load pre-trained model and tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = TFBertForSequenceClassification.from_pretrained('bert-base-uncased')

# Example text for sentiment analysis
texts = ["I love this product!", "This was a terrible experience."]
labels = [1, 0]  # 1 for positive, 0 for negative

# Tokenize and prepare inputs
encoded_inputs = tokenizer(
    texts,
    padding=True,
    truncation=True,
    return_tensors="tf"
)

# Convert labels to tensors
tf_labels = tf.convert_to_tensor(labels)

# Fine-tune the model
optimizer = tf.keras.optimizers.Adam(learning_rate=5e-5)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')

model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
model.fit(
    {k: v for k, v in encoded_inputs.items()},
    tf_labels,
    epochs=3
)

Model Evaluation and Hyperparameter Tuning

Proper evaluation and tuning are essential for developing effective machine learning models.

# Cross-validation and hyperparameter tuning
from sklearn.model_selection import cross_val_score, GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets

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

# Cross-validation
model = RandomForestClassifier(random_state=42)
scores = cross_val_score(model, X, y, cv=5)  # 5-fold cross-validation
print(f"Cross-validation scores: {scores}")
print(f"Mean accuracy: {scores.mean():.3f}")

# Grid search for hyperparameter tuning
param_grid = {
    'n_estimators': [10, 50, 100],
    'max_depth': [None, 10, 20],
    'min_samples_split': [2, 5, 10]
}

grid_search = GridSearchCV(
    RandomForestClassifier(random_state=42),
    param_grid,
    cv=5,
    scoring='accuracy',
    verbose=1
)

grid_search.fit(X, y)

print(f"Best parameters: {grid_search.best_params_}")
print(f"Best cross-validation score: {grid_search.best_score_:.3f}")

# Feature importance
best_model = grid_search.best_estimator_
importances = best_model.feature_importances_
feature_names = iris.feature_names

for feature, importance in zip(feature_names, importances):
    print(f"{feature}: {importance:.4f}")

Best Practices and Tips

General Best Practices

  • Always split your data into training, validation, and test sets
  • Preprocess your data (normalization, handling missing values, encoding)
  • Start with simple models before moving to complex ones
  • Use cross-validation to evaluate model performance
  • Watch for overfitting and use techniques like regularization when needed
  • Perform hyperparameter tuning to optimize your model
  • Create reproducible experiments (set random_state/seed)
  • Build a proper data pipeline for consistency

Deep Learning Tips

  • Use GPU acceleration when possible
  • Apply data augmentation for image tasks
  • Consider transfer learning for faster results
  • Use learning rate schedulers
  • Monitor training with tools like TensorBoard
  • Implement early stopping to prevent overfitting
  • Save checkpoints during training

Practice Exercises

  1. Build a regression model to predict house prices using the Boston Housing dataset
  2. Create a classification model for the MNIST dataset using scikit-learn
  3. Implement a CNN for image classification using the CIFAR-10 dataset
  4. Build an LSTM model for sentiment analysis on the IMDB dataset
  5. Train a model using transfer learning for your own image classification task