本文整理汇总了C#中Matrix.RowEnumerator方法的典型用法代码示例。如果您正苦于以下问题:C# Matrix.RowEnumerator方法的具体用法?C# Matrix.RowEnumerator怎么用?C# Matrix.RowEnumerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Matrix
的用法示例。
在下文中一共展示了Matrix.RowEnumerator方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FitInternal
private void FitInternal(Matrix<double> x, Vector<double> y)
{
if (this.Kernel.KernelFunction != null)
{
// you must store a reference to X to compute the kernel in predict
// TODO: add keyword copy to copy on demand
this.xFit = x;
x = this.ComputeKernel(x);
if (x.RowCount != x.ColumnCount)
{
throw new ArgumentException("X.RowCount should be equal to X.ColumnCount");
}
}
var problem = new svm_problem();
problem.l = x.RowCount;
problem.x = new svm_node[x.RowCount][];
foreach (var row in x.RowEnumerator())
{
if (Kernel.LibSvmKernel == LibSvmKernel.Precomputed)
{
var svmNodes =
row.Item2.GetIndexedEnumerator().Select(i =>
new svm_node
{
index = i.Item1 + 1,
value = i.Item2
});
problem.x[row.Item1] =
new[]
{
new svm_node
{
index = 0,
value = row.Item1 + 1
}
}.Concat(svmNodes).ToArray();
}
else
{
var svmNodes =
row.Item2.GetIndexedEnumerator().Select(
i => new svm_node { index = i.Item1, value = i.Item2 });
problem.x[row.Item1] = svmNodes.ToArray();
}
}
problem.y = y.ToArray();
this.Param.kernel_type = (int)this.Kernel.LibSvmKernel;
if (new[] { LibSvmKernel.Poly, LibSvmKernel.Rbf }.Contains(this.Kernel.LibSvmKernel) &&
this.Gamma == 0)
{
// if custom gamma is not provided ...
this.Param.gamma = 1.0 / x.ColumnCount;
}
else
{
this.Param.gamma = this.Gamma;
}
this.Model = svm.svm_train(problem, this.Param);
}
示例2: MatrixToArray
private static double[][] MatrixToArray(Matrix<double> m)
{
var arr = new double[m.RowCount][];
foreach (var row in m.RowEnumerator())
{
arr[row.Item1] = new double[row.Item2.Count];
foreach (var c in row.Item2.GetIndexedEnumerator())
{
arr[row.Item1][c.Item1] = c.Item2;
}
}
return arr;
}
示例3: PredictInternal
private Vector<double> PredictInternal(Matrix<double> x)
{
x = this.ComputeKernel(x);
if (this.Kernel.KernelFunction != null)
{
if (x.ColumnCount != this.fitShape.Item1)
{
throw new ArgumentException(
string.Format(
"X.shape[1] ={0} should be equal to {1}, " +
"the number of samples at training time",
x.ColumnCount,
this.fitShape.Item1));
}
}
var result = new DenseVector(x.RowCount);
foreach (var r in x.RowEnumerator())
{
svm_node[] svmNodes;
if (this.Kernel.LibSvmKernel == LibSvmKernel.Precomputed)
{
svmNodes = r.Item2.Select(
(v, i) => new svm_node { index = i + 1, value = v }).ToArray();
svmNodes = new[] { new svm_node { index = 0, value = 1 } }.Concat(svmNodes).ToArray();
}
else
{
svmNodes = r.Item2.Select((v, i) => new svm_node { index = i, value = v }).ToArray();
}
result[r.Item1] = svm.svm_predict(this.Model, svmNodes);
}
return result;
}
示例4: CenterData
/// <summary>
/// Centers data to have mean zero along axis 0. This is here because
/// nearly all linear models will want their data to be centered.
/// If sample_weight is not None, then the weighted mean of X and y
/// is zero, and not the mean itself
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="fitIntercept"></param>
/// <param name="normalize"></param>
/// <param name="sampleWeight"></param>
internal static CenterDataResult CenterData(
Matrix<double> x,
Matrix<double> y,
bool fitIntercept,
bool normalize = false,
Vector<double> sampleWeight = null)
{
Vector<double> xMean;
Vector<double> yMean = new DenseVector(y.ColumnCount);
Vector<double> xStd;
if (fitIntercept)
{
if (x is SparseMatrix)
{
xMean = DenseVector.Create(x.ColumnCount, i => 0.0);
xStd = DenseVector.Create(x.ColumnCount, i => 1.0);
}
else
{
if (sampleWeight == null)
{
xMean = x.MeanOfEveryColumn();
}
else
{
xMean = x.MulColumnVector(sampleWeight).SumOfEveryColumn().Divide(sampleWeight.Sum());
}
x = x.SubtractRowVector(xMean);
if (normalize)
{
xStd = new DenseVector(x.ColumnCount);
foreach (var row in x.RowEnumerator())
{
xStd.Add(row.Item2.PointwiseMultiply(row.Item2), xStd);
}
xStd.MapInplace(Math.Sqrt);
for (int i = 0; i < xStd.Count; i++)
{
if (xStd[i] == 0)
{
xStd[i] = 1;
}
}
x.DivRowVector(xStd, x);
}
else
{
xStd = DenseVector.Create(x.ColumnCount, i => 1.0);
}
}
if (sampleWeight == null)
{
yMean = y.MeanOfEveryColumn();
}
else
{
yMean = y.MulColumnVector(sampleWeight).SumOfEveryColumn() / sampleWeight.Sum();
}
y = y.Clone();
y = y.SubtractRowVector(yMean);
}
else
{
xMean = DenseVector.Create(x.ColumnCount, i => 0);
xStd = DenseVector.Create(x.ColumnCount, i => 1);
}
return new CenterDataResult { X = x, Y = y, xMean = xMean, yMean = yMean, xStd = xStd };
}