Start studying
Module 6 · Data Wrangling Module contents
Module 6  ·  Card 4 / 14intermediate

NumPy Arrays

A NumPy array is a grid of numbers that all share one type, stored side by side in one block of memory.

It can have one dimension like a list, two like a table, or more.

For example, a week of hourly temperatures for 10 cities fits in an array of shape 10 by 168.

The shape gives the size along each dimension, and every row must be the same length.

Because every element has the same type, NumPy can do maths on a whole array far faster than on a Python list.

Learn in depth ↓
Module 06 · Data Wrangling · 14 concepts
In depth  /  15 lines

NumPy Arrays, in more detail

NumPy is the Python library for arrays of numbers, and most data tools are built on it, pandas included.

Its core object is the ndarray, short for n-dimensional array.

A Python list stores references to separate objects, each carrying its own type information.

An array stores the raw numbers side by side, so a million 64-bit floats take about 8 megabytes.

Every array has one dtype, such as int64 or float64, that applies to every element.

Mixing types forces a common one, so an array made from 1 and 2.5 becomes an array of floats.

The shape is a tuple of sizes such as (10, 168), and reshaping changes it without copying data where possible.

Indexing works per dimension, so temps[0] is the first city and temps[:, 0] is the first hour for every city.

Slicing usually returns a view of the same memory rather than a copy.

Changing a view changes the original array, which surprises people who expected a copy.

Arrays have a fixed size, so appending a value builds a whole new array.

Growing an array inside a loop is therefore slow; collect the values first and convert once.

Arrays are poor at mixed data such as names next to prices, which is why tables use pandas instead.

Machine learning libraries accept NumPy arrays directly, so data usually reaches a model in this form.

A common slip is comparing two arrays with ==, which returns an array of True and False rather than one answer.

Want the full technical article? Wikipedia ↗
Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
An analyst doubles one million prices, once stored in a Python list and once in a NumPy array. Why is the array version much faster?
Data Wrangling

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close