Skip to content

Scikit Learn - Introduction

This chapter introduces Scikit-learn (often abbreviated as sklearn), a cornerstone library for machine learning in Python. We will cover its purpose, origins, key features, prerequisites, and installation, providing a foundational understanding for new users.

Scikit-learn is an open-source machine learning library for Python. It provides a comprehensive suite of efficient tools for various machine learning and statistical modeling tasks, including classification, regression, clustering, dimensionality reduction, model selection, and preprocessing. Known for its clean, consistent API and extensive documentation, Scikit-learn is built upon other fundamental Python scientific libraries like NumPy (for numerical operations), SciPy (for scientific computing), and Matplotlib (for plotting, though Scikit-learn itself focuses on algorithms). Its ease of use makes it a popular choice for both beginners and experienced practitioners.

Scikit-learn originated as ‘scikits.learn,’ a Google Summer of Code project by David Cournapeau in 2007. The project gained significant momentum in 2010 when Fabian Pedregosa, Gael Varoquaux, Alexandre Gramfort, and Vincent Michel from INRIA (French Institute for Research in Computer Science and Automation) took the lead. They released the first public version (v0.1 beta) on February 1st, 2010.

Since its inception, Scikit-learn has undergone continuous development and improvement, with regular releases introducing new features, enhancements, and bug fixes. For detailed version history, please refer to the official Scikit-learn changelog on their website or GitHub repository.

Scikit-learn is a community-driven project, with contributions from a large and diverse group of developers, researchers, and users worldwide. The project is hosted on GitHub (https://github.com/scikit-learn/scikit-learn), where its development is managed. While a core team of maintainers guides the project, its success is a testament to the collective effort of its extensive community. Many organizations, from academic institutions to tech companies like Booking.com, JP Morgan, Evernote, Spotify, and Inria, utilize Scikit-learn in their work.

To use the latest versions of Scikit-learn effectively, you’ll generally need:

  • Python (typically version 3.8 or newer; check the official documentation for the exact version requirements for the Scikit-learn release you intend to use).
  • NumPy (recent stable version).
  • SciPy (recent stable version).
  • Joblib (recent stable version, for saving and loading models and parallel processing).
  • threadpoolctl (for managing thread pools in parallel processing).
  • Matplotlib (a recent stable version is recommended for Scikit-learn’s plotting capabilities and examples).
  • Pandas (a recent stable version is highly recommended for data manipulation and is used in many Scikit-learn examples).

It’s best practice to install these dependencies using a package manager like pip or conda, which usually handles version compatibility.

If you have Python and pip (Python’s package installer) set up, installing Scikit-learn is straightforward.

The most common way to install Scikit-learn is using pip:

pip install -U scikit-learn

The -U flag ensures that you upgrade to the latest stable version if Scikit-learn or its dependencies are already installed.

If you are using Anaconda or Miniconda, you can install Scikit-learn via the conda package manager:

conda install scikit-learn

Conda will handle the installation of Scikit-learn and its compatible dependencies. Popular Python distributions like Anaconda often come with Scikit-learn pre-installed or make it very easy to install.

For detailed installation instructions, including building from source or installing development versions, refer to the official Scikit-learn installation guide: https://scikit-learn.org/stable/install.html

Scikit-learn’s primary focus is on data modeling rather than data loading, manipulation, or summarization (which are typically handled by libraries like Pandas and NumPy). Its key features include a wide range of algorithms for:

  • Supervised Learning: Algorithms like Linear Regression, Logistic Regression, Support Vector Machines (SVM), Decision Trees, Naive Bayes, k-Nearest Neighbors, and ensemble methods (Random Forests, Gradient Boosting).
  • Unsupervised Learning: Algorithms for clustering (K-Means, DBSCAN, Hierarchical Clustering), dimensionality reduction (PCA, NMF), and density estimation.
  • Clustering: Grouping unlabeled data based on similarity.
  • Dimensionality Reduction: Reducing the number of features while preserving important information, useful for visualization, efficiency, and preventing the curse of dimensionality.
  • Model Selection: Tools for comparing, validating, and choosing models and parameters, including cross-validation, grid search, and metrics for evaluating model performance.
  • Preprocessing: Utilities for feature extraction, feature scaling, normalization, and handling missing values.
  • Ensemble Methods: Techniques that combine predictions from multiple models to improve performance and robustness (e.g., Random Forests, AdaBoost, Gradient Tree Boosting).
  • Feature Extraction: Converting raw data (like text or images) into numerical features suitable for machine learning.
  • Feature Selection: Identifying the most relevant features for building a model.
  • Open Source: Freely available under the BSD license, allowing for both academic and commercial use.

Scikit-learn’s consistent API (e.g., estimator.fit(X, y), estimator.predict(X), estimator.transform(X)) makes it easy to switch between different models and experiment rapidly.