Python 3.x 当我运行这个for循环时,为什么我的笔记本会崩溃?修复方法是什么?
我已经使用了与卡尔曼滤波器相关的代码,并尝试遍历每一列数据。我希望发生的是:Python 3.x 当我运行这个for循环时,为什么我的笔记本会崩溃?修复方法是什么?,python-3.x,numpy,for-loop,matplotlib,jupyter-notebook,Python 3.x,Numpy,For Loop,Matplotlib,Jupyter Notebook,我已经使用了与卡尔曼滤波器相关的代码,并尝试遍历每一列数据。我希望发生的是: 列数据被送入过滤器 过滤列数据(xhat)被放入另一个数据帧(过滤) 过滤后的列数据(xhat)用于生成可视数据 我已经创建了一个for循环来迭代列数据,但是当我运行单元格时,我会使笔记本崩溃。当它没有崩溃时,我得到以下警告: C:\Users\perso\Anaconda3\envs\learn-env\lib\site-packages\ipykernel_launcher.py:45: RuntimeWarnin
C:\Users\perso\Anaconda3\envs\learn-env\lib\site-packages\ipykernel_launcher.py:45: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
提前感谢您的帮助。我希望这个问题足够详细。我对最后一个进行了轰炸
'''A Python implementation of the example given in pages 11-15 of "An
Introduction to the Kalman Filter" by Greg Welch and Gary Bishop,
University of North Carolina at Chapel Hill, Department of Computer
Science, TR 95-041,
https://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf'''
# by Andrew D. Straw
import numpy as np
import matplotlib.pyplot as plt
# dataframe created to hold filtered data
filtered = pd.DataFrame()
# intial parameters
for column in data:
n_iter = len(data.index) #number of iterations equal to sample numbers
sz = (n_iter,) # size of array
z = data[column] # observations
Q = 1e-5 # process variance
# allocate space for arrays
xhat=np.zeros(sz) # a posteri estimate of x
P=np.zeros(sz) # a posteri error estimate
xhatminus=np.zeros(sz) # a priori estimate of x
Pminus=np.zeros(sz) # a priori error estimate
K=np.zeros(sz) # gain or blending factor
R = 1.0**2 # estimate of measurement variance, change to see effect
# intial guesses
xhat[0] = z[0]
P[0] = 1.0
for k in range(1,n_iter):
# time update
xhatminus[k] = xhat[k-1]
Pminus[k] = P[k-1]+Q
# measurement update
K[k] = Pminus[k]/( Pminus[k]+R )
xhat[k] = xhatminus[k]+K[k]*(z[k]-xhatminus[k])
P[k] = (1-K[k])*Pminus[k]
# add new data to created dataframe
filtered.assign(a = [xhat])
#create visualization of noise reduction
plt.rcParams['figure.figsize'] = (10, 8)
plt.figure()
plt.plot(z,'k+',label='noisy measurements')
plt.plot(xhat,'b-',label='a posteri estimate')
plt.legend()
plt.title('Estimate vs. iteration step', fontweight='bold')
plt.xlabel('column data')
plt.ylabel('Measurement')
这似乎是一个相当直接的错误。该警告表示在创建警告之前,您试图绘制的数字超过当前限制(该参数可以更改,但默认设置为20)。这是因为在for循环的每次迭代中,您都会创建一个新的图形。根据n_iter的大小,您可能会打开数百或数千个图形。这些图中的每一个都需要资源来生成和显示,因此您在系统上创建了一个非常大的资源负载。要么处理速度非常慢,要么完全崩溃。在任何情况下,解决方案都是绘制更少的图形
我不知道你在循环中绘制的是什么,但似乎循环的每个迭代都对应于一个时间步,并且在每个时间步你都想绘制估计值和实际值。在这种情况下,您需要在循环之外定义一次地物和地物选项,而不是在每次迭代时。但更好的方法可能是提前生成要打印的所有数据,并将其存储在易于打印的数据类型(如列表)中,然后在最后打印一次 有道理,我欣赏快速响应,并将在创建新数据帧后在单独的单元格中运行绘图。