【问题标题】:Why is my notebook crashing when I run this for loop and what is the fix?当我运行这个 for 循环时,为什么我的笔记本崩溃了,解决方法是什么?
【发布时间】:2019-09-27 20:24:36
【问题描述】:

我已经获取了与卡尔曼滤波器相关的代码,并且正在尝试遍历每一列数据。我希望发生的是:

  1. 列数据被送入过滤器
  2. 过滤后的列数据(xhat)放入另一个DataFrame(过滤)
  3. 过滤后的列数据 (xhat) 用于生成视觉效果。

我创建了一个 for 循环来遍历列数据,但是当我运行单元格时,我的笔记本崩溃了。当它不崩溃时,我会收到以下警告:

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')

【问题讨论】:

    标签: python-3.x numpy for-loop matplotlib jupyter-notebook


    【解决方案1】:

    这似乎是一个非常简单的错误。该警告表明您在创建警告之前尝试绘制的数字超过当前限制(您可以更改该参数,但默认设置为 20)。这是因为在 for 循环的每次迭代中,您都会创建一个新图形。根据 n_iter 的大小,您可能会打开数百或数千个图形。这些数字中的每一个都需要资源来生成和显示,因此您在系统上创建了非常大的资源负载。它要么处理得非常缓慢,要么完全崩溃。无论如何,解决方案是绘制更少的数字。

    我不知道您在循环中究竟在绘制什么,但似乎循环的每次迭代都对应一个时间步长,并且在每个时间步长您都希望绘制估计值和实际值。在这种情况下,您需要在循环之外定义一次图形和图形选项,而不是在每次迭代时。但更好的方法可能是提前生成您想要绘制的所有数据并将其存储在易于绘制的数据类型(如列表)中,然后在最后绘制一次。

    【讨论】:

    • 有道理,我很欣赏快速响应,并将在创建新数据框后在单独的单元格中运行绘图。
    猜你喜欢
    • 2015-11-05
    • 1970-01-01
    • 2022-06-11
    • 2022-12-29
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多