Python 使用线空间和平滑
我在运行平滑线条的函数时遇到问题。我有三个独立的变量,我从我的csv文件导入,需要平滑线。使用指数平滑可以从信号中尽可能多地去除随机和周期性噪声,而不会丢失基础信号的重要特征。为平滑每个单独的信号选择单独的时间常数。以图形方式显示结果 以下是我的一些代码:Python 使用线空间和平滑,python,numpy,smoothing,Python,Numpy,Smoothing,我在运行平滑线条的函数时遇到问题。我有三个独立的变量,我从我的csv文件导入,需要平滑线。使用指数平滑可以从信号中尽可能多地去除随机和周期性噪声,而不会丢失基础信号的重要特征。为平滑每个单独的信号选择单独的时间常数。以图形方式显示结果 以下是我的一些代码: from numpy import * from scipy.stats import norm import csv from matplotl
from numpy import *
from scipy.stats import norm
import csv
from matplotlib.pyplot import *
from IPython.core.pylabtools import figsize
#figsize(14, 7)
myID = 20155265
fn = str(myID)+".csv"
fn
data = np.genfromtxt("20155265.csv", delimiter=',', skip_header = 1)
smth1 = data[:,1]
smth2 = data[:,2]
smth3 = data[:,3]
def expSmooth(t,T,tau): # returns a smoothed version of T with time constant tau
tau = 0.01
t = np.linspace(0, 1, 10540, 2) # time of measurement
T = smth1
Ts = T.copy() # another vector for the smoothed data
for i in range(1,len(Ts)): # here's where the actual smoothing happens
dt = t[i]-t[i-1]
w = min(dt/tau,1)
Ts[i] = Ts[i-1] * (1-w) + T[i] * w
return Ts # return the smoothed data
plot(t,T)
plot(t,Ts, label = 'Smoothed Data')
xlabel('X-axis')
ylabel('Y-axis')
legend()
我在运行时也会出现以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-97b29b6d5ee7> in <module>
16 return Ts # return the smoothed data
17
---> 18 plot(t,T)
19 plot(t,Ts, label = 'Smoothed Data')
20 xlabel('X-axis')
~\anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
2759 @docstring.copy(Axes.plot)
2760 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2761 return gca().plot(
2762 *args, scalex=scalex, scaley=scaley, **({"data": data} if data
2763 is not None else {}), **kwargs)
~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
1645 """
1646 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1647 lines = [*self._get_lines(*args, data=data, **kwargs)]
1648 for line in lines:
1649 self.add_line(line)
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in __call__(self, *args, **kwargs)
214 this += args[0],
215 args = args[1:]
--> 216 yield from self._plot_args(this, kwargs)
217
218 def get_next_color(self):
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
340
341 if x.shape[0] != y.shape[0]:
--> 342 raise ValueError(f"x and y must have same first dimension, but "
343 f"have shapes {x.shape} and {y.shape}")
344 if x.ndim > 2 or y.ndim > 2:
ValueError: x and y must have same first dimension, but have shapes (10000,) and (10540, 2)
0.000000000000000000e+00,1.987649568467855232e+01,1.502770732591550562e+01,9.796532651620051269e+00
3.000284656988328846e-04,2.032588413582576692e+01,1.481391678788685518e+01,9.838773733555921552e+00
6.000569313976657692e-04,2.013795291324559145e+01,1.488696952239413029e+01,1.019210385573444100e+01
9.000853970964985996e-04,1.964606529847883110e+01,1.509763246836727113e+01,9.999835670287511746e+00
1.200113862795331538e-03,1.958979211177121371e+01,1.504942038590157338e+01,1.001807556622187967e+01
1.500142328494164477e-03,2.013860595459548009e+01,1.491842308020370567e+01,1.002097110595789431e+01
1.800170794192997199e-03,1.998167239030997777e+01,1.499645533912064010e+01,9.980793149246499851e+00
2.100199259891830355e-03,1.969884339061692202e+01,1.513994939974103282e+01,1.009387635287656337e+01
2.400227725590663077e-03,2.034433013515393185e+01,1.499177629776954745e+01,1.015006458706236359e+01
2.700256191289495799e-03,2.020764923596562568e+01,1.506159825939071339e+01,9.951152223719963885e+00
3.000284656988328955e-03,2.018453199942792864e+01,1.502803223857966231e+01,9.954336954252600123e+00
3.300313122687161677e-03,1.986644455611485682e+01,1.500975760158698868e+01,1.011627342579141775e+01
3.600341588385994399e-03,2.037481855217002291e+01,1.480681519737734853e+01,1.010520076861238259e+01
3.900370054084827554e-03,2.034613903815154146e+01,1.512797772833010335e+01,9.928197322515870837e+00
4.200398519783660710e-03,2.029763103912693012e+01,1.490699641094429140e+01,9.878651383161761501e+00
4.500426985482493432e-03,2.023957114424099757e+01,1.501257438050702397e+01,1.007540085537949359e+01
4.800455451181326154e-03,2.079433775809863860e+01,1.521317735549064665e+01,9.950279667926240634e+00
5.100483916880158876e-03,2.052803575990732554e+01,1.509758248396236979e+01,9.891986590075967811e+00
5.400512382578991598e-03,2.015682499321863119e+01,1.508489022657818523e+01,9.955164448148114076e+00
5.700540848277825187e-03,2.011819909355674696e+01,1.501075380973703943e+01,1.015537374721878905e+01
6.000569313976657909e-03,2.010649295602275188e+01,1.505906652977147964e+01,9.939615832465094059e+00
6.300597779675490631e-03,2.029240277512916890e+01,1.528788294973895390e+01,9.931050203825693146e+00
6.600626245374323353e-03,2.018988672364908155e+01,1.514912941994663598e+01,1.001967172353933222e+01
6.900654711073156075e-03,2.022259285851694699e+01,1.535320632721861323e+01,1.003319994573799079e+01
7.200683176771988797e-03,1.943010811933178061e+01,1.527350809725071734e+01,1.001153381953393051e+01
7.500711642470822386e-03,1.995908124651828430e+01,1.543542279981245002e+01,1.002018300551928753e+01
7.800740108169655108e-03,1.983987933411555460e+01,1.516848407834020485e+01,1.010803502461462067e+01```