python - 我的 numpy 数组总是以零结尾?

标签 python math numpy

我想我错过了什么地方。我使用两个 for 循环(x 和 y)和一个基于 x,y 位置的函数填充了一个 numpy 数组。唯一的问题是无论数组的大小如何,数组的值总是以零结尾。

thetamap = numpy.zeros(36, dtype=float)
thetamap.shape = (6, 6)
for y in range(0,5):
    for x in range(0,5):
        thetamap[x][y] =  x+y
print thetamap

最佳答案

range(0, 5) 产生 0, 1, 2, 3, 4。端点总是被省略。您只需要 range(6)

更好的是,使用 NumPy 的强大功能 将数组放在一行中:

thetamap = np.arange(6) + np.arange(6)[:,None]

这会产生一个行向量和一个列向量,然后使用 NumPy 广播将它们加在一起形成一个矩阵。

关于python - 我的 numpy 数组总是以零结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15329557/

相关文章:

python - 如何让这个 Python 质数查找程序更加有用?

Python - 确定参数是否为整数

python - 使用来自 pandas DataFrame 的数据拟合 sklearn 的 SVM 分类器

java - 对于给定的一大组数据的操作,有没有办法确定数据是否可以分解为mapreduce操作?

math - 解释 Vinay Deolalikar 的证明 P != NP

python - Scrapy + 更改请求参数

javascript - 计算第二行的 Y1,它是第一行大小的一半

python - @cupy.fuse cupy python 装饰器在哪里记录?

python - Scikit 学习 : Randomized Logistic Regression gives ValueError: output array is read-only

python - 如何在二维 numpy 数组中查找行?