How to Use Slice Assignment in NumPy?

NumPy slice assignment allows you to use slicing on the left-hand side of an assignment operation to overwrite a specific subsequence of a NumPy array at once. The right side of the slice assignment operation provides the exact number of elements to replace the selected slice. For example, a[::2] = [...] would overwrite every other value of NumPy array a.

Here’s a minimal example of slice assignment:

>>> import numpy as np
>>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a[2:5] = [40, 41, 42]
>>> a
array([ 1,  2, 40, 41, 42,  6,  7,  8,  9])

The NumPy slice assignment operation doesn’t need the same shape on the left and right side because NumPy will use broadcasting to bring the array-like data structure providing the replacement data values into the same shape as the array to be overwritten.

The next example shows how to replace every other value of a 1D array with the same value. The left and right operands don’t have the same array shape—but NumPy figures it out through broadcasting.

>>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> a[::2] = 42
>>> a
array([42,  2, 42,  4, 42,  6, 42,  8, 42])

For 2D arrays, you can use the advanced slice notation—selection comma-separated by axis—to replace whole columns like so:

>>> a = np.array([[1, 2, 3],
	          [4, 5, 6]])
>>> a[:, 0] = 42
>>> a
array([[42,  2,  3],
       [42,  5,  6]])

Let’s dive into a practical example about NumPy slice assignments from my Python One-Liners book next. Take your time to study it and watch the explainer video to polish your NumPy slicing skills once and for all.

Practical Example Slice Assignment NumPy

Real-world data is seldomly clean: It may contain errors because of faulty sensor, or it may contain missing data because of damaged sensors. In this one-liner example, you learn about how to quickly handle smaller cleaning tasks in a single line of code.

Say, you have installed a temperature sensor in your garden to measure temperature data over a period of many weeks. Every Sunday, you uninstall the temperature sensor from the garden and take it in your house to digitize the sensor values. Now, you realize that the Sunday sensor values are faulty because they partially measured the temperature at your home and not at the outside location.

In this mini code project, you want to β€œclean” your data by replacing every Sunday sensor value with the average sensor value of the last seven days. But before we dive into the code, let’s explore the most important concepts you need as a basic understanding.

Examples Slice Assignment NumPy

In NumPy’s slice assignment feature, you specify the values to be replaced on the left-hand side of the equation and the values that replace them on the right-hand side of the equation.

Here is an example:

import numpy as np


a = np.array([4] * 16)
print(a)
# [4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4]

a[1::] = [16] * 15
print(a)
# [ 4 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16]

The code snippet creates an array containing 16 times the value 4. Then we use slice assignment to replace the 15 trailing sequence values with the value 16. Recall that the notation a[start:stop:step] selects the sequence starting at index β€œstart”, ending in index β€œstop” (exclusive), and considering only every β€œstep”-th sequence element. Thus, the notation a[1::] replaces all sequence elements but the first one.

import numpy as np


a = np.array([4] * 16)

a[1:8:2] = 16
print(a)
# [ 4 16  4 16  4 16  4 16  4  4  4  4  4  4  4  4]

This example shows how to use slice assignment with all parameters specified. An interesting twist is that we specify only a single value β€œ16” to replace the selected elements. Do you already know the name of this feature?

Correct, broadcasting is the name of the game! The right-hand side of the equation is automatically transformed into a NumPy array. The shape of this array is equal to the left-hand array.

Before we investigate how to solve the problem with a new one-liner, let me quickly explain the shape property of NumPy arrays. Every array has an associated shape attribute (a tuple). The i-th tuple value specifies the number of elements of the i-th axis. Hence, the number of tuple values is the dimensionality of the NumPy array.

Read over the following examples about the shapes of different arrays:

import numpy as np


a = np.array([1, 2, 3])
print(a)
"""
[1 2 3]
"""
print(a.shape)
# (3,)

b = np.array([[1, 2, 3],
              [4, 5, 6]])
print(b)
"""
[[1 2 3]
 [4 5 6]]
"""
print(b.shape)
# (2, 3)

c = np.array([[[1, 2, 3], [4, 5, 6]],
              [[7, 8, 9], [10, 11, 12]]])
print(c)
"""
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]
"""
print(c.shape)
# (2, 2, 3)

We create three arrays a, b, and c.

  • Array a is 1D, so the shape tuple has only a single element.
  • Array b is 2D, so the shape tuple has two elements.
  • Array c is 3D, so the shape tuple has three elements.

Problem Formulation

This is everything you need to know to solve the following problem:

Given an array of temperature values, replace every seventh temperature value with the average of the last seven days.

Solution

## Dependencies
import numpy as np

## Sensor data (M, T, W, T, F, Sa, Su)
tmp = np.array([1, 2, 3, 4, 3, 4, 4,
                5, 3, 3, 4, 3, 4, 6,
                6, 5, 5, 5, 4, 5, 5])


## One-liner
tmp[6::7] = np.average(tmp.reshape((-1,7)), axis=1)


## Result
print(tmp)

Take a guess: what’s the output of this code snippet?

First, the puzzle creates the data matrix β€œtmp” with a one-dimensional sequence of sensor values. In every line, we define all seven sensor values for seven days of the week (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday).

Second, we use slice assignment to replace all the Sunday values of this array. As Sunday is the seventh day of the week, the expression β€œtmp[6::7]” selects the respective Sunday values starting from the seventh element in the sequence (again: the Sunday sensor value).

Third, we reshape the one-dimensional sensor array into a two-dimensional array with seven columns. This makes it easier for us to calculate the weekly average temperature value to replace the Sunday data. Note that the dummy shape tuple value -1 (in β€œtmp.reshape((-1,7))”) means that the number of rows (axis 0) should be selected automatically. In our case, it results in the following array after reshaping:

print(tmp.reshape((-1,7)))
"""
[[1 2 3 4 3 4 4]
 [5 3 3 4 3 4 6]
 [6 5 5 5 4 5 5]]
"""

It’s one row per week and one column per weekday.

Now we calculate the 7-day average by collapsing every row into a single average value using the np.average() function with the axis argument: axis=1 means that the second axis is collapsed into a single average value. This is the result of the right-hand side of the equation:

print(np.average(tmp.reshape((-1,7)), axis=1))
# [3. 4. 5.]

After replacing all Sunday sensor values, we get the following final result of the one-liner:

# [1 2 3 4 3 4 3 5 3
3 4 3 4 4 6 5 5 5 4 5 5]

This example is drawn from my Python One-Liners book:

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!

Where to Go From Here?

Do you love data science? But you struggle to get everything together and develop a good intuition about the NumPy library?

To help you improving your code understanding speed in NumPy, I co-authored a brand-new NumPy book based on puzzle-based learning. Puzzle-based learning is a new, very practical approach to learning to code — based on my experience of teaching more than 60,000 ambitious Python coders online.

Get your “Coffee Break NumPy” now!