How to fix ZeroDivisionError: float division by zero when calculating Gaussian probability
How to fix ZeroDivisionError: float division by zero when calculating Gaussian probability
我正在尝试计算高斯概率,但是当我 运行 通过我的数据时,我得到 ZeroDivisionError: float division by zero
代码如下:
def GaussianProbability(x, mean, std):
exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(std,2))))
return (1 / (math.sqrt(2*math.pi) * std)) * exponent
以及完整的错误日志:
ZeroDivisionError Traceback (most recent call last)
<ipython-input-22-b3fd5204c6fa> in <module>
----> 1 precisions_PDZ1,MAP,recalls_PDZ1,fp_rates_PDZ1 = CV_results(PDZ1_graphs,PDZ1_targets,'undirected')
<ipython-input-14-044940745269> in CV_results(list_graphs, list_targets, network_type, multi)
7 for part in tqdm(range(5)):
8
----> 9 probabilities_0,predictions = prediction(list_graphs[part], list_targets[part], network_type, multi)
10
11 if recalls == [] and fp_rates == []:
<ipython-input-12-beeed13223ea> in prediction(graph, actual_edges, network_type, multi)
11
12 for i in tqdm(range(len(X_test))):
---> 13 prob = ClassProbabilities(summaries, prob_class, X_test[i])
14 if (prob[0]+prob[1])==0:
15 # both probabilities are too low
<ipython-input-11-286b09a753e9> in ClassProbabilities(summaries, prob_class, x)
8 for i in range(len(classSummaries)): # for every attribute
9 mean, std = classSummaries[i]
---> 10 probabilities[classValue] *= GaussianProbability(x[i], mean, std)
11
12 return probabilities
<ipython-input-10-4a13cc5a69b2> in GaussianProbability(x, mean, std)
1 def GaussianProbability(x, mean, std):
----> 2 exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(std,2))))
3 return (1 / (math.sqrt(2*math.pi) * std)) * exponent
ZeroDivisionError: float division by zero
当你的 standerdv 等于零时,你的函数需要有一个 else
语句
您可以查看此参考资料,它可能会有所帮助 What is the normal distribution when standard deviation is zero?
唯一可能出现这种错误的地方是(2*math.pow(std,2)) == 0
。
这意味着对于某些数据,当所有样本都具有完全相同的值时,您的 std == 0
就会发生这种情况。
你需要处理这种情况!
除非你想自己实现(不推荐)你可以使用scipy
package:
import scipy
scipy.stats.norm(0, 1).pdf(0)
您还可以使用内置统计数据包 python 提供(更简单、更快速的计算)。看看here
我正在尝试计算高斯概率,但是当我 运行 通过我的数据时,我得到 ZeroDivisionError: float division by zero
代码如下:
def GaussianProbability(x, mean, std):
exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(std,2))))
return (1 / (math.sqrt(2*math.pi) * std)) * exponent
以及完整的错误日志:
ZeroDivisionError Traceback (most recent call last)
<ipython-input-22-b3fd5204c6fa> in <module>
----> 1 precisions_PDZ1,MAP,recalls_PDZ1,fp_rates_PDZ1 = CV_results(PDZ1_graphs,PDZ1_targets,'undirected')
<ipython-input-14-044940745269> in CV_results(list_graphs, list_targets, network_type, multi)
7 for part in tqdm(range(5)):
8
----> 9 probabilities_0,predictions = prediction(list_graphs[part], list_targets[part], network_type, multi)
10
11 if recalls == [] and fp_rates == []:
<ipython-input-12-beeed13223ea> in prediction(graph, actual_edges, network_type, multi)
11
12 for i in tqdm(range(len(X_test))):
---> 13 prob = ClassProbabilities(summaries, prob_class, X_test[i])
14 if (prob[0]+prob[1])==0:
15 # both probabilities are too low
<ipython-input-11-286b09a753e9> in ClassProbabilities(summaries, prob_class, x)
8 for i in range(len(classSummaries)): # for every attribute
9 mean, std = classSummaries[i]
---> 10 probabilities[classValue] *= GaussianProbability(x[i], mean, std)
11
12 return probabilities
<ipython-input-10-4a13cc5a69b2> in GaussianProbability(x, mean, std)
1 def GaussianProbability(x, mean, std):
----> 2 exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(std,2))))
3 return (1 / (math.sqrt(2*math.pi) * std)) * exponent
ZeroDivisionError: float division by zero
当你的 standerdv 等于零时,你的函数需要有一个 else
语句
您可以查看此参考资料,它可能会有所帮助 What is the normal distribution when standard deviation is zero?
唯一可能出现这种错误的地方是(2*math.pow(std,2)) == 0
。
这意味着对于某些数据,当所有样本都具有完全相同的值时,您的 std == 0
就会发生这种情况。
你需要处理这种情况!
除非你想自己实现(不推荐)你可以使用scipy
package:
import scipy
scipy.stats.norm(0, 1).pdf(0)
您还可以使用内置统计数据包 python 提供(更简单、更快速的计算)。看看here