- Python Data Analysis(Second Edition)
- Armando Fandango
- 289字
- 2021-07-09 19:04:05
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 face picture in the SciPy package, and then create a view and alter it at the final stage:
- Get the face image:
face = scipy.misc.face()
- Create a copy of the face array:
acopy = face.copy()
- Create a view of the array:
aview = face.view()
- Set all the values in the view to
0
with aflat
iterator:aview.flat = 0
The final outcome is that only one of the pictures depicts the model. The other ones are censored altogether, as shown in the following screenshot:

Refer to the following code of this tutorial, which shows the behavior of array views and copies:
import scipy.misc import matplotlib.pyplot as plt face = scipy.misc.face() acopy = face.copy() aview = face.view() aview.flat = 0 plt.subplot(221) plt.imshow(face) plt.subplot(222) plt.imshow(acopy) plt.subplot(223) plt.imshow(aview) plt.show()
As you can see, by altering the view at the end of the program, we modified the original Lena array. This resulted in three blue (or black if you are reading the print version of this book) pictures. The copied array was unchanged. It is crucial to remember that views are not read-only.
- Testing with JUnit
- The Android Game Developer's Handbook
- Mastering JavaScript Object-Oriented Programming
- 實戰(zhàn)低代碼
- Elastic Stack應(yīng)用寶典
- Learning Apache Mahout Classification
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- Microsoft Dynamics AX 2012 R3 Financial Management
- Django 5企業(yè)級Web應(yīng)用開發(fā)實戰(zhàn)(視頻教學版)
- UML2面向?qū)ο蠓治雠c設(shè)計(第2版)
- C++程序設(shè)計教程
- C語言從入門到精通(視頻實戰(zhàn)版)
- 計算機軟件項目實訓指導
- Learning Node.js for Mobile Application Development
- PHP程序設(shè)計經(jīng)典300例