← All questions
Python & DataMedium· NumPy Essentials

NumPy copy vs. view — what's the actual difference, and...

A view is a new array object pointing at the same underlying memory as the original — you get one from slicing, .reshape(), or .T. Modifying a view modifies the original, because there’s only one buffer of data underneath. A copy, from .copy() or from fancy/boolean indexing, owns its own memory entirely, so changes to it never touch the original.

The reason this matters in ML pipelines is a specific, quiet bug: slice a batch out of your training array, normalize it in place — batch /= 255.0 — and because that slice was a view, you’ve just corrupted the original training data. Every subsequent batch drawn from the same region gets normalized twice. The rule that avoids it: use views freely when you’re only reading (they’re free, no memory cost), and call .copy() the moment you’re about to modify a slice of data you need to keep intact elsewhere. arr.base is None tells you whether something is a copy.

Code playground (Python)