private void regression(List <Cell> samplePoints) { // 构造输入输出数据集 // 样本数目 int COUNT = samplePoints.Count; // 构造输入和输出数据集 double[][] inputs = new double[COUNT][]; bool[] outputs = new bool[COUNT]; for (int i = 0; i < COUNT; i++) { Cell cell = samplePoints[i]; int pos = cell.row * width + cell.col; inputs[i] = (from buffer in driveBuffers select buffer[pos]).ToArray <double>(); outputs[i] = cell.type; } var learner = new IterativeReweightedLeastSquares <Accord.Statistics.Models.Regression.LogisticRegression>() { Tolerance = 1e-8, // 收敛参数 Iterations = 20, // 最大循环数目 Regularization = 0, ComputeStandardErrors = true }; Accord.Statistics.Models.Regression.LogisticRegression regression = learner.Learn(inputs, outputs); // 输出 odds StringBuilder strb = new StringBuilder(); for (int i = 0; i <= inputs[0].Length; i++) { strb.AppendLine(" " + i + " : " + regression.GetOddsRatio(i)); } updateConsoleEvent(strb.ToString()); // 输出 weights StringBuilder strw = new StringBuilder(); strw.AppendLine("权重系数:"); strw.AppendLine("截距: " + regression.Intercept.ToString()); var weights = regression.Weights; for (int i = 0; i < weights.Length; i++) { strw.AppendLine("权重" + (i + 1) + ":" + weights[i]); } updateConsoleEvent(strw.ToString()); double[] result = new double[width * height]; double minProp = double.MaxValue; double[] minInput = null; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int pos = row * width + col; if (beginBuffer[pos] < 0 || !IsValid(pos)) { result[pos] = this.landUse.NullInfo.LandUseTypeValue; continue; } double[] input = (from buffer in driveBuffers select buffer[pos]).ToArray <double>(); double prop = regression.Probability(input); if (prop < minProp) { minProp = prop; minInput = input; } result[pos] = prop; } } // 新建 GDAL dataset OSGeo.GDAL.Driver driver = OSGeo.GDAL.Gdal.GetDriverByName("GTIFF"); OSGeo.GDAL.Dataset dataset = driver.Create(this.ResultLayerName, width, height, 1, OSGeo.GDAL.DataType.GDT_Float64, null); dataset.WriteRaster(0, 0, width, height, result, width, height, 1, new int[1] { 1 }, 0, 0, 0); dataset.FlushCache(); }
public double[] GetResult() { // 采样 List <Cell> samplePoints = getSample(this.NumberOfSample); // 样本数目 int COUNT = samplePoints.Count; // 构造输入和输出数据集 double[][] inputs = new double[COUNT][]; bool[] outputs = new bool[COUNT]; for (int i = 0; i < COUNT; i++) { Cell cell = samplePoints[i]; int pos = cell.row * width + cell.col; inputs[i] = (from buffer in driveBuffers select buffer[pos]).ToArray <double>(); outputs[i] = cell.type; } var learner = new IterativeReweightedLeastSquares <Accord.Statistics.Models.Regression.LogisticRegression>() { Tolerance = 1e-8, // 收敛参数 Iterations = 20, // 最大循环数目 Regularization = 0, ComputeStandardErrors = true }; Accord.Statistics.Models.Regression.LogisticRegression regression = learner.Learn(inputs, outputs); //// 输出 odds //StringBuilder strb = new StringBuilder(); //for (int i = 0; i <= inputs[0].Length; i++) //{ // strb.AppendLine(" " + i + " : " + regression.GetOddsRatio(i)); //} //updateConsoleEvent(strb.ToString()); //// 输出 weights //StringBuilder strw = new StringBuilder(); //strw.AppendLine("权重系数:"); //strw.AppendLine("截距: " + regression.Intercept.ToString()); //var weights = regression.Weights; //for (int i = 0; i < weights.Length; i++) //{ // strw.AppendLine("权重" + (i + 1) + ":" + weights[i]); //} //updateConsoleEvent(strw.ToString()); double[] result = new double[width * height]; double minProp = double.MaxValue; double[] minInput = null; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int pos = row * width + col; if (beginBuffer[pos] < 0 || !IsValid(pos)) { result[pos] = this.landUse.NullInfo.LandUseTypeValue; continue; } double[] input = (from buffer in driveBuffers select buffer[pos]).ToArray <double>(); double prop = regression.Probability(input); if (prop < minProp) { minProp = prop; minInput = input; } result[pos] = prop; } } return(result); }