简体   繁体   English

如何将 statsmodels 测试的结果导出到 CSV?

[英]How to export the result from statsmodels test to CSV?

I am just learning how to use the statsmodels (Python module) to perform such things as regression analysis, testing for normality and homogenous variance.我只是在学习如何使用statsmodels (Python 模块)来执行诸如回归分析、正态性测试和同质方差之类的事情。 I am working with several data sets (usually as CSV files) and would like to write a script to help me do this more efficiently.我正在处理多个数据集(通常为 CSV 文件),并且想编写一个脚本来帮助我更有效地完成此操作。 My data is just a set of numbers.我的数据只是一组数字。

Example of data数据示例

column1, column2
2.80609,2.80609
2.39059,1.6697286666666666
3.6487540000000003,1.8243770000000001
1.8582885714285717,3.0046419047619044
2.587834,1.7252226666666666
...

Specifically, I would like to:具体来说,我想:

(1) Iterate a test over several files (1) 对多个文件进行迭代测试

(2) Save the result of each test to a new row in a new file (2) 将每次测试的结果保存到新文件的新行中

An example of one of the tests I am using.我正在使用的测试之一的示例。

data = pd.read_csv("data.csv") 

column1 = data['column1']
p_value = sm.stats.diagnostic.lilliefors(data, dist='norm', pvalmethod='approx')

The float it exports look like this.它导出的浮点数如下所示。

(0.08557045418097009, 7.144631930303909e-50)

My adventures into if/else and boolean values resulted in this code that prints a text that would be nifty to have exported as well.我对 if/else 和 boolean 值的冒险导致这段代码打印出一个也很适合导出的文本。

p_value = sm.stats.diagnostic.lilliefors(curvature_length, dist='norm', pvalmethod='approx')[1]
if p_value<0.05:
    print("Data is not normal distributed")
else:
    print("Data is normal distributed")
print(p_value)

Any tips & feedback on how to go about this would be greatly appreciated!任何有关如何 go 的提示和反馈将不胜感激!

To write the two floats to CSV https://docs.python.org/3/library/csv.html将两个浮点数写入 CSV https://docs.python.org/3/library/csv.ZFC35EZ8383226

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile)

cycle through the pairs of floats doing循环通过成对的花车做

        csvwriter.writerow([float1, float2])

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

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