public Data(List <Entry> r, StreamReader r2, double learning_rate, WeightBias bestWB, int forestSize)
        {
            ForestSize      = forestSize;
            data_1          = r;
            data_2          = new List <Entry>();
            AccuracyWeightB = new Dictionary <int, AccuracyWB>();
            Predictions     = new List <Prediction>();
            //SetData(r);

            perceptron    = new Perceptron(data_1, null, learning_rate, false, 0, null, false, 0, false, 0, false, ForestSize);
            Test_Accuracy = perceptron.GetAccuracy(data_1, bestWB);
            SetAccountIDs(r2, perceptron.Labels);
        }
Exemple #2
0
        public Data(int epochs, double learning_rate, double margin, double c, bool logistic_regression, double tradeoff, Random r,
                    List <Entry> train, List <Entry> test)
        {
            double temp_accuracy1;
            double temp_accuracy2;
            double temp_accuracy3;
            double temp_accuracy4;
            double temp_accuracy5;

            Learning_Rate       = learning_rate;
            C                   = c;
            Tradeoff            = tradeoff;
            Training_Data       = new List <Entry>();
            Test_Data           = new List <Entry>();
            Cross_Validate_Data = train.Concat(test).ToList();
            Cross_1             = new List <Entry>();
            Cross_2             = new List <Entry>();
            Cross_3             = new List <Entry>();
            Cross_4             = new List <Entry>();
            Cross_5             = new List <Entry>();
            SetValidateData(r);

            #region First Fold
            Training_Data = new List <Entry>();
            Test_Data     = new List <Entry>();
            Training_Data = Cross_1.Concat(Cross_2.Concat(Cross_3.Concat(Cross_4))).ToList();
            Test_Data     = Cross_5;

            perceptron = new Perceptron(Training_Data, Test_Data, learning_rate, margin, C, logistic_regression, Tradeoff, r);
            Dictionary <int, double> w = new Dictionary <int, double>();
            double b = (r.NextDouble() * (0.01 + 0.01) - 0.01);

            WeightBias wb = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy1 = perceptron.GetAccuracy(Test_Data, wb);
            #endregion

            #region Second Fold
            Training_Data = new List <Entry>();
            Test_Data     = new List <Entry>();

            Training_Data = Cross_1.Concat(Cross_2.Concat(Cross_3.Concat(Cross_5))).ToList();
            Test_Data     = Cross_4;

            perceptron = new Perceptron(Training_Data, Test_Data, learning_rate, margin, C, logistic_regression, Tradeoff, r);
            wb         = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy2 = perceptron.GetAccuracy(Test_Data, wb);
            #endregion

            #region Third Fold
            Training_Data = new List <Entry>();
            Test_Data     = new List <Entry>();

            Training_Data = Cross_1.Concat(Cross_2.Concat(Cross_4.Concat(Cross_5))).ToList();
            Test_Data     = Cross_3;

            perceptron = new Perceptron(Training_Data, Test_Data, learning_rate, margin, C, logistic_regression, Tradeoff, r);
            wb         = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy3 = perceptron.GetAccuracy(Test_Data, wb);
            #endregion

            #region Fourth Fold
            Training_Data = new List <Entry>();
            Test_Data     = new List <Entry>();

            Training_Data = Cross_1.Concat(Cross_3.Concat(Cross_4.Concat(Cross_5))).ToList();
            Test_Data     = Cross_2;

            perceptron = new Perceptron(Training_Data, Test_Data, learning_rate, margin, C, logistic_regression, Tradeoff, r);
            wb         = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy4 = perceptron.GetAccuracy(Test_Data, wb);
            #endregion

            #region Fifth Fold
            Training_Data = new List <Entry>();
            Test_Data     = new List <Entry>();

            Training_Data = Cross_2.Concat(Cross_3.Concat(Cross_4.Concat(Cross_5))).ToList();
            Test_Data     = Cross_1;

            perceptron = new Perceptron(Training_Data, Test_Data, learning_rate, margin, C, logistic_regression, Tradeoff, r);
            wb         = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy5 = perceptron.GetAccuracy(Test_Data, wb);
            #endregion

            Test_Accuracy = (temp_accuracy1 + temp_accuracy2 + temp_accuracy3 + temp_accuracy4 + temp_accuracy5) / 5;
        }
        public Data(List <Entry> train, List <Entry> test, int epochs, double learning_rate, Random r, bool DymanicLearningRate, double margin, bool Average, bool Aggressive,
                    double c, bool svm, double tradeoff, bool logistic_regression, int forestSize)
        {
            C                   = c;
            SVM                 = svm;
            Tradeoff            = tradeoff;
            Logistic_Regression = logistic_regression;
            ForestSize          = forestSize;

            double[]   w_average = new double[ForestSize];
            double     b_average;
            WeightBias wb_average = null;

            if (Average)
            {
                for (int i = 0; i < ForestSize; i++)
                {
                    double randomNumber = (r.NextDouble() * (0.01 + 0.01) - 0.01);
                    w_average[i] = randomNumber;
                }
                b_average  = (r.NextDouble() * (0.01 + 0.01) - 0.01);
                wb_average = new WeightBias(w_average, b_average, 0);
            }

            data_1          = train;
            data_2          = test;
            AccuracyWeightB = new Dictionary <int, AccuracyWB>();
            perceptron      = new Perceptron(data_1, data_2, learning_rate, DymanicLearningRate, margin, wb_average, Aggressive, C, SVM, Tradeoff, Logistic_Regression, ForestSize);
            double[] w = new double[ForestSize];
            double   b = (r.NextDouble() * (0.01 + 0.01) - 0.01);

            for (int i = 0; i < ForestSize; i++)
            {
                double randomNumber = (r.NextDouble() * (0.01 + 0.01) - 0.01);
                w[i] = randomNumber;
            }
            WeightBias wb = new WeightBias(w, b, 0);

            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                if (Average)
                {
                    perceptron.WeightBias_Average.Updates = wb.Updates;
                    AccuracyWeightB.Add(i + 1, new AccuracyWB(perceptron.GetAccuracy(data_2, perceptron.WeightBias_Average), perceptron.WeightBias_Average));
                }
                else
                {
                    AccuracyWeightB.Add(i + 1, new AccuracyWB(perceptron.GetAccuracy(data_2, wb), wb));
                }
                perceptron.ShuffleTraining_Data(r);
            }
            //foreach (var item in AccuracyWeightB)
            //{
            //    Console.WriteLine(item.Value.Accuracy);
            //}
            AccuracyWB bestAccuracy = AccuracyWeightB.OrderByDescending(x => x.Value.Accuracy).ThenByDescending(y => y.Key).Select(z => z.Value).First();


            Test_Accuracy  = bestAccuracy.Accuracy;
            BestWeightBias = bestAccuracy.Weight_Bias;
            Learning_Rate  = learning_rate;
            //Console.WriteLine("\n" + Accuracy);
        }
        public Data(int epochs, double learning_rate, Random r, bool DymanicLearningRate, double margin, bool Average, bool Aggressive,
                    double c, bool svm, double tradeoff, bool logistic_regression, List <Entry> train, List <Entry> test, int forestSize)
        {
            double temp_accuracy1;
            double temp_accuracy2;
            double temp_accuracy3;
            double temp_accuracy4;
            double temp_accuracy5;

            data_1 = new List <Entry>();
            data_2 = new List <Entry>();
            Cross_Validate_Data = train.Concat(test).ToList();
            Cross_1             = new List <Entry>();
            Cross_2             = new List <Entry>();
            Cross_3             = new List <Entry>();
            Cross_4             = new List <Entry>();
            Cross_5             = new List <Entry>();
            SetValidateData(null, null, r);

            C                   = c;
            SVM                 = svm;
            Tradeoff            = tradeoff;
            Logistic_Regression = logistic_regression;
            Learning_Rate       = learning_rate;
            Margin              = margin;
            ForestSize          = forestSize;

            double[]   w_average = new double[ForestSize];
            double     b_average;
            WeightBias wb_average = null;

            if (Average)
            {
                for (int i = 0; i < ForestSize; i++)
                {
                    double randomNumber = (r.NextDouble() * (0.01 + 0.01) - 0.01);
                    w_average[i] = randomNumber;
                }
                b_average  = (r.NextDouble() * (0.01 + 0.01) - 0.01);
                wb_average = new WeightBias(w_average, b_average, 0);
            }


            #region First Fold

            data_1 = Cross_1.Concat(Cross_2.Concat(Cross_3.Concat(Cross_4))).ToList();
            data_2 = Cross_5;

            perceptron = new Perceptron(data_1, data_2, learning_rate, DymanicLearningRate, margin, wb_average, Aggressive, C, SVM, Tradeoff, Logistic_Regression, ForestSize);
            double[] w = new double[ForestSize];
            double   b = (r.NextDouble() * (0.01 + 0.01) - 0.01);
            for (int i = 0; i < ForestSize; i++)
            {
                double randomNumber = (r.NextDouble() * (0.01 + 0.01) - 0.01);
                w[i] = randomNumber;
            }
            WeightBias wb = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy1 = perceptron.GetAccuracy(data_2, wb);
            if (Average)
            {
                temp_accuracy1 = perceptron.GetAccuracy(data_2, perceptron.WeightBias_Average);
            }
            #endregion

            #region Second Fold
            data_1 = new List <Entry>();
            data_2 = new List <Entry>();

            data_1 = Cross_1.Concat(Cross_2.Concat(Cross_3.Concat(Cross_5))).ToList();
            data_2 = Cross_4;

            perceptron = new Perceptron(data_1, data_2, learning_rate, DymanicLearningRate, margin, wb_average, Aggressive, C, SVM, Tradeoff, Logistic_Regression, ForestSize);
            wb         = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy2 = perceptron.GetAccuracy(data_2, wb);
            if (Average)
            {
                temp_accuracy2 = perceptron.GetAccuracy(data_2, perceptron.WeightBias_Average);
            }
            #endregion

            #region Third Fold
            data_1 = new List <Entry>();
            data_2 = new List <Entry>();

            data_1 = Cross_1.Concat(Cross_2.Concat(Cross_4.Concat(Cross_5))).ToList();
            data_2 = Cross_3;

            perceptron = new Perceptron(data_1, data_2, learning_rate, DymanicLearningRate, margin, wb_average, Aggressive, C, SVM, Tradeoff, Logistic_Regression, ForestSize);
            wb         = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy3 = perceptron.GetAccuracy(data_2, wb);
            if (Average)
            {
                temp_accuracy3 = perceptron.GetAccuracy(data_2, perceptron.WeightBias_Average);
            }
            #endregion

            #region Fourth Fold
            data_1 = new List <Entry>();
            data_2 = new List <Entry>();

            data_1 = Cross_1.Concat(Cross_3.Concat(Cross_4.Concat(Cross_5))).ToList();
            data_2 = Cross_2;

            perceptron = new Perceptron(data_1, data_2, learning_rate, DymanicLearningRate, margin, wb_average, Aggressive, C, SVM, Tradeoff, Logistic_Regression, ForestSize);
            wb         = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy4 = perceptron.GetAccuracy(data_2, wb);
            if (Average)
            {
                temp_accuracy4 = perceptron.GetAccuracy(data_2, perceptron.WeightBias_Average);
            }
            #endregion

            #region Fifth Fold
            data_1 = new List <Entry>();
            data_2 = new List <Entry>();

            data_1 = Cross_2.Concat(Cross_3.Concat(Cross_4.Concat(Cross_5))).ToList();
            data_2 = Cross_1;

            perceptron = new Perceptron(data_1, data_2, learning_rate, DymanicLearningRate, margin, wb_average, Aggressive, C, SVM, Tradeoff, Logistic_Regression, ForestSize);
            wb         = new WeightBias(w, b, 0);
            for (int i = 0; i < epochs; i++)
            {
                wb = perceptron.CalculateWB(wb);
                perceptron.ShuffleTraining_Data(r);
            }
            temp_accuracy5 = perceptron.GetAccuracy(data_2, wb);
            if (Average)
            {
                temp_accuracy5 = perceptron.GetAccuracy(data_2, perceptron.WeightBias_Average);
            }
            #endregion

            Test_Accuracy = (temp_accuracy1 + temp_accuracy2 + temp_accuracy3 + temp_accuracy4 + temp_accuracy5) / 5;
        }