我怎样才能将这个Python解释为C#? [英] How can I interpret this Python into C#?

查看:68
本文介绍了我怎样才能将这个Python解释为C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们 - 我绝对不是蟒蛇人,但我是C#家伙。我可以试着弄清楚如何在c#中重新创建这段代码片段吗?



背景:

我有一个标签 - 包含p值/临界值的分隔文件(用于卡方分析)。共有9列数据。第一列是自由度,后续列是p值的临界值。没有标题,即数据从第0行开始。看起来我有一个百分位数组正在设置,它用于片段中的rx计算以及稍后添加一些额外的统计数据分析与p - 表中的值和自由度。经常使用bs数组。 in1变量是要枚举的文件的文件路径。



 df = [] 
bs = []
百分位数= [[] i 范围内( 100 )]
line_idx, 枚举(in1) :
cols = line.replace(' \ n'' ')。split(' \t'
df.append(float(cols [ 0 ]))
bs.append(float(cols [1]))
for j 范围内( 9 ):
百分位数[line_idx] .append(float(cols [j + 1 ]))
rx =(百分位数[line_idx] [ 2 ] +百分位数[line_idx] [ 0 ] - 2 *百分位[line_idx] [ 1 ]) /(百分位数[line_idx] [ 2 ] - 百分位数[line_idx] [ 0 ])
bs.append(rx)





我尝试过:



我尝试设置double [] [] test = double [100] [];当我试图直接翻译所有内容时编译,但是引用了许多运行时错误引用了索引之外的内容 - 我不相信这种方法有很多正面的结果。



我将chi平方文件放入数据表中,并确定它可能有用。我没有编辑任何数据。我无法将p值1:1匹配,因为它们处于奇怪的时间间隔......我能够识别0.05和0.005的p值,但其他一切都在p值约为0.99和0之间。 div class =h2_lin>解决方案

这可能是一个很好的起点:



  var  df =  new  List< float>(); 
var bs = new List< float>();
var 百分位数= new 列表< float> [ 100 ];
for int i = 0 ; i < percentiles.Length; i ++)
{
percentiles [i] = 列表< float>();
}

var line_idx = 0 ;
foreach var line in enumerate(in1))
{
var cols = line.Replace(Environment.NewLine,
.Split( new [] {' \t'});
df.Add( float .Parse(cols [ 0 ]));
for int j = 1 ; j < 9 ; j ++)
{
百分位数[line_idx ] .Add( float .Parse(cols [j]));
}

var rx =(百分位[line_idx] [ 2 ] +百分位数[line_idx] [ 0 ] - 2 *百分位[line_idx] [ 1 ])/(百分位数[line_idx] [ 2 ] - 百分位数[line_idx] [ 0 ]);
bs.Add(rx);
line_idx ++;
}





我本来可以做一些错字,因为这只是一个小小的动态片段。



编辑:Linq

  var  values = File.ReadLines( 
.Select(line = >
{
var cols = line.Replace(Environment.NewLine,
.Split( new [] {' \t'})
。选择(m = > float .Parse(m))
.ToArray();
返回 new
{
df = cols [ 0 ],
percentiles = cols.Skip( 1 )。ToArray(),
bs =(cols [ 3 ] + cols [ 1 ] - 2 * cols [ 2 ])/(cols [ 3 ] - cols [ 1 ])
};
});
var df = values.Select(m = > m.df)。ToArray( );
var bs = values.Select(m = > m.bs)。ToArray( );
var 百分位数= values.Select(m = > m.percentiles)。ToArray( );


Hey folks - I'm definitely not a python guy but I am a C# guy. Can I get a hand trying to figure out how I can recreate this code snippet in c#?

Background:
I have a tab-delimited file that holds a p-value/critical value (for chi squared analysis). There are 9 columns of data. The first column are degrees of freedom and the subsequent columns are the critical values in order of p-value. There are no headers, i.e. the data starts on row 0. It looks like I have a percentiles array being set up and it is used in the rx calculation in the snippet as well as later on to add on some extra stats analysis vs. p-values in the table and degrees of freedom. The bs array is used frequently. The in1 variable is the file path of the file to be enumerated.

df=[]
	bs=[]
	percentiles=[[] for i in range(100)]
	for line_idx, line in enumerate(in1):
		cols = line.replace('\n', '').split('\t')		
		df.append(float(cols[0]))
		# bs.append(float(cols[1]))
		for j in range(9):
			percentiles[line_idx].append(float(cols[j+1]))
		rx=(percentiles[line_idx][2]+percentiles[line_idx][0]-2*percentiles[line_idx][1])/(percentiles[line_idx][2]-percentiles[line_idx][0])
		bs.append(rx)



What I have tried:

I tried setting up a double[][] test = double[100][]; and this compiled when I tried to translate everything directly, but got many runtime errors referencing things being out of index - I don't believe that there was much positive with that method.

I put the chi squared file into a data table as well figuring it might be helpful. I didn't edit any data. I can't match up the p-values 1:1 because they are at weird intervals... I was able to identify p values of 0.05 and 0.005 but everything else ranges between a p-value of somewhere around 0.99 and 0.

解决方案

This could be a good starting point:

var df = new List<float>();
var bs = new List<float>();
var percentiles = new List<float>[100];
for(int i = 0; i < percentiles.Length; i++)
{
    percentiles[i] = new List<float>();
}

var line_idx = 0;
foreach(var line in enumerate(in1))
{
    var cols = line.Replace(Environment.NewLine, "")
                   .Split(new[]{'\t'});
    df.Add(float.Parse(cols[0]));
    for(int j = 1; j < 9; j++)
    {
        percentiles[line_idx].Add(float.Parse(cols[j]));
    }
    
    var rx = (percentiles[line_idx][2] + percentiles[line_idx][0] - 2 * percentiles[line_idx][1])/(percentiles[line_idx][2]-percentiles[line_idx][0]);
    bs.Add(rx);
    line_idx++;
}



I could have done some typo as this is just a little snippet on the fly.

EDIT: Linq

var values = File.ReadLines("")
                .Select(line =>
                {
                    var cols = line.Replace(Environment.NewLine, "")
                                   .Split(new[] { '\t' })
                                   .Select(m => float.Parse(m))
                                   .ToArray();
                    return new
                    {
                        df = cols[0],
                        percentiles = cols.Skip(1).ToArray(),
                        bs = (cols[3] + cols[1] - 2 * cols[2]) / (cols[3] - cols[1])
                    };
                });
var df = values.Select(m => m.df).ToArray();
var bs = values.Select(m => m.bs).ToArray();
var percentiles = values.Select(m => m.percentiles).ToArray();


这篇关于我怎样才能将这个Python解释为C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆