【问题标题】:output operand requires a reduction, but reduction is not enabled Python输出操作数需要归约,但没有启用归约 Python
【发布时间】:2013-02-21 09:22:21
【问题描述】:
import numpy as np
from numpy.linalg import solve,norm,cond,inv,pinv
import math
import matplotlib.pyplot as plt
from scipy.linalg import toeplitz
from numpy.random import rand

c = np.zeros(512)
c[0] = 2
c[1] = -1
a = c
A = toeplitz(c,a)

cond_A = cond(A,2)

# creating 10 random vectors 512 x 1
b = rand(10,512)

# making b into unit vector
for i in range (10):
    b[i]= b[i]/norm(b[i],2)

# creating 10 random del_b vectors 
del_b = [rand(10,512), rand(10,512), rand(10,512), rand(10,512), rand(10,512), rand(10,512), rand(10,512), rand(10,512), rand(10,512), rand(10,512)] 

# del_b = 10 sets of 10 vectors (512x1) whose norm is 0.01,0.02 ~0.1
for i in range(10):
    for j in range(10):
        del_b[i][j] = del_b[i][j]/(norm(del_b[i][j],2)/((float(j+1)/100)))

x_in = [np.zeros(512), np.zeros(512), np.zeros(512), np.zeros(512), np.zeros(512), np.zeros(512), np.zeros(512), np.zeros(512), np.zeros(512), np.zeros(512)]

x2 = np.zeros((10,10,512))
for i in range(10):
    x_in[i] =  A.transpose()*b[i]

for i in range(10):
    for j in range(10):
        x2[i][j] = ((A.transpose()*(b[i]+del_b[i][j]))

最后一行给了我错误。 (输出操作数需要归约,但没有启用归约) 我如何解决它? 我是 python 新手,如果有更简单的方法,请告诉我

谢谢

【问题讨论】:

  • 如果您可以添加导入语句(numpy as np、scipy#s toeplitz 等)以便代码复制、粘贴和按原样运行,将会有很大帮助。
  • 我刚刚包括在内。谢谢
  • 在引发错误的行中,左侧的形状为(512,),右侧的形状为(512, 512)。您正在尝试将 512x512 二维数组填充到 512 长的一维数组中。

标签: python arrays numpy scipy


【解决方案1】:

您看到的错误是因为您创建的内容的尺寸不匹配,但是您的代码在所有循环中效率也很低,并且没有最佳地利用 Numpy 的自动广播。我已经重写了代码来做你想做的事:

import numpy as np
from numpy.linalg import solve,norm,cond,inv,pinv
import math
import matplotlib.pyplot as plt
from scipy.linalg import toeplitz
from numpy.random import rand

# These should probably get more sensible names
Nvec = 10 # number of vectors in b
Nlevels = 11 # number of perturbation norm levels
Nd = 512 # dimension of the vector space

c = np.zeros(Nd)
c[0] = 2
c[1] = -1
a = c

# NOTE: I'm assuming you want A to be a matrix
A = np.asmatrix(toeplitz(c, a)) 

cond_A = cond(A,2)

# create Nvec random vectors Nd x 1
# Note: packing the vectors in the columns makes the next step easier
b = rand(Nd, Nvec)

# normalise each column of b to be a unit vector
b /= norm(b, axis=0)

# create Nlevels of Nd x Nvec random del_b vectors 
del_b = rand(Nd, Nvec, Nlevels)

# del_b = 10 sets of 10 vectors (512x1) whose norm is 0.01,0.02 ~0.1
targetnorms = np.linspace(0.01, 0.1, Nlevels)
# cause the norms in the Nlevels dimension to be equal to the target norms
del_b /= norm(del_b, axis=0)[None, :, :]/targetnorms[None, None, :]

# Straight linear transformation - make sure you actually want the transpose
x_in = A.T*b

# same linear transformation on perturbed versions of b
x2 = np.zeros((Nd, Nvec, Nlevels))
for i in range(Nlevels):
    x2[:, :, i] = A.T*(b + del_b[:, :, i])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    相关资源
    最近更新 更多