【发布时间】:2026-01-28 11:25:01
【问题描述】:
我正在尝试在 numpy 中构建一个矩阵。矩阵尺寸应为 (5001x7)。这是我的代码:
S=np.array([.0788,.0455,.0222,.0042,.0035,.0029,.0007])
#This is vector S, comprised of 7 scalars.
lamb=list(range(0,5001))
#This is a list of possible values for lambda, a parameter in my data.
M = np.empty([5001,7], order='C')
#This is the empty matrix which is to be filled in the iterations below.
for i in S:
for j in lamb:
np.append(M,((S[i]**2)/(lamb[j]+S[i]**2)))
我遇到的问题是 M 仍然是一个零向量矩阵。
重要细节: 1) 我将最后一行指定为:
M=np.append(M,((S[i]**2)/(lamb[j]+S[i]**2)))
然后我在一个一维数组中得到一个长度为 70,014 的值数组。我真的不知道该怎么做。
2) 我已经尝试在矩阵 M 的 'float' 和 'int' 之间切换 dtype 参数。
3) 我在运行代码时收到此警告: VisibleDeprecationWarning:使用非整数而不是整数将导致将来出错 app.launch_new_instance()
4) 我正在使用 Python 3.4
非常感谢您的帮助。谢谢!
【问题讨论】:
-
我不认为
np.append正在做你认为它正在做的事情。
标签: python arrays numpy matrix iteration