Esempio n. 1
0
        static void Main(string[] args)
        {
            if (args.Length > 3 | args.Length < 1 | args.Length < 3)
            {
                Console.WriteLine("Requires a previously trained and saved Model File");
                Console.WriteLine("Usage <testfile> <label file> <Model File>");
                System.Environment.Exit(-1);
            }

            Console.WriteLine("Logisitic Regression Prediction\n");
            string testFname   = args[0];
            string labelsFname = args[1];
            string ModelFname  = args[2];


            double[,] Rawdata;
            double[,] labeldata;
            // Read in the test data, validate file existence by attempting to open the files first
            try
            {
                FileStream fs = File.Open(testFname, FileMode.Open, FileAccess.Write, FileShare.None);
                fs.Close();
                // Reuse fs for validating labels
                fs = File.Open(labelsFname, FileMode.Open, FileAccess.Write, FileShare.None);
                fs.Close();

                fs = File.Open(ModelFname, FileMode.Open, FileAccess.Read, FileShare.None);
                fs.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error opening file{0}", e);
                System.Environment.Exit(-1);
            }
            using (CsvReader reader = new CsvReader(testFname, hasHeaders: false))
            {
                Rawdata = reader.ToMatrix();
            }
            using (CsvReader reader = new CsvReader(labelsFname, hasHeaders: false))
            {
                labeldata = reader.ToMatrix();
            }

            // Convert Raw data to Jagged array
            double[][] testdata = Rawdata.ToJagged();
            int[]      output1  = funcs.convetToJaggedArray(labeldata);

            int [] answers = new int[labeldata.GetLength(0)];

            // For Accord.net Logistic Regression the input data needs to be in Jagged Arrays
            // Labels can either be int (1,0) or bools
            if (ModelFname.IndexOf("bfgs", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                // Load a BFGS regression model
                try
                {
                    MultinomialLogisticRegression mlr = Serializer.Load <MultinomialLogisticRegression>(ModelFname);
                    answers = mlr.Decide(testdata);
                } catch (Exception e)
                {
                    Console.WriteLine("Error opening model file: {0}", ModelFname);
                    Console.WriteLine("Exception {0}", e);
                    System.Environment.Exit(-1);
                }
            }


            else if (ModelFname.IndexOf("pcd", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                LogisticRegression regression = new LogisticRegression();
                try
                {
                    regression = Serializer.Load <LogisticRegression>(ModelFname);
                    answers    = funcs.BoolToInt(regression.Decide(testdata));
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error opening model file: {0}", ModelFname);
                    Console.WriteLine("Exception {0}", e);
                    System.Environment.Exit(-1);
                }
            }

            Console.WriteLine("Successfully loaded model file => {0}", ModelFname);

            double subtotal = 0;
            int    index    = 0;

            foreach (var result in answers)
            {
                if (result == output1[index])
                {
                    subtotal = subtotal + 1;
                }
                index++;
            }
            double accuracy = subtotal / answers.Count();

            Console.WriteLine("Predicted accuracy using model:{0} is, {1}", ModelFname, Math.Round(accuracy * 100, 2));
        }
Esempio n. 2
0
        //Calcuates the each user transaction
        private void CalculateButton_Click(object sender, EventArgs e)
        {   //variables to be used in calculating each user inputs
            int TotalAdults, TotalStud, TotalChild; decimal Totalsum;


            //Try and catch have been declared for each numerical inputs
            try
            {
                TotalAdults = int.Parse(NumAdults.Text); //Checks that adult tickets
                                                         //are numerical

                try
                {
                    TotalStud = int.Parse(NumStud.Text); //checks tickets entered
                                                         //for students are
                                                         //numerical


                    try
                    {
                        TotalChild = int.Parse(NumChild.Text); //checks that the
                                                               //child ticket
                                                               //inputs are also numerical


                        /* if the inputs for adults, student and
                         * children are numerical, it proceeds to perform
                         * the calculations
                         */

                        //Calculates the sum of the tickets
                        Totalsum = TotalAdults + TotalStud + TotalChild;
                        ListAmount.Add(Totalsum);//
                        TotalTicketSoldOutput.Text = Totalsum.ToString();
                        CashierSummary.Visible     = true;



                        decimal AmountAdults, AmountStud, Amountchild;
                        decimal Rawdata;

                        //this multiplies the data
                        AmountAdults = TotalAdults * (decimal)ADULT;
                        AmountStud   = TotalStud * (decimal)STUDENT;
                        Amountchild  = TotalChild * (decimal)CHILDREN;
                        Rawdata      = AmountAdults + AmountStud + Amountchild;
                        Listreceipts.Add(Rawdata);
                        TotalReciptsOutput.Text = Rawdata.ToString("C");

                        //this calculates the average
                        decimal AvgPricePaid;
                        AvgPricePaid            = Rawdata / Totalsum;
                        AvgPricePaidOutput.Text = AvgPricePaid.ToString("C");
                    }
                    catch { NumChild.ForeColor = System.Drawing.Color.Red;
                            MessageBox.Show("CHILD TICKETS\n\n" +
                                            "Only Numerical Input accepted\n Please change the " +
                                            "input in RED to a number"); }
                }
                catch { NumStud.ForeColor = System.Drawing.Color.Red;
                        MessageBox.Show("STUDENT TICKETS\n\n" +
                                        "Only Numerical Input accepted\n Please change the " +
                                        "input in RED to a number"); }
            }
            catch { NumAdults.ForeColor = System.Drawing.Color.Red;
                    MessageBox.Show("ADULT TICKETS\n\n" +
                                    "Only Numerical Input accepted\n Please change the " +
                                    "input in RED to a number"); }



            SummaryButton.Enabled = true;                   //enables summary button

            CashierNameOutput.Text = UserNameInputBox.Text; // receive user inputs
            CashierNames.Add(CashierNameOutput.Text);       //appends the cashier names
                                                            //to the list

            CalculateButton.Enabled = false;
            ClearButton.Enabled     = true;
        }
Esempio n. 3
0
        public Form3(int port, string ip, Image localpic, string localnick, int rounds)
        {
            win           = false;
            lose          = false;
            Turn          = false;
            firstTurn     = false;
            score         = 0;
            enemyScore    = 0;
            game          = true;
            this.rounds   = rounds;
            localNickName = localnick;
            localImage    = localpic;
            ConnectionResult connectionResult = ConnectionResult.TCPConnectionNotAlive;

            connection = ConnectionFactory.CreateTcpConnection(ip, port, out connectionResult);
            if (connectionResult == ConnectionResult.Connected)
            {
                MessageBox.Show("Connected", "connected");
            }
            InitializeComponent();
            pictureBox2.Image    = localImage;
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            label1.Text          = localnick;
            connection.SendRawData("Image", ImageConvert.image2Bytes(localImage));
            connection.SendRawData("Nick", Encoding.UTF8.GetBytes(localNickName));
            connection.RegisterRawDataHandler("image", (RawData, con) =>
            {
                enemyImage        = ImageConvert.bytes2Image(RawData.Data);
                pictureBox1.Image = enemyImage;
            });
            connection.RegisterRawDataHandler("nick", (RawData, con) =>
            {
                EnemyNickName = Encoding.UTF8.GetString(RawData.Data);
                label2.Text   = EnemyNickName;
            });
            connection.RegisterRawDataHandler("ready", (RawData, con) =>
            {
                enemyReady       = RawData.ToBoolean();
                label2.ForeColor = Color.Green;
            });
            connection.RegisterRawDataHandler("Turn", (RawData, con) =>
            {
                Turn = RawData.ToBoolean();
            });
            connection.RegisterRawDataHandler("firstTurn", (Rawdata, con) =>
            {
                firstTurn = Rawdata.ToBoolean();
                Turn      = true;
            });
            connection.RegisterRawDataHandler("MapUpdate", (RawData, con) =>
            {
                char[] newmap = RawData.ToUTF8String().ToArray();
                for (int i = 0; i < 9; i++)
                {
                    map[i] = int.Parse(newmap[i].ToString());
                }
                for (int i = 0; i < 9; i++)
                {
                    if (map[i] == 1)
                    {
                        panel1.Controls[i].BackgroundImage = Properties.Resources.Cross;
                    }
                    else if (map[i] == 2)
                    {
                        panel1.Controls[i].BackgroundImage = Properties.Resources.Circle;
                    }
                }
            });
            connection.RegisterRawDataHandler("win", (RawData, Con) =>
            {
                score      += 1;
                label3.Text = score.ToString();
                ClearMap();
            });
            connection.RegisterRawDataHandler("lose", (RawData, Con) =>
            {
                enemyScore += 1;
                label4.Text = enemyScore.ToString();
                ClearMap();
            });
            label1.ForeColor = Color.Red;
            label2.ForeColor = Color.Red;
        }