Skip to main content
 首页 » 编程设计

python之确定 txt 文件中的数据是否符合某些统计信息

2025年05月04日12soundcode

我正在使用盖革计数器,它可以连接到计算机并以 .txt 文件 NC.txt 的形式记录其输出,它记录自开始以来的时间和它记录的辐射。看起来像

import pylab 
import scipy.stats 
import numpy as np 
import matplotlib.pyplot as plt 
x1 = [] 
y1 = [] 
#Define a dictionary: counts 
f = open("NC.txt", "r") 
for line in f: 
   line = line.strip() 
   parts = line.split(",") #the columns are separated by commas and spaces 
   time = float(parts[1])  #time is recorded in the second column of NC.txt 
   value = float(parts[2]) #and the value it records is in the third 
   x1.append(time) 
   y1.append(value) 
f.close() 
 
xv = np.array(x1) 
yv = np.array(y1) 
 
#Statistics 
m = np.mean(yv) 
d = np.std(yv) 
#Strip out background radiation 
trueval = yv - m 
 
#Basic plot of counts 
num_bins = 10000 
plt.hist(trueval,num_bins) 
plt.xlabel('Value') 
plt.ylabel('Count') 
plt.show() 

到目前为止,这段代码只会创建一个以零为中心的辐射计数的简单直方图,因此忽略了背景辐射。

我现在想做的是执行卡方检验,看看数据与泊松统计数据的拟合程度如何(然后继续将其与其他分布进行比较)。我不太确定该怎么做。我可以访问 scipy 和 numpy,所以我觉得这应该是一个简单的任务,但我只是在学习 python 的时候来到这里,所以我不是一个了不起的程序员。

有谁知道一个简单的方法来做到这一点?

为清楚起见进行编辑:我并没有过多询问是否存在卡方函数。我更感兴趣的是如何将它与其他统计分布进行比较。

提前致谢。

请您参考如下方法:

您可以使用 SciPy 库,here is documentation和例子。