To train a machine learning model, you give a computer many examples and let it find patterns in them. It then uses those patterns to make predictions on new data. The basic steps are to define a problem, collect and prepare data, split it, pick an algorithm, train the model, and test the results.
You do not need a complex system to get started. Basic Python and the scikit-learn library are enough to build a simple model. This guide uses real Python code to train a model that estimates median home values for California census areas.
What Do You Need Before Training a Machine Learning Model?
You need a few tools and a clear goal before you start.
- Basic Python skills: You should know variables, lists, and functions.
- A dataset: This is the data the model learns from. Each row is one example, such as one neighborhood.
- A machine learning library: We will use scikit-learn, which has ready-made models and testing tools.
- A clear problem: Know what you want the model to predict. Here, it is the value of a home.
Python must be installed on your computer. Then install the two libraries with this command:
pip install scikit-learn pandas
Pandas helps you work with data tables. Scikit-learn provides the model and the tools to train and test it.
Step 1: Define the Problem
Start by deciding what you want the model to do. This choice tells you which type of task you have. Two common types of supervised machine learning tasks are regression and classification. This example uses supervised learning, where a model learns from data that includes the correct answers, also called targets or labels.
- Regression: Predicts a number, such as a home price, monthly sales, or delivery time.
- Classification: Predicts a label, such as spam or not spam, or fraud or not fraud.
Our example is regression because a home value is a number. Before you move on, write the goal in one sentence: Use California housing data to predict the median home value for a census area. A short goal like this keeps your data and your success measure focused.
Step 2: Collect a Dataset
A model can only learn from examples that fit your problem. For a home value model, useful data might include the number of bedrooms, the size of the home, its age, and its location. The details the model uses to make a guess are called features. The answer it tries to predict is called the target, or label.
You can find data on public data portals, government sites, and trusted dataset libraries. Always check where the data came from and what each column means. Then make sure it matches your goal. A rental price dataset, for example, would not help you predict sale prices.
This guide uses the California Housing dataset that comes with scikit-learn. It is based on the 1990 U.S. Census and contains 20,640 rows, with each row representing a census block group. Its features include median income, house age, and location. Its target is the median house value for that area, not the price of one home. The first time you load it, scikit-learn downloads the dataset, so you need an internet connection. It saves a local copy for later use.
Step 3: Prepare the Data
Raw data often has problems that hurt training. Check your columns and fix any issues before you build the model. Common tasks include these:
- Handle missing values: Fill them in or remove the rows when it makes sense. Never fill them with random numbers.
- Remove duplicates: Repeated rows give some examples too much weight.
- Encode text labels: Many models need numbers, so categories like property type must be converted to numeric form.
- Scale features: Scaling puts numbers on a similar range. It helps models such as support vector machines and K-nearest neighbors. Basic linear regression usually does not need it.
You do not need every task for every dataset. The California Housing features are all numeric and have no missing values, so we can use them as they are.
One rule matters a lot. Split your data first, and only then learn values for filling gaps or scaling. If you learn them from the full dataset, information from the test data leaks into training. That makes the model look better than it really is.
Step 4: Split the Dataset
Never train and test a model on the same data. A model can do well on examples it has already seen and still fail on new ones. So you divide the data into three groups.
| Dataset | Purpose |
|---|---|
| Training set | Teaches the model with examples |
| Validation set | Compares models and helps adjust settings |
| Test set | Checks the final model on data it has never used |
A common starting point is 70% for training, 15% for validation, and 15% for testing. These numbers are not fixed rules. The best split depends on how much data you have. For time-based data, such as sales or weather records, split the data by date. Train on earlier records and test on later ones so the model is checked against future data.
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
# Load the dataset
housing = fetch_california_housing(as_frame=True)
X = housing.data
y = housing.target
# Set aside 30% for validation and testing
X_train, X_temp, y_train, y_temp = train_test_split(
X, y, test_size=0.30, random_state=42
)
# Split the remaining data into validation and test sets
X_val, X_test, y_val, y_test = train_test_split(
X_temp, y_temp, test_size=0.50, random_state=42
)
Here, X holds the features and y holds the target values. The random_state setting makes the split repeatable, so you get the same groups each time you run the code. Keep the test set untouched while you build the model. Do not use it to pick features or settings.
Step 5: Choose a Machine Learning Algorithm
An algorithm is the method a computer uses to learn patterns. The right one depends on your task and your data. For a first project, a simple model is a good choice.
We will use linear regression. It predicts numbers, it works well with numeric features, and it is a common starting point for regression problems. It is also easier to inspect than many complex models. It finds the formula that best links the features to the target, then uses that formula to predict new values.
A complex algorithm is not always better. Start simple, then compare other options if the results are not good enough.
Step 6: Train the Model
In scikit-learn, the .fit() method trains the model. You pass it the training features and the training targets.
from sklearn.linear_model import LinearRegression
# Create the model
model = LinearRegression()
# Train it with the training data
model.fit(X_train, y_train)
While training, the model studies the examples. It adjusts its internal numbers to shrink the gap between its predictions and the true answers. For example, it learns how income, house age, and room counts relate to home value.
Training does not prove the model is good. It only means the model has learned from the training data. You still need to test it on data it has not seen.
Step 7: Evaluate Model Performance
Next, check how well the model predicts data it did not learn from. Use the validation set while you compare and improve models. Save the test set for the very end.
For regression, a helpful measure is mean absolute error (MAE). It is the average size of the model’s mistakes, whether the guess is too high or too low. A lower MAE means the predictions are closer to the real values.
from sklearn.metrics import mean_absolute_error
# Make predictions on validation data
val_predictions = model.predict(X_val)
# Calculate mean absolute error
val_mae = mean_absolute_error(y_val, val_predictions)
# Convert the result to dollars
print(f"Validation MAE: ${val_mae * 100000:,.0f}")
This dataset stores its target in units of $100,000. That is why the code multiplies the result by 100,000 to show dollars. No MAE score is good for every project. Judge it by the problem and by what a wrong prediction would cost.
Step 8: Improve the Model
If the first results are not useful enough, try changes and compare them on the validation set. Change one thing at a time so you know what helped. Here are some ways to improve a model.
- Review the features: Add useful details or remove ones that do not help.
- Improve data quality: Fix wrong values and check that the data fits the problem.
- Try another algorithm: Compare linear regression with a decision tree or a random forest.
- Tune hyperparameters: These are settings you choose before training. Compare each setup on validation data.
- Use cross-validation: This tests a model on several different training and validation splits. It gives a steadier picture when you have little data.
Do not tune the model until it only works on the training data. A good model should also work on new data. This ability is called generalization.
Final Check on the Test Set
Once you finish choosing and improving your model, run it on the test set one time.
# Make predictions on the test data
test_predictions = model.predict(X_test)
# Calculate final test error
test_mae = mean_absolute_error(y_test, test_predictions)
print(f"Test MAE: ${test_mae * 100000:,.0f}")
Treat this score as your final check. If you keep changing the model based on test results, the test set can no longer show how the model will do on new data. Classification tasks use other measures, such as accuracy, precision, recall, and F1 score.
Step 9: Save and Use the Model
After you are happy with the test results, save the model so you do not have to train it again. Scikit-learn models can be saved with joblib, which is installed along with scikit-learn.
import joblib
# Save the trained model
joblib.dump(model, "house_price_model.joblib")
# Load it later
loaded_model = joblib.load("house_price_model.joblib")
You can now use the loaded model on new data. The new data must have the same columns, in the same order, as the training data.
import pandas as pd
# Example input with the same feature columns
new_home = pd.DataFrame(
[[5.0, 25, 6, 1.2, 1000, 3, 34.05, -118.25]],
columns=X.columns
)
# Predict the value
prediction = loaded_model.predict(new_home)
# Convert from units of $100,000 to dollars
predicted_value = prediction[0] * 100000
print(f"Estimated area value: ${predicted_value:,.0f}")
This is example input, not a real appraisal. The model predicts the median value for a census area, so do not treat the output as the market value of one home. The dataset is based on 1990 U.S. Census data, so its home values do not reflect today’s housing market.
To use a model in a real app, you also need a way to send it new data and return predictions. You should check its results over time as well. If the data or the problem changes, retrain the model.
Common Mistakes Beginners Should Avoid
Many first projects fail for reasons that have little to do with the algorithm. Watch for these mistakes.
- Testing on training data: The score looks better than the model really is.
- Data leakage: Information from the test set or from the future slips into training. Scikit-learn explains this well in its guide to common pitfalls.
- Poor data: Wrong labels, missing values, and unrelated features hurt predictions.
- The wrong metric: MAE suits many regression tasks. Accuracy suits some classification tasks.
- Skipping the validation set: Without it, comparing models fairly is hard.
- Trusting one score: A single number can hide weaknesses. Look at the kinds of errors the model makes.
A good habit is to keep your training, validation, and test data separate from the start.
Frequently Asked Questions
Can I train a machine learning model without coding?
Yes. Some tools let you train models with visual interfaces. Learning basic Python still gives you more control over your data, model choices, and results.
How long does it take to train a machine learning model?
It depends on the data size, the algorithm, your computer, and the task. A small linear regression model can train in seconds. A large deep learning model can take much longer and may need powerful hardware.
Which programming language is best for machine learning?
Python is a popular choice. It has libraries such as scikit-learn, pandas, and PyTorch. It is a practical place for beginners to start.
How much data do I need to train a machine learning model?
There is no fixed amount. A simple problem may work with a small dataset, and a complex one may need far more examples. The data also has to be relevant and cover the cases the model will see.
Can I train a machine learning model on my laptop?
Yes. Most beginner projects run fine on a regular laptop. Large datasets and deep learning tasks may need more memory or a graphics processing unit (GPU).
Final Thoughts
Training a model starts with a clear problem and useful data. You prepare the data, split it into three sets, pick an algorithm, and train it. Then you measure the results on data the model has never seen.
A model that simply runs is not the goal. A model that predicts well on new data is. Start with a simple project, measure it honestly, and improve it one change at a time.
You Might Also Like:
Overfitting vs Underfitting in Machine Learning: Key Differences
I’m Emma Rose, the founder of Punstation. I cover technology, digital trends, business, and online tools, creating practical guides and thoughtful reviews to help readers understand their options and make informed decisions. For more on how I approach my content, see our Editorial Standards.