Здравствуйте!
У меня есть некий код, он рисует гистограммы. И на тестовых данных (приложу их), рисует все, как надо. А на реальных данных выдает ошибку:
RuntimeWarning: divide by zero encountered in log
fit_vec = count_vec * (np.log(count_vec) - np.log(width / dt))
RuntimeWarning: invalid value encountered in multiply
fit_vec = count_vec * (np.log(count_vec) - np.log(width / dt))
И я просто не пойму, почему. И как мне в реальных данных выловить ошибку, потому что на мой взгляд все там хорошо. Помогите кто чем может, пожалуйста((
Код:
	
	
	
		
			
			У меня есть некий код, он рисует гистограммы. И на тестовых данных (приложу их), рисует все, как надо. А на реальных данных выдает ошибку:
RuntimeWarning: divide by zero encountered in log
fit_vec = count_vec * (np.log(count_vec) - np.log(width / dt))
RuntimeWarning: invalid value encountered in multiply
fit_vec = count_vec * (np.log(count_vec) - np.log(width / dt))
И я просто не пойму, почему. И как мне в реальных данных выловить ошибку, потому что на мой взгляд все там хорошо. Помогите кто чем может, пожалуйста((
Код:
		Python:
	
	import numpy as np
import pylab as pl
def create_and_visualise(name):
    x = []
    t1 = []
    t2 = []
    tbins = []
    xplot = []
    with open(name, 'r') as f:
        for line in f:
            data_list = line.split()
            t1.append(float(data_list[0]))
            t2.append(float(data_list[1]))
            x.append(float(data_list[2]))
            tbins.append(float(data_list[0]))
            xplot.append(float(data_list[2]))
    tbins.append(t2[-1])
    xplot.append(x[-1])
    edges, change_points = bayesian_blocks(t1, np.array(tbins), x)
    print(f'{edges=}')
    print(f'{change_points=}')
    i, j, result = 0, 0, []
    while j != len(change_points) - 1:
        tmp = change_points[j + 1] - change_points[j]
        if tmp > 1:
            result.append(sum(x[i:tmp + i]) // tmp)
            i += tmp
        else:
            result.append(x[i])
            i += 1
        j += 1
    else:
        result.append(result[-1])
    pl.step(tbins, xplot, where='post')
    pl.step(edges, result, where='post')
    pl.show()
def bayesian_blocks(t, tb, x):
    # copy and sort the array
    t = np.array(t)
    N = t.size
    # create length-(N + 1) array of cell edges
    edges = tb
    block_length = edges[-1] - edges
    # arrays needed for the iteration
    best = np.zeros(N, dtype=float)
    last = np.zeros(N, dtype=int)
    # -----------------------------------------------------------------
    # Start with first data cell; add one cell at each iteration
    # -----------------------------------------------------------------
    for K in range(N):
        # Compute the width and count of the final bin for all possible
        # locations of the K^th changepoint
        width = block_length[:K + 1] - block_length[K + 1]
        count_vec = np.cumsum(x[:K + 1][::-1])[::-1]
        # evaluate fitness function for these possibilities
        dt = 0.002
        fit_vec = count_vec * (np.log(count_vec) - np.log(width / dt))
        fit_vec -= 4  # 4 comes from the prior on the number of changepoints
        fit_vec[1:] += best[:K]
        # find the max of the fitness: this is the K^th changepoint
        i_max = np.argmax(fit_vec)
        last[K] = i_max
        best[K] = fit_vec[i_max]
    # -----------------------------------------------------------------
    # Recover changepoints by iteratively peeling off the last block
    # -----------------------------------------------------------------
    change_points = np.zeros(N, dtype=int)
    i_cp = N
    ind = N
    while True:
        i_cp -= 1
        change_points[i_cp] = ind
        if ind == 0:
            break
        ind = last[ind - 1]
    change_points = change_points[i_cp:]
    return edges[change_points], change_points
def create_and_show():
    create_and_visualise('GRB_090227.txt')
if __name__ == '__main__':
    create_and_show()