如何使用行和列上的迭代在Python中填充矩阵 [英] How to fill a matrix in Python using iteration over rows and columns

查看:1183
本文介绍了如何使用行和列上的迭代在Python中填充矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个5个整数v的数组和10个整数的另一个数组v。
我有一个5乘10的矩阵P我想要填充这样(P)ij = v [i] + u [j]



我试过:

  P = np.empty((len(asset_grid),len(asset_grid)))
for i in range(asset_grid):
for j in range(asset_grid):
P [i,j] = asset_grid [i] + asset_grid [j]

但是它给了我一个错误

  TypeError:只有一个元素的整数数组可以转换为索引

我应该如何在Python中执行此操作。如果我的方法太天真,我很抱歉,我习惯了Matlab,现在慢慢学习Python。任何帮助表示赞赏。

解决方案

广播就是你想要做的。虽然对于像你这样的小型数组,它没有什么区别,但它与较大的数组产生显着的差异:

 >>> arr1 = np.arange(5)
>>> arr2 = np.arange(10,20)
>>> arr1 [:,None] + arr2
array([[10,11,12,13,14,15,16,17,18,19],
[11,12,13,14, 15,16,17,18,19,20],
[12,13,14,15,16,17,18,19,20,21],
[13,14,15, 16,17,18,19,20,21,22],
[14,15,16,17,18,19,20,21,22,23]])

通常使用 numpy 您希望避免遍历行和列并使用vectorized /广播业务。这是速度改进的实际来源。



所以,根据你的评论进行详细说明:




说P_ij是x的第i个元素,增加到4次幂减去y的第j个元素,增加到2次幂




通常,Python使用通常的Python运算符支持您希望以矢量化方式进行的大多数算术运算:

 >>> arr1 [:,无] ** 4  -  arr2 ** 2 
数组([[ - 100,-121,-144,-169,-196,-225,-256,-289,-324, - 361],
[-99,-120,-143,-168,-195,-224,-255,-288,-323,-360],
[-84,-105, -128,-153,-180,-209,-240,-273,-308,-345],
[-19,-40,-63,-88,-115,-144,-175 ,-208,-243,-280],
[156,135,112,87,60,31,0,-33,-68,-105]])


So I have an array of 5 integers v and another of 10 integers v. I have a 5 by 10 matrix P that I would want to fill so that (P)ij = v[i] + u[j]

I tried:

P = np.empty((len(asset_grid),len(asset_grid)))
for i in range(asset_grid):
    for j in range(asset_grid):
        P[i,j] = asset_grid[i] + asset_grid[j]

but it gives me an error

TypeError: only integer arrays with one element can be converted to an index

How should I be able to do this in Python. I apologize if my approach is too naive, I am used to Matlab and now slowly learning Python. Any help is appreciated.

解决方案

Broadcasting is what you want to do. Although for small arrays such as yours, it doesn't make a difference, it makes a significant difference with larger arrays:

>>> arr1 = np.arange(5)
>>> arr2 = np.arange(10,20)
>>> arr1[:,None] + arr2
array([[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
       [12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
       [13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
       [14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])

Generally with numpy you want to avoid iteration over rows and columns and use vectorized/broadcasted operations. This is where speed improvements actually come from.

So, elaborating based on your comment:

Say P_ij is ith element of x raised to the 4th power minus jth element of y raised to 2nd power

In general, Python supports most arithmetical operations you would want in a vectorized way, using the usual Python operators:

>>> arr1[:, None]**4 - arr2**2
array([[-100, -121, -144, -169, -196, -225, -256, -289, -324, -361],
       [ -99, -120, -143, -168, -195, -224, -255, -288, -323, -360],
       [ -84, -105, -128, -153, -180, -209, -240, -273, -308, -345],
       [ -19,  -40,  -63,  -88, -115, -144, -175, -208, -243, -280],
       [ 156,  135,  112,   87,   60,   31,    0,  -33,  -68, -105]])

这篇关于如何使用行和列上的迭代在Python中填充矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆