score:0
It's not clear to me what's not working, but I think maybe you're trying to do something like this:
import numpy
class X(object):
def __init__(self, parent):
self.parent = parent
self.pid = [0, 1, 2]
@property
def values(self):
tmp = self.parent.P[self.pid]
return tmp
@values.setter
def values(self, input):
self.parent.P[self.pid] = input
class Node(object):
def __init__(self):
self.P = numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
self._values = X(self)
@property
def x(self):
return self._values.values
@x.setter
def x(self, input):
self._values.values = input
I hope that get's you started.
update
The reason that n.x[1:3] = [77, 88]
doesn't work using this approach is because n.x
and n.x[:] = ~
both call the get method of X.values which returns tmp
. But tmp
is a copy of part of P and after n.x[:] = ~
tmp
is thrown away and P
is not updated. tmp
is a copy because when you index an array with another array you get a copy not a view. Here is an example to make that more clear, you can read more about numpy slicing/indexing here.
>>> P = np.arange(10)
>>> pid = np.array([1, 2, 3])
>>> Q = P[pid]
>>> Q[:] = 99
>>> P
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> R = P[1:4]
>>> R[:] = 99
>>> P
array([ 0, 99, 99, 99, 4, 5, 6, 7, 8, 9])
>>> P[[1,2]][:] = 88
>>> P
array([ 0, 99, 99, 99, 4, 5, 6, 7, 8, 9])
setitem won't help, because you're calling the setitem method of the tmp
not X
.
The easiest way to make it work is to replace the pid array with a slice, but I know that's kind of limiting. You could also keep track of the tmp
array, have a self._tmp so you can move the values from _tmp to P later. I know neither of those are perfect, but maybe someone else here will come up with a better approach. Sorry I couldn't do more.
Credit To: stackoverflow.com
Related Query
- Setting a subsection or slice of a global numpy array through a python object
- Update 2d numpy array through a slice object
- Convert map object to numpy array in python 3
- Python enumerate through 2D array in numpy
- Error: Setting an array element with a sequence. Python / Numpy
- Send numpy array as bytes from python to JS through Flask
- Python NumPy - angled slice of 3D array
- specify dtype of each object in a python numpy array
- Setting numpy array to slice without any in-place operations
- How to convert Python dictionary object to numpy array
- Python Numpy Error: ValueError: setting an array element with a sequence
- What is a faster way to iterate through a numpy array in Python
- Read swig python 'double *' object into numpy (maybe through ctypes?)
- Python how to loop through an array in numpy at C speed and store some positions
- Iterating through python numpy array when used with scipy.optimize.curve_fit
- setting Numpy array elements with a class object
- Tracking Python 2.7.x object attributes at class level to quickly construct numpy array
- Convert a MATLAB matrix object to a python NumPy array
- Speed up nested for-loops in python / going through numpy array
- ValueError: Setting void-array with object members using buffer. Plotting a timeseries of an numpy array
- How can I slice 2D array in python without Numpy module?
- How to find slice object in numpy array
- Setting all values within a subsection of a 2D array to another value using NumPy
- Memory Error when trying to load an R object file into a python numpy array
- Python / Tensorflow / Numpy setting an array element with a sequence error
- Store multidimensional numpy array slice with newaxis to object
- Python numpy array of numpy arrays as object
- Different behavior between the normal python list and the numpy array object
- Python NumPy array: Setting an array element with a sequence
- Python numpy array integer indexed flat slice assignment
More Query from same tag
- how to manipulate array in array
- TypeError: only integer scalar arrays can be converted to a scalar - error in 2 basic arrays
- Using itertools in Python to generate sums while also keeping track of each element of the sum?
- Downsize 3D Matrix by Averaging in Numpy or alike
- re-arranging entries in 2d array based on certain columns
- numpy.unique vs collections.Counter performance question
- Create pandas Dataframe from list of objects
- Cross Entropy for batch with Theano
- How to convert all bytes files under the folder into images
- Conditional Cumulative Count pandas while preserving values before first change
- Python check if array row or column exist and extend array size if needed
- Python Negamax Algorithm
- Python plot median value
- sum elements of array
- How to convert c_int_array into python list?