C# 如何确定整数值列表的模式?
对不起,这真是太愚蠢了。。。有人能帮我吗C# 如何确定整数值列表的模式?,c#,C#,对不起,这真是太愚蠢了。。。有人能帮我吗 公共静态IEnumerable模式(此IEnumerable列表) { var modesList=list .GroupBy(值=>值) .选择(valueCluster=> 新的 { Value=valueCluster.Key, 引用=valueCluster.Count(), }) .ToList(); int maxOccurrence=modesList .Max(g=>g.发生率); 返回模式列表 .Where(x=>x.Occurrenc
公共静态IEnumerable模式(此IEnumerable列表)
{
var modesList=list
.GroupBy(值=>值)
.选择(valueCluster=>
新的
{
Value=valueCluster.Key,
引用=valueCluster.Count(),
})
.ToList();
int maxOccurrence=modesList
.Max(g=>g.发生率);
返回模式列表
.Where(x=>x.Occurrence==maxOccurrence&&maxOccurrence>1)//谢谢!
.选择(x=>x.Value);
}
}
来自
公共静态IEnumerable模式(此IEnumerable列表)
{
var modesList=list
.GroupBy(值=>值)
.选择(valueCluster=>
新的
{
Value=valueCluster.Key,
引用=valueCluster.Count(),
})
.ToList();
int maxOccurrence=modesList
.Max(g=>g.发生率);
返回模式列表
.Where(x=>x.Occurrence==maxOccurrence&&maxOccurrence>1)//谢谢!
.选择(x=>x.Value);
}
}
因为它听起来像一个学生问题,而且语言是C#我认为LINQ查询是不可能的(尽管问题本身必须说明它)。对于LINQ查询,请查看另一个答案。我提供了一个自己动手的答案(不应该在现实世界的编程中使用)
类程序
{
静态void Main(字符串[]参数)
{
var listOfInt=新列表{7,4,2,5,7,5,4,3,4,5,6,3,7,5,7,4,2};
var histogram=BuildHistogram1(listOfInt);
var modeValue=FindMode(直方图);
控制台写入线(modeValue);
}
私有静态int FindMode(字典直方图)
{
int模式=0;
整数计数=0;
foreach(直方图中的KeyValuePair对)
{
如果(配对值>计数)
{
count=pair.Value;
mode=pair.Key;
}
}
返回模式;
}
私有静态字典buildHistorogram1(列表列表OFIT)
{
var直方图=新字典();
foreach(listOfInt中的var v)
{
if(直方图包含(v))
直方图[v]=直方图[v]+1;
其他的
直方图[v]=1;
}
返回直方图;
}
}
它使用字典来构建直方图。但是,如果您知道时间头的值范围,并且它足够窄,那么普通数组也可以用于相同的目的。因为它听起来像一个学生问题,语言是C#我认为LINQ查询是不可能的(尽管问题本身必须说明它)。对于LINQ查询,请查看另一个答案。我提供了一个自己动手的答案(不应该在现实世界的编程中使用)
类程序
{
静态void Main(字符串[]参数)
{
var listOfInt=新列表{7,4,2,5,7,5,4,3,4,5,6,3,7,5,7,4,2};
var histogram=BuildHistogram1(listOfInt);
var modeValue=FindMode(直方图);
控制台写入线(modeValue);
}
私有静态int FindMode(字典直方图)
{
int模式=0;
整数计数=0;
foreach(直方图中的KeyValuePair对)
{
如果(配对值>计数)
{
count=pair.Value;
mode=pair.Key;
}
}
返回模式;
}
私有静态字典buildHistorogram1(列表列表OFIT)
{
var直方图=新字典();
foreach(listOfInt中的var v)
{
if(直方图包含(v))
直方图[v]=直方图[v]+1;
其他的
直方图[v]=1;
}
返回直方图;
}
}
它使用字典来构建直方图。但是,如果您知道时间头的值范围,并且它足够窄,那么普通数组也可以用于相同的目的。您如何定义数据的
“模式”
。。。最明显的数字。吉恩是对的。答案比我的好得多。我想知道当你回答并投票结束时会发生什么您如何定义数据的“模式”
。。。最明显的数字。吉恩是对的。答案比我的好得多。我想知道当你回答并投票结束时会发生什么如果使用MoreLinqMaxBy
方法,则可以简化GroupBy之后的所有代码,并将其从两次传递减少为一次。如果使用MoreLinqMaxBy
方法,则可以简化GroupBy之后的所有代码,并将其从两次传递减少为一次。
List<int> lstNumbers = new List<int>();
private void GetMode()
{
lblMode.Text += //How do I determine the mode of the data
}
public static IEnumerable<double> Modes(this IEnumerable<double> list)
{
var modesList = list
.GroupBy(values => values)
.Select(valueCluster =>
new
{
Value = valueCluster.Key,
Occurrence = valueCluster.Count(),
})
.ToList();
int maxOccurrence = modesList
.Max(g => g.Occurrence);
return modesList
.Where(x => x.Occurrence == maxOccurrence && maxOccurrence > 1) // Thanks Rui!
.Select(x => x.Value);
}
}
class Program
{
static void Main(string[] args)
{
var listOfInt = new List<int> {7, 4, 2, 5, 7, 5, 4, 3, 4, 5, 6, 3, 7, 5, 7, 4, 2};
var histogram = BuildHistogram1(listOfInt);
var modeValue = FindMode(histogram);
Console.WriteLine(modeValue);
}
private static int FindMode(Dictionary<int, int> histogram)
{
int mode = 0;
int count = 0;
foreach (KeyValuePair<int, int> pair in histogram)
{
if( pair.Value>count)
{
count = pair.Value;
mode = pair.Key;
}
}
return mode;
}
private static Dictionary<int,int> BuildHistogram1(List<int> listOfInt)
{
var histogram = new Dictionary<int, int>();
foreach (var v in listOfInt)
{
if (histogram.ContainsKey(v))
histogram[v] = histogram[v] + 1;
else
histogram[v] = 1;
}
return histogram;
}
}