当前位置: 首页>>代码示例>>C#>>正文


C# Matrix.RowEnumerator方法代码示例

本文整理汇总了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);
        }
开发者ID:geoparsYoti,项目名称:Sharpkit.Learn,代码行数:66,代码来源:LibSvmBase.cs

示例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;
        }
开发者ID:geoparsYoti,项目名称:Sharpkit.Learn,代码行数:15,代码来源:LibSvmBase.cs

示例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;
        }
开发者ID:geoparsYoti,项目名称:Sharpkit.Learn,代码行数:37,代码来源:LibSvmBase.cs

示例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 };
        }
开发者ID:geoparsYoti,项目名称:Sharpkit.Learn,代码行数:89,代码来源:LinearModel.cs


注:本文中的Matrix.RowEnumerator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。