【问题标题】:Why am I geting this error? AttributeError: 'module' object has no attribute 'periodogram'为什么我会收到此错误? AttributeError:“模块”对象没有属性“周期图”
【发布时间】:2021-03-19 08:30:13
【问题描述】:

我复制了以下几行

from scipy import signal
import matplotlib.pyplot as plt
import numpy as np

fs = 10e3
N = 1e5
amp = 2*np.sqrt(2)
freq = 1234.0
noise_power = 0.001 * fs / 2
time = np.arange(N) / fs
x = amp*np.sin(2*np.pi*freq*time)
x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
# Compute and plot the power spectral density.

f, Pxx_den = signal.periodogram(x, fs)
plt.semilogy(f, Pxx_den)
plt.xlabel('frequency [Hz]')
plt.ylabel('PSD [V**2/Hz]')
plt.show()

来自 **http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.periodogram.html#scipy.signal.periodogram 但是当我尝试运行代码时出现此错误:

f, Pxx_den = signal.periodogram(x, fs)
AttributeError: 'module' object has no attribute 'periodogram'

我使用的是 Scipy 0.12 版 感谢您的帮助。 亲切的问候。 伊沃

【问题讨论】:

  • 在解释器中输入from scipy import signal,然后输入dir(signal),您是否在该列表中看到“周期图”?
  • print signal 会得到什么?
  • 当我打印 dir(signal) 时,我得到了很多方法,但没有周期图。如果我从 scipy 导入信号更改为从 scipy.signal 导入频谱,我将得到 lombscargle 方法。此方法是否已从 scipy v0.12 中删除?还是在另一个模块中?亲切的问候。Ivo
  • API 声明它应该在 .12 中......所以您可能遇到了某种安装问题。我从未使用过 scipy,所以我不确定它是否像传统的 python 模块一样安装,但也许检查 site-packages/scipy/signal ?
  • 是的,肯定是安装问题。我的目录(信号)肯定有周期图(我刚刚安装)

标签: python scipy


【解决方案1】:
>>>from scipy import signal

>>>print [x for x in dir(signal) if x == 'periodogram'] #just list comprehension to limit the amount of methods displayed
['periodogram']

你的 scipy 安装肯定有问题。我推荐http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy 这个站点通常是我在安装或导入您认为正确安装的模块时遇到困难时的首选。

网站上的东西列表并不是 100% 的东西,而是你可以在那里找到的大部分重要的东西。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 2020-01-08
    • 2022-08-18
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    相关资源
    最近更新 更多