如何解决Python 3.6-Scipy引发错误:NotImplementedError:不支持输入类型'<U32'
我想用lfilter
去噪数据。
我的代码:
data = []
with open('Accelerometer.csv',newline='') as csvfile:
spamreader = csv.reader(csvfile,delimiter=',',quotechar='|')
for row in spamreader:
data.append(row)
timeInMS = []
mx = []
my = []
mz = []
for rowIndex in range(0,len(data)):
print(rowIndex)
timeInMS.append(data[rowIndex][1])
mx.append(data[rowIndex][2])
my.append(data[rowIndex][3])
mz.append(data[rowIndex][4])
# Data for plotting
n = 15 # the larger n is,the smoother curve will be
b = [1.0 / n] * n
a = 1
fMx = lfilter(b,a,mx)
fMy = lfilter(b,my)
fMz = lfilter(b,mz)
# plotting the line 1 points
plt.plot(timeInMS,fMx,label="x")
# plotting the line 2 points
plt.plot(timeInMS,fMy,label="y")
# plotting the line 3 points
plt.plot(timeInMS,fMz,label="z")
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Changes on Accelerometer')
plt.legend()
plt.show() # display a figure.
Accelerometer.csv(1000行):
Timestamp,Milliseconds,X,Y,Z
2020-10-05 10:19:23,11,0.02633622,0.2633622,9.933543
2020-10-05 10:19:23,18,0.037110128,0.25498247,9.938332
2020-10-05 10:19:23,29,0.034715924,0.25258827,9.939528
2020-10-05 10:19:23,37,0.25139117,9.944317
2020-10-05 10:19:23,47,0.039504327,0.2621651,9.932345
2020-10-05 10:19:23,58,0.035913024,0.25378537,9.927557
2020-10-05 10:19:23,68,9.931149
2020-10-05 10:19:23,78,0.027533319,0.25857377,87,9.94312
...
...
...
错误是:
Traceback (most recent call last):
File "Accelerometer/main.py",line 32,in <module>
fMx = lfilter(b,mx)
File "Accelerometer\venv\lib\site-packages\scipy\signal\signaltools.py",line 1883,in lfilter
raise NotImplementedError("input type '%s' not supported" % dtype)
NotImplementedError: input type '<U32' not supported
解决方法
使用熊猫pd.read_csv()
读取文件确实有帮助。这将自动将数据放入方便的格式。然后,您可以直接使用mx = data['X']
。
在没有大熊猫的情况下,您会遇到诸如此处的问题,在这里您会忘记跳过标题,此外,您的数据全都是字符串格式而不是数字格式。还将日期和时间从字符串格式转换为实际的日期时间格式。
import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import lfilter
data = pd.read_csv('Accelerometer.csv')
n = 15 # the larger n is,the smoother curve will be
b = [1.0 / n] * n
a = 1
fMx = lfilter(b,a,data['X'])
fMy = lfilter(b,data['Y'])
fMz = lfilter(b,data['Z'])
# plotting the line 1 points
plt.plot(data['Milliseconds'],fMx,label="x")
# plotting the line 2 points
plt.plot(data['Milliseconds'],fMy,label="y")
# plotting the line 3 points
plt.plot(data['Milliseconds'],fMz,label="z")
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Changes on Accelerometer')
plt.legend()
plt.show() # Display a figure.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。