Distinguish Views and Copies

Determine whether a selected or rearranged array shares storage, then copy deliberately before an independent mutation.

Selecting or rearranging an array can produce another array that looks independent while still referring to the same stored data. Before changing a derived array, we need to know whether that change can reach the source.

A Basic Slice Shares Stored Data

A basic slice is a view. It presents part of an array without making an independent copy of those entries:

[18.5 21.5 19. ]
True

sensor_0 is a one-dimensional view of the first sensor column. Mutating the view therefore changes the corresponding entry in readings:

[18.75 21.5  19.  ]
[[18.75 20.  ]
 [21.5  22.  ]
 [19.   24.  ]]

This behavior is useful when the intention is to edit the original table through a selected region. It is a bug when the selected series was meant to be an independent trial. The important question is not whether two variables have different names, but whether their arrays share memory.

Q1. Predict a change through a slice

After sensor_1 = readings[:, 1] and sensor_1[2] = 23.5, which source entry changes?

Choose one

Select one choice, then check.

HintTrace the selected column

readings[:, 1] contains source entries [0, 1], [1, 1], and [2, 1].

SolutionThe last entry of sensor 1 changes

sensor_1[2] refers to readings[2, 1], so that source entry becomes 23.5.

Not attempted
Review

Not marked done.

Transpose Shares; Boolean Selection Does Not

The transpose of a two-dimensional array is also a view:

(2, 3)
True

The view exchanges how the axes are presented, but it still refers to the source entries. A change through transposed can therefore change readings.

Boolean selection follows a different rule. It gathers matching values into a new array:

[22. 24.]
False

Changing high does not change the source. This is a copy created by boolean selection, not a view onto the matching positions.

reshape needs more care. NumPy can often return a view, but the relationship depends on the array's storage layout and the requested shape. A reshape is not a promise that storage is shared or independent. When the distinction matters, inspect it:

For this contiguous table, the result is True. Do not turn that observation into a rule for every possible reshape.

Q2. Identify the storage relationship

Which statement follows the rules established in this lesson?

Choose one

Select one choice, then check.

HintDo not infer from appearance

Recall which operation gathers matching values, and which operation may depend on storage layout.

SolutionUse rules plus evidence

Basic slices and transposes are views. Boolean selection returns a copy. Reshape may or may not share memory, so use np.shares_memory when the answer affects a mutation.

Not attempted
Review

Not marked done.

Copy When Independence Is Part of the Plan

Call .copy() when the derived array must be independent:

[18.75 21.5  19.  ]
[18.5 21.5 19. ]
False

The changed value appears only in independent_sensor. The explicit copy records an intention: experiment with this sensor series without changing the measurement table. If the intention were to correct the source table itself, the slice view would be the appropriate object to edit.

Q3. Edit with and without independence

Complete the program so source_edit changes the first sensor value in the source, while independent can change its next value without changing the source again.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintChoose a view, then an explicit copy

Use readings[:, 0] for the intended source edit. Start with the same slice and add .copy() for the independent trial.

SolutionMake independence visible in the code
Not attempted
Review

Not marked done.

Treat storage relationship as part of a mutation's meaning. Basic slices and transposes are views, boolean selection copies, and reshape may require evidence. Use np.shares_memory when the relationship matters, and use .copy() when independence is part of the intended computation.

References

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.