コード例 #1
0
        static void Main(string[] args)
        {
            var trainingset   = new double[100, 4];
            var testset       = new double[20, 4];
            var testset_noans = new double[20, 3];

            for (int i = 0; i < 100; i++)
            {
                Random r = new Random();
                trainingset[i, 0] = r.Next(1, 3000);

                Random r1 = new Random();
                if (i % 2 == 1)
                {
                    trainingset[i, 3] = 1;
                    if (r1.Next(0, 100) < 80)
                    {
                        trainingset[i, 1] = 1;
                    }
                    else
                    {
                        trainingset[i, 1] = 0;
                    }
                }
                else
                {
                    trainingset[i, 3] = 0;
                    if (r1.Next(0, 100) < 50)
                    {
                        trainingset[i, 1] = 0;
                    }
                    else
                    {
                        trainingset[i, 1] = 1;
                    }
                }

                Random r2 = new Random();
                if (i % 2 == 1)
                {
                    trainingset[i, 2] = r2.Next(60, 100);
                }
                else
                {
                    trainingset[i, 2] = r2.Next(0, 60);
                }
            }

            for (int i = 0; i < 20; i++)
            {
                Random r = new Random();
                testset[i, 0]       = r.Next(1, 3000);
                testset_noans[i, 0] = testset[i, 0];

                Random r1 = new Random();
                if (i % 2 == 1)
                {
                    testset[i, 3] = 1;
                    if (r1.Next(0, 100) < 80)
                    {
                        testset[i, 1]       = 1;
                        testset_noans[i, 1] = testset[i, 1];
                    }
                    else
                    {
                        testset[i, 1]       = 0;
                        testset_noans[i, 1] = testset[i, 1];
                    }
                }
                else
                {
                    testset[i, 3] = 0;
                    if (r1.Next(0, 100) < 50)
                    {
                        testset[i, 1]       = 0;
                        testset_noans[i, 1] = testset[i, 1];
                    }
                    else
                    {
                        testset[i, 1]       = 1;
                        testset_noans[i, 1] = testset[i, 1];
                    }
                }

                Random r2 = new Random();
                if (i % 2 == 1)
                {
                    testset[i, 2]       = r2.Next(60, 100);
                    testset_noans[i, 2] = testset[i, 2];
                }
                else
                {
                    testset[i, 2]       = r2.Next(0, 60);
                    testset_noans[i, 2] = testset[i, 2];
                }
            }

            int NPoints  = 70; //     -   training set size, NPoints>=1
            int NVars    = 3;  //       -   number of independent variables, NVars>=1
            int NClasses = 2;
            int NTrees   = 20; //     -   number of trees in a forest, NTrees>=1.
                               // recommended values: 50-100.
            double R = 0.5;

            int info = 0;

            var forest      = new alglib.dforest.decisionforest();
            var report      = new alglib.dforest.dfreport();
            var prediction  = new double[20, 2];
            var prediction1 = new double[2];



            alglib.dforest.dfbuildrandomdecisionforest(trainingset, NPoints, NVars, NClasses, NTrees, R, ref info, forest, report);

            Console.WriteLine(info);

            for (int i = 0; i < 20; i++)
            {
                var test1 = new double[3];
                test1[0] = testset_noans[i, 0];
                test1[1] = testset_noans[i, 1];
                test1[2] = testset_noans[i, 2];
                alglib.dforest.dfprocess(forest, test1, ref prediction1);
                prediction[i, 0] = prediction1[0];
                prediction[i, 1] = prediction1[1];
            }


            Console.WriteLine("break");

            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("Result was " + testset[i, 3]);
                Console.WriteLine("Prediction is - zero: " + prediction[i, 0] + "    one: " + prediction[i, 1]);
            }

            Console.Write("");
            //int size = 6;
            //int[,] matrix = new int[size,size];
            //Random r = new Random();
            //for (int i = 0; i < size; i++)
            //{
            //    for (int j = 0; j < size; j++)
            //    {
            //        matrix[i, j] = r.Next(1,3000);
            //    }
            //}

            //Console.WriteLine("Initial cost");
            //for (int i = 0; i < size; i++)
            //{
            //    Console.Write("[");
            //    for (int j = 0; j < size -1; j++)
            //    {
            //        Console.Write(matrix[i,j] + ", ");
            //    }
            //    Console.Write(matrix[i, size-1]);
            //    Console.Write("]\n");
            //}

            //MatchingAlgorithm Algo = new MatchingAlgorithm(matrix);
            //int[,] mat = Algo.Run();

            //Console.WriteLine("Final assignment");
            //for (int i = 0; i < size; i++)
            //{
            //    Console.Write("[");
            //    for (int j = 0; j < size - 1; j++)
            //    {
            //        Console.Write(mat[i, j] + ", ");
            //    }
            //    Console.Write(mat[i, size - 1]);
            //    Console.Write("]\n");
            //}
            //Console.Read();
        }
コード例 #2
0
        public StatusModel <List <TraineeModel> > GeneratePrediction(Area?area)
        {
            var status = new StatusModel <List <TraineeModel> >(false, String.Empty, new List <TraineeModel>());

            try
            {
                using (var unitOfWork = new UnitOfWork <BelibaHomaDBEntities>())
                {
                    var traineeRepository            = unitOfWork.GetRepository <ITraineeRepository>();
                    var predictionTrainingRepository = unitOfWork.GetRepository <IPredictionTrainingRepository>();
                    var alertRepository = unitOfWork.GetRepository <IAlertRepository>();

                    var          trainingRawData = predictionTrainingRepository.GetAll().ToList();
                    int          NPoints         = trainingRawData.Count();
                    const int    NVars           = 7;
                    const int    NClasses        = 2;
                    const int    NTrees          = 50;
                    const double R      = 0.6;
                    int          info   = 0;
                    var          forest = new alglib.dforest.decisionforest();
                    var          report = new alglib.dforest.dfreport();

                    var trainingSet = new double[NPoints, 8];
                    for (int i = 0; i < NPoints; i++)
                    {
                        //Gender0,AcademicInstitution1,AcademicMajor2,AcademicMinor3,SemesterNumber4,SemesterGrade5,AlertCount6,IsDroppedOut7
                        trainingSet[i, 0] = trainingRawData[i].Gender;
                        trainingSet[i, 1] = trainingRawData[i].AcademicInstitution;
                        trainingSet[i, 2] = trainingRawData[i].AcademicMajor;
                        trainingSet[i, 3] = trainingRawData[i].AcademicMinor;
                        trainingSet[i, 4] = trainingRawData[i].SemesterNumber;
                        trainingSet[i, 5] = trainingRawData[i].LastSemesterGrade;
                        trainingSet[i, 6] = trainingRawData[i].AlertCount;
                        trainingSet[i, 7] = trainingRawData[i].IsDroppedOut;
                    }

                    alglib.dforest.dfbuildrandomdecisionforest(trainingSet, NPoints, NVars, NClasses, NTrees, R, ref info, forest, report);

                    if (info == 1)
                    {
                        var testRawData =
                            traineeRepository.GetQuery(
                                t => t.User.IsActive && (area == null || t.User.Area == (int?)area)).ToList();
                        int traineeCount       = testRawData.Count();
                        var endangeredTrainees = new List <TraineeModel>();

                        for (int i = 0; i < traineeCount; i++)
                        {
                            var trainee = testRawData[i];
                            //Gender0,AcademicInstitution1,AcademicMajor2,AcademicMinor3,SemesterNumber4,SemesterGrade5,AlertCount6
                            var testParameters = new double[7];
                            testParameters[0] = trainee.Gender;
                            testParameters[1] = trainee.AcademicInstitutionId;
                            testParameters[2] = trainee.AcademicMajorId;
                            testParameters[3] = (double)(trainee.AcademicMajor1 != null ? trainee.AcademicMinorId : -1);
                            testParameters[4] = trainee.SemesterNumber;
                            testParameters[5] = trainee.Grade.OrderByDescending(g => g.SemesterNumber).Select(g => g.Grade1).FirstOrDefault();
                            //testParameters[6] = (double) alertRepository.GetQuery(a => a.AlertType == (int)AlertType.Intervention && a.TutorReport.TutorTrainee.TraineeId == testRawData[i].UserId).Count();
                            testParameters[6] =
                                alertRepository.GetAll().Count(a => a.AlertType == (int)AlertType.Intervention &&
                                                               a.TutorReport.TutorTrainee.TraineeId == trainee.UserId);
                            var prediction = new double[NClasses];
                            alglib.dforest.dfprocess(forest, testParameters, ref prediction);
                            if (prediction[1] > 0.65)
                            {
                                endangeredTrainees.Add(new TraineeModel(trainee));
                            }
                        }

                        //If we got here - Yay! :)
                        status.Data    = endangeredTrainees;
                        status.Success = true;
                        status.Message = String.Format("חיזוי בוצע בהצלחה");
                        return(status);
                    }
                }
            }
            catch (Exception ex)
            {
                if (status.Message == String.Empty)
                {
                    status.Message = String.Format("שגיאה במהלך חיזוי חניכים בסיכון לנשירה");
                }
                LogService.Logger.Error(status.Message, ex);
            }
            return(status);
        }