podqast
Machine Learning GuideOCDevelAll episodes

MLA 005 Shapes and Sizes: Tensors and NDArrays

27 min
Next episodeMLA 006 Salaries for Data Science & Machine Learning20 min
Get its new episodes on Latest. No account

Explains the fundamental differences between tensor dimensions, size, and shape, clarifying frequent misconceptions—such as the distinction between the number of features ("columns") and true data dimensions—while also demystifying reshaping operations like expand_dims, squeeze, and transpose in NumPy. Through practical examples from images and natural language processing, listeners learn how to manipulate tensors to match model requirements, including scenarios like adding dummy dimensions for grayscale images or reordering axes for sequence data.
Links • Notes and resources at ocdevel.com/mlg/mla-5

Definitions • Tensor: A general term for an array of any number of dimensions.

Tensor Properties Dimensions • Number of nested levels in the array (e.g., a matrix has two dimensions: rows and columns).

Size • Total number of elements in the tensor.

Shape • Tuple listing the number of elements per dimension.

Common Scenarios & Examples Data Structures in Practice • CSV/Spreadsheet Example: Dataset with 1 million housing examples and 50 features: • Shape: (1_000_000, 50)

Conceptual Clarifications • The term "dimensions" in data science often refers to features (columns), but technically in tensors it means the number of structural axes.

Reshaping and Manipulation in NumPy Reshaping Tensors • Adding Dimensions:

Axis Reordering • Transposing Axes: • Needed when model input or output expects axes in a different order (e.g., sequence length and embedding dimensions in NLP).

Practical Example • In NLP sequence models: • 3D tensor with (batch_size, sequence_length, embedding_dim) might need to be reordered to (batch_size, embedding_dim, sequence_length) for certain models.

Core NumPy Functions for Manipulation • reshape: General function for changing the shape of a tensor, including adding or removing di

More from Machine Learning Guide