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


C# DenseVector.MapInplace方法代码示例

本文整理汇总了C#中MathNet.Numerics.LinearAlgebra.Double.DenseVector.MapInplace方法的典型用法代码示例。如果您正苦于以下问题:C# DenseVector.MapInplace方法的具体用法?C# DenseVector.MapInplace怎么用?C# DenseVector.MapInplace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MathNet.Numerics.LinearAlgebra.Double.DenseVector的用法示例。


在下文中一共展示了DenseVector.MapInplace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: 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


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