本文介绍了我对C#不太熟悉。如何循环遍历数据表以分别计算每个类的前四列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现的所有示例都使用foreach循环。在添加其他2个类之前,我使用了foreach循环,但现在我必须使索引工作正常。有什么建议吗?
All the examples I find use a foreach loop. Prior to adding the other 2 classes I used a foreach loop fine, but now I got to get the indexing to work right. Any suggestions?
static void Main(string[] args)
{
DataTable table = new DataTable("IrisTrainingData");
table.Columns.Add("SepalLength", typeof(double));
table.Columns.Add("SepalWidth", typeof(double));
table.Columns.Add("PedalLength", typeof(double));
table.Columns.Add("PedalWidth", typeof(double));
table.Columns.Add("Class", typeof(double));
//training data
table.Rows.Add(5.1, 3.5, 1.4, 0.2, 1);
table.Rows.Add(4.9, 3.0, 1.4, 0.2, 1);
table.Rows.Add(4.7, 3.2, 1.3, 0.2, 1);
table.Rows.Add(4.6, 3.1, 1.5, 0.2, 1);
table.Rows.Add(5.0, 3.6, 1.4, 0.2, 1);
table.Rows.Add(5.4, 3.9, 1.7, 0.4, 1);
table.Rows.Add(4.6, 3.4, 1.4, 0.3, 1);
table.Rows.Add(5.0, 3.4, 1.5, 0.2, 1);
table.Rows.Add(4.4, 2.9, 1.4, 0.2, 1);
table.Rows.Add(4.9, 3.1, 1.5, 0.1, 1);
table.Rows.Add(5.4, 3.7, 1.5, 0.2, 1);
table.Rows.Add(4.8, 3.4, 1.6, 0.2, 1);
table.Rows.Add(4.8, 3.0, 1.4, 0.1, 1);
table.Rows.Add(4.3, 3.0, 1.1, 0.1, 1);
table.Rows.Add(5.8, 4.0, 1.2, 0.2, 1);
table.Rows.Add(5.7, 4.4, 1.5, 0.4, 1);
table.Rows.Add(5.4, 3.9, 1.3, 0.4, 1);
table.Rows.Add(5.1, 3.5, 1.4, 0.3, 1);
table.Rows.Add(5.7, 3.8, 1.7, 0.3, 1);
table.Rows.Add(5.1, 3.8, 1.5, 0.3, 1);
table.Rows.Add(5.4, 3.4, 1.7, 0.2, 1);
table.Rows.Add(5.1, 3.7, 1.5, 0.4, 1);
table.Rows.Add(4.6, 3.6, 1.0, 0.2, 1);
table.Rows.Add(5.1, 3.3, 1.7, 0.5, 1);
table.Rows.Add(4.8, 3.4, 1.9, 0.2, 1);
// Variables to total the values of the four columns
double total1 = 0.0;
double total2=0.0;
double total3=0.0;
double total4=0.0;
//Calculate the sum of training data
for (int i = 0; i < 25; i++)
{
total1 += table.Rows[i]["SepalLength"]; <- I know this is wrong
total2 += (double)row["SepalWidth"];
total3 += (double)row["PedalLength"];
total4 += (double)row["PedalWidth"];
}
推荐答案
var tableFiltered = table.AsEnumerable().Where(p => p.Field<double>("Class") == 1);
double total1 = tableFiltered.Sum(p => p.Field<double>("SepalLength"));
double total2 = tableFiltered.Sum(p => p.Field<double>("SepalWidth"));
double total3 = tableFiltered.Sum(p => p.Field<double>("PedalLength"));
double total4 = tableFiltered.Sum(p => p.Field<double>("PedalWidth"));
要计算方差,均值,标准偏差,您可以将此扩展方法类添加到项目中;
To calculate variance, mean, standart deviation you can add this extension methods class to your project;
public static class MyListExtensions
{
public static double Mean(this List<double> values)
{
return values.Count == 0 ? 0 : values.Mean(0, values.Count);
}
public static double Mean(this List<double> values, int start, int end)
{
double s = 0;
for (int i = start; i < end; i++)
{
s += values[i];
}
return s / (end - start);
}
public static double Variance(this List<double> values)
{
return values.Variance(values.Mean(), 0, values.Count);
}
public static double Variance(this List<double> values, double mean)
{
return values.Variance(mean, 0, values.Count);
}
public static double Variance(this List<double> values, double mean, int start, int end)
{
double variance = 0;
for (int i = start; i < end; i++)
{
variance += Math.Pow((values[i] - mean), 2);
}
int n = end - start;
if (start > 0) n -= 1;
return variance / (n);
}
public static double StandardDeviation(this List<double> values)
{
return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count);
}
public static double StandardDeviation(this List<double> values, int start, int end)
{
double mean = values.Mean(start, end);
double variance = values.Variance(mean, start, end);
return Math.Sqrt(variance);
}
}
那么你可以简单地找到一列这样的变体;
Then you can simply find a variation of one column like that;
var tableFiltered = table.AsEnumerable().Where(p => p.Field<double>("Class") == 1);
double varSepalLen = tableFiltered.Select(p => p.Field<double>("SepalLength")).ToList().Variance();
Double nrClasses = Convert.ToDouble(table.Compute("max(Class)", string.Empty));
double[] total1 = new double[nrClasses];
double[] total2 = new double[nrClasses];
double[] total3 = new double[nrClasses];
double[] total4 = new double[nrClasses];
用
替换你的循环
replace your loop with
foreach (DataRow row in table.Rows)
{
double nrClass = row["Class"] == DBNull.Value ? 0 : Convert.ToDouble(row["Class"]) -1;
total1[nrClass] += row["SepalLength"] == DBNull.Value ? 0: Convert.ToDouble(row["SepalLength"]);
total2[nrClass] += row["SepalWidth"] == DBNull.Value ? 0: Convert.ToDouble(row["SepalWidth"]);
total3[nrClass] += row["PedalLength"] == DBNull.Value ? 0: Convert.ToDouble(row["PedalLength"]);
total4[nrClass] += row["PedalWidth"] == DBNull.Value ? 0: Convert.ToDouble(row["PedalWidth"]);
}
这篇关于我对C#不太熟悉。如何循环遍历数据表以分别计算每个类的前四列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!