Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Guides › AI & Machine Learning

Machine Learning Explained with Python

Understand machine learning concepts through Python examples. Learn regression, classification, clustering, preprocessing, model evaluation, overfitting, and how to build your first predictive model.

Machine Learning Explained with Python

Introduction

Machine Learning (ML) enables computers to learn patterns from examples and use those patterns to make predictions or decisions. Instead of writing every rule manually, developers train a model using data.

This guide introduces the core concepts and demonstrates a complete basic workflow using Python and scikit-learn.

1. How Machine Learning Works

A typical ML workflow includes:

  1. Define a problem.
  2. Collect relevant data.
  3. Explore and clean the data.
  4. Select features and a target.
  5. Split data into training and testing sets.
  6. Train a model.
  7. Evaluate its predictions.
  8. Improve and deploy the model if appropriate.

2. Types of Machine Learning

Supervised Learning

Uses examples with known target values.

Unsupervised Learning

Finds structure in data without target labels.

Reinforcement Learning

An agent learns behavior through actions and feedback from an environment.

3. Install the Tools

Install Python, then run:

python -m pip install numpy pandas matplotlib scikit-learn

Use a virtual environment for each project when possible.

4. Features and Targets

Features are the input variables used by a model. The target is what the model learns to predict.

For a house-price model:

A feature should be available at the time a real prediction is made.

5. Your First Regression Model

This example learns a simple relationship between hours studied and a practice score.

from sklearn.linear_model import LinearRegression

X = [[1], [2], [3], [4], [5]]
y = [20, 35, 50, 65, 80]

model = LinearRegression()
model.fit(X, y)

prediction = model.predict([[6]])
print("Predicted score:", prediction[0])

The dataset is synthetic and intentionally simple. A real educational outcome cannot reliably be predicted from study hours alone.

6. Training and Testing

Training data is used to fit a model. Test data estimates performance on examples the model did not train on.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

For time-series forecasting, use a time-aware split instead of randomly mixing past and future observations.

7. Regression Algorithms

Common algorithms include:

Evaluate regression using MAE, MSE, RMSE, and R-squared where appropriate.

8. Classification Algorithms

Classification predicts a category.

Common algorithms:

Example: classify messages as spam or not spam using labeled examples.

9. Build a Classification Model

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

data = load_iris()
X = data.data
y = data.target

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)

predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))

This example uses a built-in educational dataset. The reported test result can vary with the split and model settings.

10. Unsupervised Learning with Clustering

Clustering groups similar observations.

K-Means is a common introductory algorithm. Feature scaling can be important because distance-based algorithms are sensitive to feature magnitudes.

Before interpreting clusters, check whether the groups are meaningful for the problem rather than assuming every cluster represents a real-world category.

11. Data Preprocessing

Common preprocessing steps include:

Use pipelines to apply preprocessing consistently. Fit transformations only on training data to avoid leakage.

12. Overfitting and Underfitting

Overfitting occurs when a model captures noise in training data and generalizes poorly. Underfitting occurs when a model cannot capture important patterns.

Possible solutions include:

13. Model Evaluation

For regression:

For classification:

Choose metrics based on the consequences of different errors.

14. Hyperparameters and Cross-Validation

Parameters are learned during training. Hyperparameters are configuration choices set before or around training, such as tree depth or regularization strength.

Cross-validation helps compare configurations using multiple data splits. Keep a final test set separate until the model-selection process is complete.

15. Feature Importance and Explainability

Some models provide feature-importance estimates. Other approaches include permutation importance and SHAP-style explanations.

Importance scores do not automatically prove that a feature causes an outcome. Consider correlated features, data quality, and the context of the model.

16. Save and Load a Model

For a trusted local model, scikit-learn supports serialization workflows. Only load model files from trusted sources because some serialization formats can execute code when loaded.

import joblib

joblib.dump(model, "model.joblib")
loaded_model = joblib.load("model.joblib")

Save the preprocessing pipeline and relevant dependency versions along with the model.

17. Beginner Practice Projects

  1. House Price Prediction.
  2. Student Score Prediction.
  3. Iris Flower Classification.
  4. Spam Message Detection.
  5. Customer Churn Prediction.
  6. Customer Segmentation.
  7. Sales Forecasting.
  8. Movie Recommendation Prototype.

18. Common Beginner Mistakes

19. Learning Checklist

Conclusion

Machine learning becomes easier when you build small models and inspect their predictions. Start with simple baselines, evaluate on unseen data, and improve your approach through experiments.

More guides