Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: equivalent of numpy.roll but only for data visualisation

Is there a way to perform a roll on an array, but instead of having a copy of the data having just a different visualisation of it?

An example might clarify: given b a rolled version of a...

>>> a = np.random.randint(0, 10, (3, 3))
>>> a
array([[6, 7, 4],
       [5, 4, 8],
       [1, 3, 4]])
>>> b = np.roll(a, 1, axis=0)
>>> b
array([[1, 3, 4],
       [6, 7, 4],
       [5, 4, 8]])

...if I perform an assignment on array b...

>>> b[2,2] = 99
>>> b
array([[ 1,  3,  4],
       [ 6,  7,  4],
       [ 5,  4, 99]])

...the content of a won't change...

>>> a
array([[6, 7, 4],
       [5, 4, 8],
       [1, 3, 4]])

...contrarily, I would like to have:

>>> a
array([[6, 7, 4],
       [5, 4, 99],    # observe as `8` has been changed here too!
       [1, 3, 4]])

Thanks in advance for your time and expertise!

like image 209
mac Avatar asked Nov 08 '11 13:11

mac


People also ask

What is NumPy data visualization?

Numpy is a library for scientific computing in Python and also a basis for pandas. It provides a high-performance multidimensional array object and tools for working with these arrays. A numpy array is similar to the list. It is usually fixed in size and each element is of the same type.

How do I roll a NumPy array?

The numpy. roll() function rolls array elements along the specified axis. Basically what happens is that elements of the input array are being shifted. If an element is being rolled first to the last position, it is rolled back to the first position.

Is Matplotlib based on NumPy?

Matplotlib is a plotting library for Python. It is used along with NumPy to provide an environment that is an effective open source alternative for MatLab. It can also be used with graphics toolkits like PyQt and wxPython.

What is NumPy vectorization?

The concept of vectorized operations on NumPy allows the use of more optimal and pre-compiled functions and mathematical operations on NumPy array objects and data sequences. The Output and Operations will speed up when compared to simple non-vectorized operations.


1 Answers

This is not possible, sorry. The rolled array cannot be described by a different set of strides, which would be necessary for a NumPy view to work.

like image 102
Sven Marnach Avatar answered Oct 23 '22 10:10

Sven Marnach