Skip to content

Scikit Learn - Data Representation

Machine learning models learn from data. To be understood by algorithms in Scikit-learn, data needs to be represented in a specific numerical format. This chapter discusses how Scikit-learn expects data to be structured.

The most common way to think about and represent data for Scikit-learn is as a table. This table is essentially a 2D grid where:

  • Rows represent individual observations or samples (e.g., a customer, a flower, a document).
  • Columns represent distinct characteristics or features of these samples (e.g., age of a customer, petal length of a flower, word count in a document).

Let’s consider the Iris dataset, a classic dataset in machine learning. We can load it using Scikit-learn’s datasets module and view it, often with the help of Pandas for a tabular display.

from sklearn.datasets import load_iris
import pandas as pd
# Load the Iris dataset object
iris_dataset = load_iris()
# Create a Pandas DataFrame for features
# iris_dataset.data contains the feature values
# iris_dataset.feature_names contains the names of the columns
X_df = pd.DataFrame(iris_dataset.data, columns=iris_dataset.feature_names)
# Create a Pandas Series for the target variable
# iris_dataset.target contains the target values (species encoded as 0, 1, 2)
# iris_dataset.target_names contains the names of the species
y_s = pd.Series(iris_dataset.target, name='species')
print("First 5 rows of the feature DataFrame (X_df):")
print(X_df.head())
print("\nFirst 5 values of the target Series (y_s):")
print(y_s.head())
print(f"\nTarget names: {list(iris_dataset.target_names)}")
First 5 rows of the feature DataFrame (X_df):
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
First 5 values of the target Series (y_s):
0 0
1 0
2 0
3 0
4 0
Name: species, dtype: int32
Target names: ['setosa', 'versicolor', 'virginica']

In this table, each row is a single observed flower (a sample), and each column represents a measurement taken for that flower (a feature).

Scikit-learn expects the features to be in a 2D array-like structure, commonly referred to as the feature matrix and conventionally denoted by X. This matrix has a shape of [n_samples, n_features]:

  • n_samples: The number of items or observations in your dataset (the number of rows).
  • n_features: The number of distinct characteristics used to describe each sample (the number of columns).

The feature matrix X is usually a NumPy array or a Pandas DataFrame. Scikit-learn algorithms are optimized to work with NumPy arrays. If you provide a Pandas DataFrame, Scikit-learn will typically convert it internally. All data in X must be numerical (integers or floating-point numbers). Categorical data (like text labels) must be converted to a numerical representation before being used in most Scikit-learn estimators (e.g., using one-hot encoding or label encoding).

Alongside the feature matrix X, most supervised learning algorithms in Scikit-learn require a target array, conventionally denoted by y. This array contains the values we want to predict.

  • The target array y is usually a 1D array (or Pandas Series) with length n_samples.
  • Each entry in y corresponds to a sample (row) in X.
  • For classification tasks, y contains discrete class labels (e.g., integers like 0, 1, 2, or strings like ‘cat’, ‘dog’). Scikit-learn often works best with integer labels.
  • For regression tasks, y contains continuous numerical values (e.g., house prices, temperature).

Unsupervised learning algorithms, like clustering or PCA, typically only require the feature matrix X and do not use a target array y during training.

Distinguishing Target Array from Feature Columns

Section titled “Distinguishing Target Array from Feature Columns”

The key difference is that the target array y represents the quantity we aim to predict (the dependent variable), while the columns in the feature matrix X are the inputs used for making that prediction (the independent variables).

Example: Separating Features and Target from Iris Data

Section titled “Example: Separating Features and Target from Iris Data”

If we want to predict the iris species based on its measurements, the species column becomes our target y, and the measurement columns form our feature matrix X.

from sklearn.datasets import load_iris
iris = load_iris()
# X: Feature matrix (measurements)
X_iris = iris.data
# y: Target array (species, encoded as integers)
y_iris = iris.target
print(f"Shape of feature matrix X_iris: {X_iris.shape}")
print(f"First 3 rows of X_iris:\n{X_iris[:3]}")
print(f"\nShape of target array y_iris: {y_iris.shape}")
print(f"First 10 values of y_iris: {y_iris[:10]}")
print(f"Unique values in y_iris: {pd.unique(y_iris)}")
print(f"Target names corresponding to unique values: {iris.target_names}")
Shape of feature matrix X_iris: (150, 4)
First 3 rows of X_iris:
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]]
Shape of target array y_iris: (150,)
First 10 values of y_iris: [0 0 0 0 0 0 0 0 0 0]
Unique values in y_iris: [0 1 2]
Target names corresponding to unique values: ['setosa' 'versicolor' 'virginica']

Ensuring your data is in this X (2D numerical array) and y (1D numerical array, for supervised tasks) format is the first step to using Scikit-learn effectively.