【问题标题】:TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S64') dtype('S64') dtype('S64')TypeError: ufunc 'multiply' 不包含签名匹配类型 dtype('S64') dtype('S64') dtype('S64') 的循环
【发布时间】:2017-01-11 08:43:31
【问题描述】:

我正在尝试使用我的代码计算周期图

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

x = [line.rstrip('\n') for line in open('27000.dat')]
x = np.array(x)
fs=64

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

但我得到了

  File "m3.py", line 9, in <module>
    f, Pxx_den = signal.periodogram(x, fs)
  File "/home/milenko/miniconda2/lib/python2.7/site-packages/scipy/signal/spectral.py", line 141, in periodogram
    scaling, axis)
  File "/home/milenko/miniconda2/lib/python2.7/site-packages/scipy/signal/spectral.py", line 273, in welch
    return_onesided, scaling, axis)
  File "/home/milenko/miniconda2/lib/python2.7/site-packages/scipy/signal/spectral.py", line 391, in csd
    mode='psd')
  File "/home/milenko/miniconda2/lib/python2.7/site-packages/scipy/signal/spectral.py", line 824, in _spectral_helper
    scale = 1.0 / (fs * (win*win).sum())
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S64') dtype('S64') dtype('S64')

为什么? 我在 Ubuntu 16.04,Python 2.7.11 上。

【问题讨论】:

标签: python


【解决方案1】:

this question 一样,晦涩的错误代码来自对字符串数据而不是浮点数据运行 numpy 函数。致电rstripreturns a string。我不知道您的数据文件是什么样的,但在x = np.array(x) 之后添加x = x.astype(np.float) 应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2021-12-15
    • 2016-08-06
    • 2018-07-25
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多