File "d:\DK\vmd\白鲸优化VMD.py", line 176, in
white_whale_optimization(population_size, max_generations, data)
File "d:\DK\vmd\白鲸优化VMD.py", line 118, in white_whale_optimization
fitness_values[i], imf, res = fitness(population[i], data)
File "d:\DK\vmd\白鲸优化VMD.py", line 29, in fitness
imf, res, u_hat, omega = VMD(data, alpha, tau, K, DC, init, tol)
File "d:\DK\vmd\白鲸优化VMD.py", line 76, in VMD
u_hat_plus[k, n] = (f_hat_plus[n] - sum_uk - lambda_hat[k, n] / 2) / (1 + alpha_hat[k] * (freqs - omega_plus[k, n])**2 + epsilon)
TypeError: only length-1 arrays can be converted to Python scalars
PS C:\Users\ddkkd>
#def calculate_local_envelope_entropy(signal):
# 计算信号的振幅包络
analytic_signal = hilbert(signal)
amplitude_envelope = np.abs(analytic_signal)
# 计算局部包络熵
epsilon = 1e-10 # 避免由于零值导致的计算错误
normalized_envelope = amplitude_envelope / (np.mean(amplitude_envelope) + epsilon)
local_envelope_entropy = -np.sum(normalized_envelope * np.log2(normalized_envelope + epsilon))
return local_envelope_entropy
def fitness(pop, data):
np.random.seed(0)
K = int(pop[0])
alpha = int(pop[1])
tau = 0
DC = 0
init = 1
tol = 1e-7
imf, res, u_hat, omega = VMD(data, alpha, tau, K, DC, init, tol)
comp = np.vstack([imf, res.reshape(1, -1)])
SE = 0
se_imf = []
for i in range(comp.shape[0]):
temp = calculate_local_envelope_entropy(comp[i, :])
SE += temp
se_imf.append(temp)
fit = min(se_imf)
np.random.seed(int(time.time()))
return fit, imf, res
def VMD(data, alpha, tau, K, DC, init, tol):
# 数据长度
N = len(data)
epsilon = 1e-10 # 避免由于零值导致的计算错误
# 建立频域
freqs = np.fft.fftfreq(N)
# 初始化变量
u_hat_plus = np.zeros((K, N), dtype=np.complex128)
u = np.zeros((K, N))
omega_plus = np.zeros((K, N))
phi_plus = np.zeros((K, N), dtype=np.complex128)
alpha_hat = np.zeros(K)
alpha_plus = np.zeros((K, N))
h = np.zeros((K, N))
f_hat_plus = np.zeros(N, dtype=np.complex128)
lambda_hat = np.zeros((K, N), dtype=np.complex128)
for n in range(N):
f_hat_plus[n] = np.sum(u_hat_plus[:, n])
sum_uk = np.sum(u[:, n])
for k in range(K):
# 计算频谱中心
omega_plus[k, n] = np.dot(freqs[N//2:N], (abs(u_hat_plus[k, N//2:N])**2)) / (np.sum(abs(u_hat_plus[k, N//2:N])**2) + epsilon)
# 更新IMF分量
u_hat_plus[k, n] = (f_hat_plus[n] - sum_uk - lambda_hat[k, n] / 2) / (1 + alpha_hat[k] * (freqs[n] - omega_plus[k, n])**2 + epsilon)
phi_plus[k, :] = np.real(np.fft.ifft(u_hat_plus[k, :]))
# 计算包络函数
analytic_signal = hilbert(phi_plus[k, :])
amplitude_envelope = np.abs(analytic_signal)
# 更新包络函数权重
alpha_plus[k, :] = alpha * (amplitude_envelope**2) / (np.sum(amplitude_envelope**2) + tau)
# 更新拉格朗日乘子
lambda_hat[k, :] = lambda_hat[k, :] + alpha_plus[k, :] * (phi_plus[k, :] - u[k, :])
# 更新h函数
h[k, :] = lambda_hat[k, :]
# 更新IMF分量
u[k, :] = np.real(np.fft.ifft(h[k, :]))
# 计算残差
res = data - np.sum(u, axis=0)
# 计算频谱和中心频率
u_hat_plus = np.fft.fft(u, axis=1)
omega_hat = np.zeros((K, N))
for k in range(K):
omega_hat[k, :] = np.dot(freqs, (abs(u_hat_plus[k])**2)) / np.sum(abs(u_hat_plus[k])**2)
return u, res, u_hat_plus, omega_hat
根据提供的代码和错误信息,可以看出在VMD分解过程中出现了一个错误。具体来说,错误提示TypeError: only length-1 arrays can be converted to Python scalars表明在某一行中,只有长度为1的数组可以被转换为标量(scalar)。
根据你提供的代码,可能的原因是以下几个方面:
calculate_local_envelope_entropy函数的返回值是一个数组,而在计算temp的时候,可能需要将其转换为标量值。你可以检查calculate_local_envelope_entropy函数的实现,确保其返回值是一个标量。
fitness函数中的se_imf列表中的元素可能是数组而不是标量。可以尝试在计算fit之前,确保se_imf列表中的元素是标量值,可以使用np.mean等方法将其转换为标量。
检查其他相关函数的实现,确保在计算过程中没有使用数组而不是标量值。
你可以检查代码中涉及的函数(如calculate_local_envelope_entropy、VMD、fitness等)的实现,确保返回的值是标量而不是数组。这样就可以避免出现TypeError: only length-1 arrays can be converted to Python scalars错误。
根据提供的代码和错误信息,可以看出是在VMD函数的第76行出现了TypeError,提示只有长度为1的数组可以转换为Python标量。根据代码,这个错误可能是由于数组的维度或数据类型不匹配导致的。
具体来说,错误可能出现在以下几个地方:
在调用VMD函数时,输入的K值是一个浮点数,而VMD函数中需要的K值是一个整数。因此,需要将pop[0]强制转换为整数。
在VMD函数中,计算u_hat_plus[k, n]的公式中,分母部分包括了一个数组,可能是长度不为1的数组导致的错误。因此,需要检查数组的维度是否正确,或者将数组转换为标量数据类型。
在VMD函数中,计算alpha_hat[k] * (freqs - omega_plus[k, n])**2的公式中,freqs是一个长度为N的标准正态分布的数组,而omega_plus[k, n]是一个长度为1的标量。因此,需要将omega_plus[k, n]转换为一个长度为N的数组,可以使用np.full函数将其填充为相同的值。
下面是修改后的代码:
def fitness(pop, data):
np.random.seed(0)
K = int(pop[0])
alpha = int(pop[1])
tau = 0
DC = 0
init = 1
tol = 1e-7
imf, res, u_hat, omega = VMD(data, alpha, tau, K, DC, init, tol)
comp = np.vstack([imf, res.reshape(1, -1)])
SE = 0
se_imf = []
for i in range(comp.shape[0]):
temp = calculate_local_envelope_entropy(comp[i, :])
SE += temp
se_imf.append(temp)
fit = min(se_imf)
np.random.seed(int(time.time()))
return fit, imf, res
def VMD(data, alpha, tau, K, DC, init, tol):
# 数据长度
N = len(data)
epsilon = 1e-10 # 避免由于零值导致的计算错误
# 建立频域
freqs = np.fft.fftfreq(N)
# 初始化变量
u_hat_plus = np.zeros((K, N), dtype=np.complex128)
u = np.zeros((K, N))
omega_plus = np.zeros((K, N))
phi_plus = np.zeros((K, N), dtype=np.complex128)
alpha_hat = np.zeros(K)
alpha_plus = np.zeros((K, N))
h = np.zeros((K, N))
f_hat_plus = np.zeros(N, dtype=np.complex128)
lambda_hat = np.zeros((K, N), dtype=np.complex128)
# 进行VMD迭代
for n in range(N):
f_hat_plus[n] = np.sum(u_hat_plus[:, n])
sum_uk = np.sum(u[:, n])
for k in range(K):
# 计算频谱中心
omega_plus[k, n] = np.dot(freqs[N//2:N], (abs(u_hat_plus[k, N//2:N])**2)) / (np.sum(abs(u_hat_plus[k, N//2:N])**2) + epsilon)
# 更新IMF分量
u_hat_plus[k, n] = (f_hat_plus[n] - sum_uk - lambda_hat[k, n] / 2) / (1 + alpha_hat[k] * (freqs - np.full(N, omega_plus[k, n]))**2 + epsilon)
phi_plus[k, :] = np.real(np.fft.ifft(u_hat_plus[k, :]))
# 计算包络函数
analytic_signal = hilbert(phi_plus[k, :])
amplitude_envelope = np.abs(analytic_signal)
# 更新包络函数权重
alpha_plus[k, :] = alpha * (amplitude_envelope**2) / (np.sum(amplitude_envelope**2) + tau)
# 更新拉格朗日乘子
lambda_hat[k, :] = lambda_hat[k, :] + alpha_plus[k, :] * (phi_plus[k, :] - u[k, :])
# 更新h函数
h[k, :] = lambda_hat[k, :]
# 更新IMF分量
u[k, :] = np.real(np.fft.ifft(h[k, :]))
# 计算残差
res = data - np.sum(u, axis=0)
# 计算频谱和中心频率
u_hat_plus = np.fft.fft(u, axis=1)
omega_hat = np.zeros((K, N))
for k in range(K):
omega_hat[k, :] = np.dot(freqs, (abs(u_hat_plus[k])**2)) / np.sum(abs(u_hat_plus[k])**2)
return u, res, u_hat_plus, omega_hat
'''适应度函数,最小化各VMD分量的局部包络熵'''
def fitness(pop,data):
np.random.seed(0)
K = int(pop[0])
alpha = int(pop[1])
#print(K,alpha)
tau = 0
DC = 0
init = 1
tol = 1e-7
imf,res,u_hat,omega=VMD(data, alpha, tau, K, DC, init, tol)
comp=np.vstack([imf,res.reshape(1,-1)])
SE = 0
se_imf=[]
for i in range(comp.shape[0]):
temp= BaoLuoShang(comp[i,:])
SE +=temp
se_imf.append(temp)
# fit = SE
# fit = SE/K
fit = min(se_imf)
np.random.seed(int(time.time()))
return fit
VMD函数的第76行出现了TypeError,检查一下那里的代码,数据类型不正确
检查下你的代码,已经提示在76行,发生了一个错误::TypeError: only length-1 arrays can be converted to Python scalars
发生这个错误的原因是:此函数只能传一个值进去,而你传了一个数组进去
解决方法就是,转换下数据类型:
x.astype(int)或者np.vectorize(x)