简体   繁体   English

有没有办法引用 numpy 矩阵的列/行号来执行操作? (如果没有,dataframe?)

[英]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.我想将 function 应用于 numpy 矩阵的所有元素/单元格。 It'll be a large one (100k*100k) so I want to do it in a fast way.它会很大(100k*100k),所以我想快速完成。 I could populate it with a forloop, but it's estimated to take 43 days.我可以用 forloop 填充它,但估计需要 43 天。

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).最终的应用程序有比这更多的移动部分,但为了说明:下面的条目“X”是 2,因为它是第 1 行和第 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.最后,我将使用返回的行数和列数来搜索另一个 dataframe 的值,并执行计算,但是一旦我理解了以下内容,这应该很容易。

If this isn't possible with a numpy matrix, I can do it with a df and then convert it to a numpy array.如果使用 numpy 矩阵无法做到这一点,我可以使用 df 执行此操作,然后将其转换为 numpy 数组。 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 X

POSTEDIT:后编辑:

So I have a numpy array/matrix like this.所以我有一个像这样的 numpy 数组/矩阵。 The zeros are just placeholders, and the choice of which cell I refer to with the 'X' is arbitrary.零只是占位符,我用“X”指代哪个单元格的选择是任意的。

rows   c1   c2  c3

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

Then I have this dataframe as follows然后我有这个dataframe如下


    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.因此,例如,我想在每个“单元格”中放置一个操作,从而根据其自己的 position 提供的 arguments 查找 dataframe。 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'.例如,这个 X 有 position 第 1 行第 1 列,所以它的 arguments 是 [1,1],所以它在 Z6A8064B5DF479455500553C47C55057'Dbb' 中查找索引两次并返回 concatenation 属性。 The main thing is that the formula in the 'cell' references its own position.最主要的是“单元格”中的公式引用了它自己的position。

In the below array, the position marked 'y' would return 'bc', for example.例如,在下面的数组中,标记为“y”的 position 将返回“bc”。

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.这是您要查找的i+j索引总和。 If you want to start with 1 then you can just change the 0 in the np.arange .如果您想从 1 开始,那么您只需更改np.arange中的0即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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