scikit-learn and PyTorch
Level 6 · Better models, real tools
The free Python tools most models are built with. scikit-learn for classic models on tables. PyTorch and TensorFlow for networks.
Course contentsLesson 18 of 18
Level 1 · How a machine learns
Level 2 · Getting the data ready
Level 3 · Kinds of learning
Level 4 · Classic models
Level 5 · Neural networks
Level 6 · Better models, real tools
After this lesson you can
- Pick a library for a task
- Read a short scikit-learn script
- Say where PyTorch and TensorFlow differ
12 mincovers 3 glossary entries
Worth reading first: Decision Trees and Forests
Which library to build a model with
Most machine learning is built with a few free Python libraries. scikit-learn holds the classic models: linear and logistic regression, trees, forests, k-means and more. PyTorch and TensorFlow are for neural networks. scikit-learn began in 2007 and first came out in 2010. TensorFlow came out in 2015, and PyTorch in 2017.
scikit-learn is built for tables of data that fit on one computer. Every model works the same way: you call fit to train it and predict to use it. Swapping a forest for a k-NN model is a one-line change. It also has the tools to split, scale and score data.
It does not train deep neural networks, and it does not use graphics chips. For a spreadsheet of sales or sign-ups, that rarely matters. A forest on 100,000 rows can train in seconds on a laptop. Boosting libraries like XGBoost follow the same fit and predict pattern.
PyTorch is for building and training neural networks. You write the network as normal Python code, layer by layer, and it works out the gradients for you. The same code runs on a laptop chip or a graphics card. Most AI research today uses it, and since 2022 it has been run by an independent foundation.
TensorFlow does the same job. Most people use it through a simple layer-stacking tool that comes with it. Its strength is shipping: tools to run a finished model on phones, in web pages and on servers. Many firms still run models built with it, though new research has mostly moved to PyTorch.
A rule of thumb covers most cases. For rows and columns, start with scikit-learn, and move to a boosting library if you need more. For photos, sound or text, use PyTorch. For a model that must run inside a phone app, check what each library offers for phones before you pick.
People often reach for PyTorch for a table of 5,000 rows. A neural network there needs more code, more tuning, and usually scores no better than a forest. The plainest model that does the job is easier to check and to keep running.
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
# X holds the features, y holds the labels
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier(n_estimators=300)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))The three libraries side by side
| Library | Built for | Typical use |
|---|---|---|
| scikit-learn | Classic models on tables | Forests, regression, clustering |
| PyTorch | Neural networks, research first | Photo, sound and text models |
| TensorFlow | Neural networks, shipping first | Models inside phones and web pages |
Which one to pick
- 1
Pick scikit-learn
When your data is a table that fits on one computer.
- 2
Pick PyTorch
When you build a neural network for photos, sound or text.
- 3
Pick TensorFlow
When the finished model must run inside a phone or a web page.
Worked example
Emma builds her first model
Emma has a table of 8,000 past loans with 15 columns each. She trains a scikit-learn random forest in 7 lines, and it takes 4 seconds.
She tries a PyTorch network on the same table. It takes a day to set up and scores 1 point lower, so the forest stays.
The words for this lesson3
Check yourself
Which tool suits a forest model on a sales table?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.