Gyan Veda
Gyan Veda

Reputation: 6599

Setting new property in Numpy array

As you read through this question, you will see I am new to Python and NumPy, so please excuse any incorrect terminology.

I am currently working with two NumPy arrays, let's call them x and y.

x = numpy.array(0)
y = numpy.array(0)

I am operating on these arrays with functions that have these arrays as input and output.

x, y = function1(x, y)
x, y = function2(x, y)
...

This seems inelegant because if I could just make them parts of the same object, then I would only have a single input and output to these functions. I just want to make my code simpler, if possible.

x = function1(x)
x = function2(x)
...

As it turns out, y describes the data in x (not in this toy example, but with my actual arrays) so I figured I would set y as a property of x. My native language is MATLAB and I figured I could use syntax similar to creating struct hierarchies like so.

x.y = y

This doesn't work and, unfortunately, I don't know enough Python terminology to know what to look for online to find a solution to my problem.

Please let me know if you have any suggestions and/or if I can provide any clarification on this problem.

Upvotes: 1

Views: 289

Answers (2)

M4rtini
M4rtini

Reputation: 13549

You could create a simple class holding both arrays like this.

class Xy(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y 

import numpy as np 

x = np.array(range(10))
y = np.array(range(10))

xy = Xy(x,y)

def square(xy):
    x = xy.x 
    y = xy.y

    x **= 2
    y **= 2

    return Xy(x,y)

Upvotes: 1

a p
a p

Reputation: 3208

I assume that the error you're getting is along the lines of 'array' object has no attribute 'y'.

In this situation, if you really just want to bundle these things together, I'd recommend the use of a tuple. Here's some examples of how they work for your use case:

def myFunc1(x, y): 
    # do stuff with x and y
    return ??? # x or y or... both, somehow? 

def myFunc2(xy_bundle): # where xy_bundle is x and y packed into a tuple! 
    x, y = xy_bundle    # or: x = xy_bundle[0]; y = xy_bundle[1];
    # do stuff with x and y
    return (x, y)       # syntax for creating a tuple inline

You can use tup[x] notation to access individual members of a tuple, just like a list or an array, or you can implicitly unpack them, as in myFunc2. Tuples are a pretty standard tool for moving multiple things through functions, though if you find yourself using them a lot, you might have a case for a custom class or something.

As has been said in the comments, it's not clear what your motivation for wanting to make an array a property of another array, but it seems like a bad idea to me. If you have arrays that are dependent on one another, you could definitely subclass pandas' array and add a property to it. Seems overly complex, though.

Edit: calling these things! Your code probably looks like:

new_x = do_thing_with_array(x)
new_y = do_thing_with_array(y)

This would generally change to:

arrs = (x, y)
new_x, new_y = do_thing_with_arrays(arrs)

Hope that's what you were looking for.

Upvotes: 1

Related Questions