Teach your programs to learn from data
Train, evaluate, and understand machine learning and AI models in Python
Real Python takes you from your first regression model to neural networks, computer vision, and natural language processing with scikit-learn, NumPy, PyTorch, and TensorFlow. Learn how the algorithms work, not just which function to call.
import numpy as np from sklearn.linear_model import LinearRegression x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1)) y = np.array([5, 20, 14, 32, 22, 38]) model = LinearRegression().fit(x, y)
model.intercept_, model.coef_, model.score(x, y)
(5.633333333333337, array([0.54]), 0.7158756137479542)
plt.scatter(x, y); plt.plot(x, model.predict(x))
- 20+machine learning tutorials
- 9video courses
- 10+interactive quizzes
- 10coding exercises
- 55+resources in the ML learning path
From first model to deep learning, step by step
Machine learning is easier to trust when you understand what’s happening underneath. Real Python builds your intuition first, then shows you the libraries professionals use.
Prepare and evaluate
Split your data into training and test sets, and measure how well a model really performs on data it hasn’t seen.
Train classic models
Fit linear and logistic regression, classify with k-nearest neighbors, and find groups with k-means clustering.
Build neural networks
Write a neural network from scratch to see how it learns, then move up to deep learning frameworks.
Work with images, text, and audio
Detect and recognize faces, classify sentiment in text, and turn speech and audio into model-ready data.
A clear path into machine learning
Machine learning builds on working with data and a bit of math. Follow these learning paths in order, or jump in where you’re ready. Each one mixes tutorials, video courses, quizzes, and exercises, and tracks your progress as you go.
-
Data Science With Python Core Skills
Load, clean, and explore data with pandas, NumPy, and Matplotlib. If you can already do this comfortably, skip ahead.
-
1
Math for Data Science
Build the foundations models rely on: statistics, correlation, linear and logistic regression, and gradient descent, all explained with Python code.
-
2
Machine Learning With Python
Go from machine learning principles and your first neural network to computer vision, natural language processing, and algorithms like kNN, k-means, and GANs.
Need a refresher on working with data first? See Data Science & Analytics. Want to build apps on top of large language models? Continue with Building AI Apps & Agents.
Build a linear regression model
This is a real exercise from the Starting With Linear Regression in Python video course, using the same data as the notebook above. Write your function in the editor and click Run Tests.
Running tests, hints, and solutions are included with a Real Python membership. Not a member yet? You can still read the task and write your solution before you join.
Read it, watch it, test it, practice it
Every machine learning topic comes in the formats that help it stick. Mix them however you like.
Tutorials
In-depth guides that explain the ideas behind each algorithm and walk you through working code.
Browse tutorials →Video courses
Watch an instructor build models step by step, from linear regression to your own neural network.
Build a neural network →Quizzes
Check what you’ve learned in a few minutes and see exactly which concepts to review.
Test your ML knowledge →Coding exercises
Fit scikit-learn models and write a neural network’s forward pass and training step in your browser.
How exercises work →Confused by a shape error? Ask right where you are
Machine learning code fails in ways that can feel cryptic: an “Expected 2D array” error, a model that scores perfectly in training and poorly on new data, or a loss that won’t go down.
Mentor AI sees the tutorial, lesson, or exercise you’re on and your code, and nudges you toward the fix one hint at a time, so you understand what your model is actually doing.
Meet Mentor AI →ValueError: Expected 2D array, got 1D array instead when I call model.fit(x, y). But x is just a list of numbers. What’s wrong?Good instinct to look at x. scikit-learn expects the inputs as a table: one row per observation and one column per feature, even when there’s only one feature.
What shape does x have right now, and what shape would a table with a single column have?
(6,). So I need (6, 1), like x.reshape((-1, 1))?Exactly. The -1 lets NumPy work out the number of rows for you. Run the tests again. What does model.coef_ tell you about the line you just fit?
Learn the libraries behind real ML projects
From the foundations every model builds on to specialized tools for images, text, and audio.
Recently added
The Real Python team keeps adding machine learning tutorials, quizzes, and hands-on exercises, so you can practice what you read.
See all machine learning content →- TutorialBuild Your Own Face Recognition Tool With Python
- QuizBuild Your Own Face Recognition Tool With Python
- ExercisesBuilding a Neural Network & Making Predictions With Python AI
- ExercisesStarting With Linear Regression in Python
- TutorialUse TorchAudio to Prepare Audio Data for Deep Learning
- TutorialSplit Your Dataset With scikit-learn’s train_test_split()
- QuizMachine Learning With Python
Questions and answers
What should I know before I start?
You should be comfortable with Python basics and with loading and exploring data in pandas and NumPy. If you’re not there yet, start with Data Science & Analytics, or with the Python Basics learning path if you’re new to Python.
How much math do I need?
Less than you might think to get started, since libraries handle the heavy lifting. Understanding a few ideas like regression and gradient descent helps you choose and debug models, and the Math for Data Science path explains them with Python code rather than proofs.
Should I learn PyTorch or TensorFlow?
Start with scikit-learn for classic machine learning. When you’re ready for deep learning, both frameworks are good choices. PyTorch vs TensorFlow compares them so you can pick the one that fits your project.
How is this different from building AI apps?
Machine learning is about training and evaluating your own models on your own data. Building AI apps and agents focuses on using existing large language models through APIs and frameworks. If that’s your goal, see Building AI Apps & Agents.
What’s included in a membership?
Every Real Python membership includes all video courses, quizzes, coding exercises, and learning paths for machine learning and every other topic, along with Mentor AI as it rolls out.
Start building models that learn
Get your free learning plan and follow the full machine learning roadmap, with courses, quizzes, coding exercises, and a mentor at your side.