简体   繁体   English

使用卡方找到拟合优度

[英]Using Chi-Squared to find the goodness of a fit

I'm attempting to use scipy.stats chisquare tool to be able to determine goodness of fit between two arrays.我正在尝试使用 scipy.stats chisquare 工具来确定两个数组之间的拟合优度。 I have used the np.polyfit tool with a fit of 10 to be able to find a fit between two arrays, now I'm just trying to figure out how to use the chisquare method to determine how good that fit is.我使用了拟合为 10 的 np.polyfit 工具来找到两个数组之间的拟合,现在我只是想弄清楚如何使用卡方方法来确定拟合的好坏。 How do I correctly use scipy's chisquare tool in this situation?在这种情况下如何正确使用 scipy 的卡方工具?

data = np.loadtxt("location of data")

x = data[:,0] #defining the first column as x
y = data[:,1] #defining the second column as y

fit = np.polyfit(x, y, 10)
p = np.poly1d(fit)

import scipy
from scipy.stats import chisquare

You can directly test the distributions by passing them in the chisquare module as group1 and group2.您可以通过将它们作为 group1 和 group2 传递到 chisquare 模块中来直接测试分布。 X being group1. X 是 group1。 Y being group2. Y 是 group2。 Here group1 represents freq_observed and group2 represents freq_expected.这里 group1 代表 freq_observed,group2 代表 freq_expected。 Set degrees of freedom as optional argument ddof or you can let the model select it from the number of elements将自由度设置为可选参数 ddof 或者您可以让模型从元素数量中选择它

import scipy.stats as stat
stat.chisquare(np.array(group1), np.array(group2))

is the way to use it.是使用它的方法。 Now you can use your fitted data against both of these columns for the test individually as group1 being the fitted data and group2 being the independent X, Y.现在,您可以针对这两列分别使用拟合数据进行测试,因为 group1 是拟合数据,group2 是独立的 X、Y。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM