我有一个DataPoint列表,如
List<DataPoint> newpoints=new List<DataPoint>(); 其中DataPoint是一个类,由A到I的九个双重特性组成,
newpoints.count=100000 double points (i.e each point consists of nine double features from A to I)我需要应用赋范的列表新点使用最小-最大的去甲方法和scale_range之间的0和1。
到目前为止,我已经执行了以下步骤
该方法是成功完成的,但需要更多的时间(即3分45秒)。
如何使用Max_min ( C#中的LINQ代码)来将我的时间减少到几秒钟?我在Stackoverflow How to normalize a list of int values中发现了这个问题,但我的问题是
double valueMax = list.Max(); // I need Max point for feature A for all 100000
double valueMin = list.Min(); //I need Min point for feature A for all 100000诸如此类,对于所有其他九个功能,您的帮助将受到高度赞赏。
发布于 2014-12-27 11:02:21
作为将您的9个特性建模为类" datapoint“上的双重属性的替代方法,您还可以将一个9倍的数据点建模为一个数组,其好处是您可以再次使用LINQ一次完成所有9个计算:
var newpoints = new List<double[]>
{
new []{1.23, 2.34, 3.45, 4.56, 5.67, 6.78, 7.89, 8.90, 9.12},
new []{2.34, 3.45, 4.56, 5.67, 6.78, 7.89, 8.90, 9.12, 12.23},
new []{3.45, 4.56, 5.67, 6.78, 7.89, 8.90, 9.12, 12.23, 13.34},
new []{4.56, 5.67, 6.78, 7.89, 8.90, 9.12, 12.23, 13.34, 15.32}
};
var featureStats = newpoints
// We make the assumption that all 9 data points are present on each row.
.First()
// 2 Anon Projections - first to determine min / max as a function of column
.Select((np, idx) => new
{
Idx = idx,
Max = newpoints.Max(x => x[idx]),
Min = newpoints.Min(x => x[idx])
})
// Second to add in the dynamic Range
.Select(x => new {
x.Idx,
x.Max,
x.Min,
Range = x.Max - x.Min
})
// Back to array for O(1) lookups.
.ToArray();
// Do the normalizaton for the columns, for each row.
var normalizedFeatures = newpoints
.Select(np => np.Select(
(i, idx) => (i - featureStats[idx].Min) / featureStats[idx].Range));
foreach(var datapoint in normalizedFeatures)
{
Console.WriteLine(string.Join(",", datapoint.Select(x => x.ToString("0.00"))));
}结果:
0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00
0.33,0.33,0.33,0.33,0.34,0.47,0.23,0.05,0.50
0.67,0.67,0.67,0.67,0.69,0.91,0.28,0.75,0.68
1.00,1.00,1.00,1.00,1.00,1.00,1.00,1.00,1.00发布于 2014-12-27 10:40:20
停止一次又一次地重新计算最大值/最小值,它不会改变。
double maxInFeatureA = array_A.Max();
double minInFeatureA = array_A.Min();
// somewher in the loop:
normilized_featureA= (((array_A[i] - minInFeatureA ) * (1 - 0)) /
(maxInFeatureA - minInFeatureA ))+0;当在foreach/for中使用许多元素时,Max/Min对于数组来说是非常昂贵的。
我建议你接受以下代码:Array data normalization
并把它当作
var normalizedPoints = newPoints.Select(x => x.A)
.NormalizeData(1, 1)
.ToList(); 发布于 2014-12-27 10:53:09
double min = newpoints.Min(p => p.A);
double max = newpoints.Max(p => p.A);
double readonly normalizer = 1 / (max - min);
var normalizedFeatureA = newpoints.Select(p => (p.A - min) * normalizer);https://stackoverflow.com/questions/27666412
复制相似问题