示例#1
0
        private void OpenLearn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter           = "txt files (.txt)|*.txt";
            dlg.DefaultExt       = ".csv";
            dlg.RestoreDirectory = true;
            dlg.CheckFileExists  = true;
            dlg.CheckPathExists  = true;
            if (dlg.ShowDialog() != true)
            {
                return;
            }
            FileStream   stream = null;
            StreamReader file   = null;

            try
            {
                stream = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read, FileShare.None);
                file   = new StreamReader(stream, Encoding.Default);
                List <RegCoef> Buffer = new List <RegCoef>();
                while (!file.EndOfStream)
                {
                    RegCoef cur = new RegCoef()
                    {
                        Name = file.ReadLine()
                    };
                    cur.coef      = double.Parse(file.ReadLine());
                    cur.standCoef = double.Parse(file.ReadLine());
                    Buffer.Add(cur);
                }
                if (Buffer.Count < 3)
                {
                    throw new FormatException();
                }
                if (Buffer[Buffer.Count - 2].Name != "Intercept")
                {
                    throw new FormatException();
                }
                regression = new MultipleLinearRegression(Buffer.Count - 2, Buffer[Buffer.Count - 2].coef);
                List <double> Weights = new List <double>();
                for (int i = 0; i < Buffer.Count - 2; i++)
                {
                    Weights.Add(Buffer[i].coef);
                }
                regression.Weights             = Weights.ToArray();
                CoefficentOfDetermination.Text = $"{Buffer[Buffer.Count - 1].coef:F4}";
                Buffer.RemoveAt(Buffer.Count - 1);
                Buffer.RemoveAt(Buffer.Count - 1);
                TrainModel           = Buffer;
                SortedTrainModel     = new List <RegCoef>(TrainModel);
                CoefGrid.ItemsSource = null;
                CoefGrid.ItemsSource = SortedTrainModel;
            }
            catch (FormatException)
            {
                MessageBox.Show("В файле некорректные данные");
            }
            catch (Exception)
            {
                MessageBox.Show("Открыть файл не удалось");
            }
            finally
            {
                file?.Dispose();
                stream?.Dispose();
            }
        }
示例#2
0
        private void Train_Click(object sender, RoutedEventArgs e)
        {
            List <RegCoef> OldTrainModel = TrainModel;

            if (TrainData.Rows.Count < 1)
            {
                return;
            }
            try
            {
                double[][] inputs          = new double[TrainData.Rows.Count][];
                List <int> IndicesOfActive = new List <int>();
                for (int i = 1; i < NamesOfParameters.Count - 1; i++)
                {
                    if (!NamesOfParameters[i].Hidden)
                    {
                        IndicesOfActive.Add(i);
                    }
                }
                if (IndicesOfActive.Count < 1)
                {
                    return;
                }
                for (int i = 0; i < inputs.Length; i++)
                {
                    inputs[i] = new double[IndicesOfActive.Count];
                    for (int j = 0; j < inputs[i].Length; j++)
                    {
                        inputs[i][j] = double.Parse(TrainData.Rows[i][IndicesOfActive[j]].ToString(), CultureInfo.InvariantCulture);
                    }
                }
                double[] outputs = new double[inputs.Length];
                for (int i = 0; i < outputs.Length; i++)
                {
                    outputs[i] = double.Parse(TrainData.Rows[i][TrainData.Columns.Count - 1].ToString(), CultureInfo.InvariantCulture);
                }
                var ord = new OrdinaryLeastSquares()
                {
                    UseIntercept = true
                };
                regression = ord.Learn(inputs, outputs);
                CoefficentOfDetermination.Text = $"{regression.CoefficientOfDetermination(inputs, outputs, adjust: true):F4}";
                TrainModel = new List <RegCoef>();
                for (int i = 0; i < IndicesOfActive.Count; i++)
                {
                    double[] x = new double[outputs.Length];
                    for (int j = 0; j < inputs.Length; j++)
                    {
                        x[j] = inputs[j][i];
                    }
                    double scoef = regression.Weights[i] * (StandardDeviation(x) / StandardDeviation(outputs));
                    var    cur   = new RegCoef();
                    cur.Name      = NamesOfParameters[IndicesOfActive[i]].Name;
                    cur.StandCoef = $"{scoef:F4}";
                    cur.Coef      = $"{regression.Weights[i]:F4}";
                    TrainModel.Add(cur);
                }
                SortedTrainModel     = new List <RegCoef>(TrainModel);
                CoefGrid.ItemsSource = null;
                CoefGrid.ItemsSource = SortedTrainModel;
            }
            catch (FormatException)
            {
                MessageBox.Show("Данные должны представляться числами");
                TrainModel = OldTrainModel;
            }
            catch (Exception)
            {
                MessageBox.Show("Неизвестная ошибка");
                TrainModel = OldTrainModel;
            }
        }