Book Image

Python Data Analysis

By : Ivan Idris
Book Image

Python Data Analysis

By: Ivan Idris

Overview of this book

Table of Contents (22 chapters)
Python Data Analysis
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Key Concepts
Online Resources
Index

Creating array views and copies


In the example about ravel(), views were brought up. Views should not be confused with the construct of database views. Views in the NumPy universe are not read only and you don't have the possibility to protect the underlying information. It is crucial to know when we are handling a shared array view and when we have a replica of the array data. A slice of an array, for example, will produce a view. This entails that if you assign the slice to a variable and then alter the underlying array, the value of this variable will change. We will create an array from the famed Lena picture, and then create a view and alter it at the final stage. The Lena image array comes from a SciPy function.

  1. Create a copy of the Lena array:

    acopy = lena.copy()
  2. Create a view of the array:

    aview = lena.view()
  3. Set all the values in the view to 0 with a flat iterator:

    aview.flat = 0

The final outcome is that only one of the pictures depicts the model. The other ones are censored altogether...