简体   繁体   中英

Is there a way to reference the column/row number of a numpy matrix to perform operations? (of if not, a dataframe?)

I want to apply a function to all the elements/cells of a numpy matrix. It'll be a large one (100k*100k) so I want to do it in a fast way. I could populate it with a forloop, but it's estimated to take 43 days.

I essentially want to use the row number and column number of each element to perform a calculation. The final application has more moving parts than this, but to illustrate: the entry below where 'X' is, would be 2, because it is row 1 and column 1, (1+1). In the end instance I will be using the number of the row and column that is returned to search another dataframe for a value, and perform a calculation, but that should be easy once I understand the below.

If this isn't possible with a numpy matrix, I can do it with a df and then convert it to a numpy array. But I think that will be slower.

So below, I want each 'cell' to calculate something by reference to its row and column #

rows   c1   c2  c3

0      0    0    0
1      0    X    0
2      0    0    0

Thanks so much! x

POSTEDIT:

So I have a numpy array/matrix like this. The zeros are just placeholders, and the choice of which cell I refer to with the 'X' is arbitrary.

rows   c1   c2  c3

0      0    0    0
1      0    X    0
2      0    0    0

Then I have this dataframe as follows


    attribute 1
0   a
1   b
2   c
3   d

So for example I'd like to place an operation in each 'cell' whereby it looks up the dataframe based on arguments provided by its own position. Eg, this X has the position row 1 column 1, so its arguments are [1,1], so it looks up the index twice in the dataframe and returns the concatenation of the attribute, 'bb'. The main thing is that the formula in the 'cell' references its own position.

In the below array, the position marked 'y' would return 'bc', for example.

rows   c1   c2  c3

0      0    0    0
1      0    X    Y
2      0    0    0

I'm a newbie, hope I explained it ok

So I'm going to take a stab at this and say you're looking for something like this:

x = 5 # or whatever n by n size you want for your matrix

arr = np.arange(0, x, 1)
vec = arr[:, np.newaxis]

vec + vec.transpose()

this is going to yeild the following matrix:

array([0, 1, 2, 3, 4],
      [1, 2, 3, 4, 5],
      [2, 3, 4, 5, 6],
      [3, 4, 5, 6, 7],
      [4, 5, 6, 7, 8]])

which is the i+j index sum that you're looking for. If you want to start with 1 then you can just change the 0 in the np.arange .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM