handson-ml3
A series of Jupyter notebooks that walk you through the fundamentals of Machine Learning and Deep Learning in Python using Scikit-Learn, Keras and TensorFlow 2.
Top Related Projects
An Open Source Machine Learning Framework for Everyone
scikit-learn: machine learning in Python
Deep Learning for humans
Tensors and Dynamic neural networks in Python with strong GPU acceleration
12 weeks, 26 lessons, 52 quizzes, classic Machine Learning for all
The fastai deep learning library
Quick Overview
Handson-ml3 is a GitHub repository containing Jupyter notebooks and Python scripts for the third edition of Aurélien Géron's book "Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow". It provides practical examples and exercises covering various machine learning and deep learning concepts using popular libraries.
Pros
- Comprehensive coverage of machine learning and deep learning topics
- Hands-on approach with practical examples and exercises
- Up-to-date content aligned with the latest versions of Scikit-Learn, Keras, and TensorFlow
- Clear explanations and well-structured notebooks
Cons
- Requires a solid understanding of Python programming
- Some advanced topics may be challenging for beginners
- Large repository size due to numerous notebooks and datasets
- Occasional updates may be needed to keep pace with rapidly evolving libraries
Code Examples
- Loading and preprocessing data using Scikit-Learn:
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
housing = fetch_california_housing()
X_train, X_test, y_train, y_test = train_test_split(housing.data, housing.target, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
- Creating and training a neural network using Keras:
from tensorflow import keras
model = keras.models.Sequential([
keras.layers.Dense(30, activation="relu", input_shape=X_train.shape[1:]),
keras.layers.Dense(1)
])
model.compile(loss="mse", optimizer=keras.optimizers.SGD(learning_rate=1e-3))
history = model.fit(X_train_scaled, y_train, epochs=20, validation_split=0.2)
- Implementing a custom layer in TensorFlow:
import tensorflow as tf
class MyDense(tf.keras.layers.Layer):
def __init__(self, units, activation=None, **kwargs):
super().__init__(**kwargs)
self.units = units
self.activation = tf.keras.activations.get(activation)
def build(self, batch_input_shape):
self.kernel = self.add_weight(
name="kernel", shape=[batch_input_shape[-1], self.units],
initializer="glorot_normal")
self.bias = self.add_weight(
name="bias", shape=[self.units], initializer="zeros")
super().build(batch_input_shape)
def call(self, X):
return self.activation(X @ self.kernel + self.bias)
Getting Started
To get started with the handson-ml3 repository:
-
Clone the repository:
git clone https://github.com/ageron/handson-ml3.git
-
Install the required dependencies:
cd handson-ml3 pip install -r requirements.txt
-
Launch Jupyter Notebook:
jupyter notebook
-
Open and run the notebooks in the
notebooks
directory to explore the examples and exercises.
Competitor Comparisons
An Open Source Machine Learning Framework for Everyone
Pros of tensorflow
- Comprehensive, official library for machine learning and deep learning
- Extensive ecosystem with tools, extensions, and community support
- High-performance, scalable for large-scale deployments
Cons of tensorflow
- Steeper learning curve for beginners
- More complex setup and configuration
- Frequent updates may lead to compatibility issues
Code comparison
handson-ml3:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
tensorflow:
import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
Summary
handson-ml3 is a practical, beginner-friendly repository focused on teaching machine learning concepts using various libraries, including TensorFlow. It provides hands-on examples and Jupyter notebooks for learning.
tensorflow is the official repository for the TensorFlow library, offering a comprehensive set of tools and resources for building and deploying machine learning models. It's more suitable for advanced users and large-scale projects.
While handson-ml3 is ideal for learning and experimentation, tensorflow is better suited for production-ready applications and cutting-edge research in machine learning and deep learning.
scikit-learn: machine learning in Python
Pros of scikit-learn
- Comprehensive machine learning library with a wide range of algorithms and tools
- Well-established, mature project with extensive documentation and community support
- Designed for production use with efficient implementations and scalability features
Cons of scikit-learn
- Steeper learning curve for beginners due to its extensive functionality
- Less focus on deep learning and neural networks compared to handson-ml3
- May require additional libraries for more advanced machine learning tasks
Code Comparison
handson-ml3:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
scikit-learn:
from sklearn.neural_network import MLPClassifier
model = MLPClassifier(hidden_layer_sizes=(64,), activation='relu')
The handson-ml3 example uses TensorFlow for neural networks, while scikit-learn provides a simpler API for basic neural network models. handson-ml3 offers more flexibility for deep learning tasks, whereas scikit-learn focuses on traditional machine learning algorithms and provides a consistent API across various models.
Deep Learning for humans
Pros of Keras
- Comprehensive deep learning library with extensive documentation
- Supports multiple backend engines (TensorFlow, Theano, CNTK)
- Large community and ecosystem of extensions
Cons of Keras
- Less focus on general machine learning concepts
- May be overwhelming for beginners due to its extensive API
- Limited coverage of data preprocessing and feature engineering
Code Comparison
Keras:
from keras.models import Sequential
from keras.layers import Dense
model = Sequential([
Dense(64, activation='relu', input_shape=(10,)),
Dense(1, activation='sigmoid')
])
handson-ml3:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(10,)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
Summary
Keras is a powerful deep learning library, while handson-ml3 is a comprehensive machine learning tutorial. Keras offers a more extensive API for deep learning tasks, but handson-ml3 provides a broader introduction to machine learning concepts. The code comparison shows that handson-ml3 uses TensorFlow's implementation of Keras, while the Keras repository showcases the standalone Keras API.
Tensors and Dynamic neural networks in Python with strong GPU acceleration
Pros of PyTorch
- Extensive ecosystem with a wide range of tools and libraries
- More flexible and dynamic computational graph
- Better support for distributed computing and GPU acceleration
Cons of PyTorch
- Steeper learning curve for beginners
- Less focus on high-level APIs, requiring more low-level coding
- Smaller community compared to TensorFlow/Keras
Code Comparison
handson-ml3:
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
PyTorch:
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 64)
self.fc2 = nn.Linear(64, 10)
def forward(self, x):
x = nn.functional.relu(self.fc1(x))
return nn.functional.softmax(self.fc2(x), dim=1)
handson-ml3 focuses on practical machine learning examples using TensorFlow and Scikit-learn, making it ideal for beginners and those seeking hands-on experience. PyTorch, on the other hand, is a full-fledged deep learning framework with more advanced features and flexibility, catering to researchers and professionals working on complex projects.
12 weeks, 26 lessons, 52 quizzes, classic Machine Learning for all
Pros of ML-For-Beginners
- More comprehensive curriculum covering a wide range of ML topics
- Includes hands-on projects and quizzes for practical learning
- Designed for beginners with clear explanations and step-by-step guidance
Cons of ML-For-Beginners
- Less focus on deep learning compared to handson-ml3
- May not cover advanced topics in as much depth
- Primarily uses Azure ML, which may limit exposure to other platforms
Code Comparison
ML-For-Beginners (using Azure ML):
from azureml.core import Workspace, Experiment, ScriptRunConfig
ws = Workspace.from_config()
experiment = Experiment(workspace=ws, name='my-experiment')
config = ScriptRunConfig(source_directory='.', script='train.py')
run = experiment.submit(config)
handson-ml3 (using TensorFlow):
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, validation_split=0.2)
Both repositories offer valuable resources for learning machine learning, but they cater to different audiences and use different tools. ML-For-Beginners provides a more structured curriculum for beginners, while handson-ml3 offers a deeper dive into various ML techniques, especially in deep learning.
The fastai deep learning library
Pros of fastai
- Offers a high-level API for quick and easy model development
- Includes built-in best practices and optimizations for various tasks
- Provides a comprehensive ecosystem with integrated libraries and tools
Cons of fastai
- Steeper learning curve for those new to deep learning concepts
- Less flexibility for fine-grained control over model architecture
- May abstract away some lower-level details, potentially limiting understanding
Code Comparison
fastai:
from fastai.vision.all import *
path = untar_data(URLs.PETS)/'images'
dls = ImageDataLoaders.from_folder(path, valid_pct=0.2, seed=42)
learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)
handson-ml3:
from sklearn.model_selection import train_test_split
from tensorflow import keras
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = keras.models.Sequential([
keras.layers.Dense(30, activation="relu", input_shape=X_train.shape[1:]),
keras.layers.Dense(1)
])
model.compile(loss="mse", optimizer=keras.optimizers.SGD(learning_rate=1e-3))
model.fit(X_train, y_train, epochs=20, validation_split=0.1)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Machine Learning Notebooks, 3rd edition
This project aims at teaching you the fundamentals of Machine Learning in python. It contains the example code and solutions to the exercises in the third edition of my O'Reilly book Hands-on Machine Learning with Scikit-Learn, Keras and TensorFlow (3rd edition):
Note: If you are looking for the second edition notebooks, check out ageron/handson-ml2. For the first edition, see ageron/handson-ml.
Quick Start
Want to play with these notebooks online without having to install anything?
â Colab provides a temporary environment: anything you do will be deleted after a while, so make sure you download any data you care about.
Just want to quickly look at some notebooks, without executing any code?
-
github.com's notebook viewer also works but it's not ideal: it's slower, the math equations are not always displayed correctly, and large notebooks often fail to open.
Want to run this project using a Docker image?
Read the Docker instructions.
Want to install this project on your own machine?
Start by installing Anaconda (or Miniconda), git, and if you have a TensorFlow-compatible GPU, install the GPU driver, as well as the appropriate version of CUDA and cuDNN (see TensorFlow's documentation for more details).
Next, clone this project by opening a terminal and typing the following commands (do not type the first $
signs on each line, they just indicate that these are terminal commands):
$ git clone https://github.com/ageron/handson-ml3.git
$ cd handson-ml3
Next, run the following commands:
$ conda env create -f environment.yml
$ conda activate homl3
$ python -m ipykernel install --user --name=python3
Finally, start Jupyter:
$ jupyter notebook
If you need further instructions, read the detailed installation instructions.
FAQ
Which Python version should I use?
I recommend Python 3.10. If you follow the installation instructions above, that's the version you will get. Any version â¥3.7 should work as well.
I'm getting an error when I call load_housing_data()
If you're getting an HTTP error, make sure you're running the exact same code as in the notebook (copy/paste it if needed). If the problem persists, please check your network configuration. If it's an SSL error, see the next question.
I'm getting an SSL error on MacOSX
You probably need to install the SSL certificates (see this StackOverflow question). If you downloaded Python from the official website, then run /Applications/Python\ 3.10/Install\ Certificates.command
in a terminal (change 3.10
to whatever version you installed). If you installed Python using MacPorts, run sudo port install curl-ca-bundle
in a terminal.
I've installed this project locally. How do I update it to the latest version?
See INSTALL.md
How do I update my Python libraries to the latest versions, when using Anaconda?
See INSTALL.md
Contributors
I would like to thank everyone who contributed to this project, either by providing useful feedback, filing issues or submitting Pull Requests. Special thanks go to Haesun Park and Ian Beauregard who reviewed every notebook and submitted many PRs, including help on some of the exercise solutions. Thanks as well to Steven Bunkley and Ziembla who created the docker
directory, and to github user SuperYorio who helped on some exercise solutions. Thanks a lot to Victor Khaustov who submitted plenty of excellent PRs, fixing many errors. And lastly, thanks to Google ML Developer Programs team who supported this work by providing Google Cloud Credit.
Top Related Projects
An Open Source Machine Learning Framework for Everyone
scikit-learn: machine learning in Python
Deep Learning for humans
Tensors and Dynamic neural networks in Python with strong GPU acceleration
12 weeks, 26 lessons, 52 quizzes, classic Machine Learning for all
The fastai deep learning library
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot