如何更正此错误:溢出错误:无法将浮点无穷大转换为整数

时间:2021-02-19 11:29:36

标签: python compiler-errors model overflow

我正在尝试制作 Bak-Tang 沙堆模型,但出现此错误。我正在为 Plot 分发制作一个 python 代码。我想生成 Avalanche 的图,然后用线性函数拟合它。我提供了显示错误的完整代码。这是我的代码:

import numpy as np
import random
import matplotlib.pyplot as plt
import sys
sys.setrecursionlimit(100000)
import warnings
warnings.filterwarnings("ignore")

iterations = 100000 # number of iterations
N = 50 # size of matrix

global threshold
threshold = 4 # critical level for toppling to occur

topple = 0
topplesize = []

def initializeMatrix(N):
    return np.random.randint(4, size=(N,N))

def drop(M, x, y, N):
    global topple
    withinBounds = True

    if x < 0  or x > N-1 or y < 0 or y > N-1:
        withinBounds = False
        pass

    if withinBounds:
        M[x,y] = M[x,y] + 1
        if M[x,y] >= threshold:
            M[x,y] = M[x,y] - 4 # reset the cell value and distribute to neighbors
            topple += 1 # count the toppling
            drop(M, x+1, y, N)
            drop(M, x-1, y, N)
            drop(M, x, y-1, N)
            drop(M, x, y+1, N)

M = initializeMatrix(N)
plt.figure(figsize=(8,8))
plt.imshow(M)
plt.colorbar()
plt.title("The Sandpile Table")
plt.show()

for i in range(iterations):
    topple = 0
    x = random.randint(0, N-1)
    y = random.randint(0, N-1)
    drop(M, x, y, N)
    topplesize.append(topple)
x, y = np.histogram(topplesize, 1000)
plt.figure(figsize=(10,7))
plt.clf()
plt.loglog(y[0:-1],x, 'r.')
plt.title("Avalanche Size Distribution", fontsize=14)
plt.xlabel(r"$\log$" + "(Avalanche Size)", fontsize=12)
plt.ylabel(r"$\log$" + "(Frequency)", fontsize=12)
plt.show()

plt.figure(figsize=(8,8))
plt.imshow(M)
plt.colorbar()
plt.title("The Sandpile Table")
plt.show()

import powerlaw


def plotDist(topplesize, xmin, xmax):
    x, y = powerlaw.pdf(np.array(topplesize), linear_bins=True)
    ind = y>0
    y = y[ind]
    x = x[:-1]
    x = x[ind]
    
    tsize = np.array(topplesize)
    fit = powerlaw.Fit(tsize[tsize>0], discrete=True, xmin=xmin, xmax=xmax)
    alpha = fit.power_law.alpha
        
    fig = plt.figure(figsize=(10,7))
    ax1 = fig.add_subplot(1,1,1)
    #ax1.scatter(x, y, color='r', s=1.2, label="PDF (scatter)")
    powerlaw.plot_pdf(tsize[tsize>0], color='b', ax=ax1, linestyle='-', linewidth=2, label="PDF")
    fit.power_law.plot_pdf(tsize[tsize>0], color='b', ax=ax1, linestyle='--', linewidth=1, label="Power-law fit")
    plt.xlabel(r"Avalanche Size, $s$", fontsize=12)
    plt.ylabel(r"$p(s)$", fontsize=12)
    _ = plt.text(10e1,50e-4,r"$\alpha$="+str(round(alpha,1)), fontsize=13)
    _ = plt.legend(frameon=False)
        
    return
    
    topplesize50 = topplesize
    plotDist(topplesize50, xmin=10, xmax=500)

当我尝试运行 plotDist() 时,它给了我错误。像这样:

OverflowError                             Traceback (most recent call last)
<ipython-input-40-0a16409c5a3d> in <module>()
----> 1 plotDist(topplesize60, xmin=10, xmax=1000)

1 frames
<ipython-input-36-8b425d5f67b8> in plotDist(topplesize, xmin, xmax)
      1 import powerlaw
      2 def plotDist(topplesize, xmin, xmax):
----> 3     x, y = powerlaw.pdf(np.array(topplesize), linear_bins=True)
      4     ind = y>0
      5     y = y[ind]

/usr/local/lib/python3.6/dist-packages/powerlaw.py in pdf(data, xmin, xmax, linear_bins, **kwargs)
   1962         bins = kwargs.pop('bins')
   1963     elif linear_bins:
-> 1964         bins = range(int(xmin2), ceil(xmax2)+1)
   1965     else:
   1966         log_min_size = log10(xmin2)

OverflowError: cannot convert float infinity to integer

0 个答案:

没有答案
相关问题