Exemplo n.º 1
0
        static void Main(string[] args)
        {
            NeuralNetwork network = new NeuralNetwork();

            network.Add(new NeuralLayer(2, 2, "sigmoid"));
            network.Add(new NeuralLayer(2, 1, "sigmoid"));


            double[] x1             = { 0.0, 0.0 };
            double[] x2             = { 0.0, 1.0 };
            double[] x3             = { 1.0, 0.0 };
            double[] x4             = { 1.0, 1.0 };
            List <List <double> > X = new List <List <double> >()
            {
                x1.ToList(), x2.ToList(), x3.ToList(), x4.ToList()
            };

            double[] y1             = { 0.0 };
            double[] y2             = { 1.0 };
            double[] y3             = { 1.0 };
            double[] y4             = { 0.0 };
            List <List <double> > Y = new List <List <double> >()
            {
                y1.ToList(), y2.ToList(), y3.ToList(), y4.ToList()
            };

            network.fit(X, Y, 0.1, 0.9, 10000);
            Console.WriteLine(network.predict(x1.ToList())[0]);
            Console.WriteLine(network.predict(x2.ToList())[0]);
            Console.WriteLine(network.predict(x3.ToList())[0]);
            Console.WriteLine(network.predict(x4.ToList())[0]);
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public static void obuciMrezu()
        {
            network = new NeuralNetwork();
            network.Add(new NeuralLayer(100, 100, "sigmoid"));
            network.Add(new NeuralLayer(100, 1, "sigmoid"));

            ucitajObucavajuciSkup();
            network.fit(ulazi, izlazi, 0.1, 0.9, 10);
            List <string> lines = new List <string>();

            for (int d = 0; d < 200; d++)
            {
                lines.Add(greskeZeljenaDobijena[d].ToString());
            }

            System.IO.File.WriteAllLines(@"..\..\..\..\grad.txt", lines);

            //Console.WriteLine("Mreza obucena.");
        }
        static void Main(string[] args)
        {
            DataManipulation dm = new DataManipulation();

            dm.readDataFromDataSet();

            NeuralNetwork network = new NeuralNetwork();

            network.Add(new NeuralLayer(6, 3, "sigmoid"));
            network.Add(new NeuralLayer(3, 2, "sigmoid"));
            network.Add(new NeuralLayer(2, 1, "sigmoid"));


            Console.WriteLine("Obuka pocela.");
            network.fit(dm.train, dm.trainY, 0.1, 0.9, 500);
            Console.WriteLine("Kraj obuke.");

            int pogodak = 0;

            for (int i = 0; i < dm.test.Count; ++i)
            {
                List <Double> prediction = network.predict(dm.test[i]);
                double        alive      = 0;
                if (prediction[0] > 0.5)
                {
                    alive = 1;
                }

                Console.WriteLine("Real result:{0}, Predicted result {1}", dm.testY[i][0], prediction[0]);

                if (alive == dm.testY[i][0])
                {
                    ++pogodak;
                }
            }

            Console.Write("Pogodjeno {0} od {1} ", pogodak, dm.testY.Count);
            Console.Write("Tacnost {0} %", pogodak * 100 / dm.testY.Count);
            Console.ReadLine();
        }
Exemplo n.º 4
0
        public void FitNeuralNetwork()
        {
            for (int j = 0; j < 6; j++)
            {
                string[] lines = File.ReadAllLines(DataSet);
                lines = lines.Skip(1).ToArray();

                List <List <double> > X = new List <List <double> >();
                List <List <double> > Y = new List <List <double> >();

                foreach (String line in lines)
                {
                    string[]      tokens = line.Split(',');
                    List <double> row    = new List <double>();
                    for (int i = 0; i < 20; i++)
                    {
                        row.Add(double.Parse(tokens[i]));
                    }

                    X.Add(row);

                    List <double> resultRow = new List <double>();
                    resultRow.Add(double.Parse(tokens[20 + j]));
                    Y.Add(resultRow);
                }

                NeuralNetwork network = new NeuralNetwork();
                network.Add(new NeuralLayer(20, 2, "sigmoid"));
                network.Add(new NeuralLayer(2, 1, "sigmoid"));

                network.fit(X, Y, 0.1, 0.9, 500);

                networks.Add(network);
            }

            var jsonString = JsonSerializer.Serialize(networks);

            File.WriteAllText(FileName, jsonString);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            #region Biranje kolona za ucitavanje

            List <int> indeksKolonaInputa = new List <int>();

            /*
             *  lotArea [4]
             *  yearBuilt [19]
             *  Neighborhood [12]
             *  Bldgtype [15]
             */
            indeksKolonaInputa.Add(4);
            indeksKolonaInputa.Add(19);
            indeksKolonaInputa.Add(12);
            indeksKolonaInputa.Add(15);

            List <int> indeksKolonaOutputa = new List <int>();

            // SalePrice[80]
            indeksKolonaOutputa.Add(80);

            #endregion

            int velicinaBlokaValidacije = 25;

            // Blok predstavlja velicinu bloka u cross validaciji koju uzimamo za test podatke
            for (int blok = 0; blok <= 75; blok += velicinaBlokaValidacije)
            {
                FileDAO fileDAO = new FileDAO(indeksKolonaInputa, indeksKolonaOutputa, blok, blok + 25);



                #region Kreiranje neuronske mreze

                NeuralNetwork network = new NeuralNetwork();
                network.Add(new NeuralLayer(indeksKolonaInputa.Count, 2, "sigmoid"));
                network.Add(new NeuralLayer(2, 1, "sigmoid"));

                #endregion

                #region Vektori za inpute

                List <List <double> > X = new List <List <double> >();
                X = fileDAO.X;

                #endregion

                #region Vektori za outpute

                List <List <double> > Y = new List <List <double> >();
                Y = fileDAO.Y;

                #endregion

                #region Fitovanje

                Console.WriteLine("Obuka pocela.");
                network.fit(X, Y, 0.1, 0.9, 500);
                Console.WriteLine("Kraj obuke.");

                #endregion

                #region Predikcija

                int ukupnoPogodjenih = 0;
                for (int i = 0; i < fileDAO.XTest.Count; ++i)
                {
                    List <Double> prediction = network.predict(fileDAO.XTest[i]);

                    //Console.WriteLine("Pravi rezultat u test podacima je: {0}, Po proracunu i predikciji dobijam {1}", fileDAO.YTest[i][0], prediction[0]);


                    // Podesavam koliko tolerisem da cena ide gore dole [ posto sam u domenu [0-1] normalizovao , 0.1 je 10% , 0.2 je 20 %
                    // voditi racuna da ako stavimo 0.2 da ce on tolerisati i za gore i za dole po 20%, sto je 40 % tolerancije
                    double tolerancija = 0.1;

                    if (prediction[0] <= fileDAO.YTest[i][0] + tolerancija && prediction[0] >= fileDAO.YTest[i][0] - tolerancija)
                    {
                        ++ukupnoPogodjenih;
                    }
                }

                Console.Write("Pogodjeno {0} od {1} ", ukupnoPogodjenih, fileDAO.YTest.Count);
                Console.Write("Tacnost {0} % \n\n", ukupnoPogodjenih * 100 / fileDAO.YTest.Count);

                #endregion
            }

            Console.ReadLine();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            //Kreiranje neuronske mreze
            NeuralNetwork network = new NeuralNetwork();

            network.Add(new NeuralLayer(6, 3, "sigmoid"));
            network.Add(new NeuralLayer(3, 2, "sigmoid"));
            network.Add(new NeuralLayer(2, 1, "sigmoid"));

            List <int>               pol               = new List <int>();
            List <double>            popularnost       = new List <double>();
            List <int>               brojPojavljivanja = new List <int>();
            List <int>               isNoble           = new List <int>();
            List <int>               ubijenoUOkolini   = new List <int>();
            List <int>               pripadaPorodici   = new List <int>();
            List <int>               isAlive           = new List <int>();
            Dictionary <String, int> houseMap          = new Dictionary <String, int>();

            String[] dataLines = File.ReadAllLines(@"./../../dataset.csv");

            dataLines = dataLines.Skip(1).ToArray(); //Skipujemo heder u CSV fajlu



            foreach (String linija in dataLines)
            {
                string[] parts = linija.Split(',');
                pol.Add(int.Parse(parts[(int)Header.Male]));
                popularnost.Add(ConvertToDouble(parts[(int)Header.Popularity]));


                int firstBook  = int.Parse((parts[(int)Header.book1]));
                int secondBook = int.Parse((parts[(int)Header.book2]));
                int thirdBook  = int.Parse((parts[(int)Header.book3]));
                int fourthBook = int.Parse((parts[(int)Header.book4]));
                int fifthBook  = int.Parse((parts[(int)Header.book5]));

                int brojPojava = firstBook + secondBook + thirdBook + fourthBook + fifthBook;

                brojPojavljivanja.Add(brojPojava);


                isNoble.Add(int.Parse(parts[(int)Header.isNobleD]));
                ubijenoUOkolini.Add(int.Parse(parts[(int)Header.numDeathRelations]));

                String porodica = parts[(int)Header.house];

                if (!porodica.Equals(""))
                {
                    if (!houseMap.ContainsKey(porodica))
                    {
                        houseMap.Add(porodica, houseMap.Count);

                        pripadaPorodici.Add(houseMap.Count);
                    }
                    else
                    {
                        pripadaPorodici.Add(houseMap[porodica]);
                    }
                }
                else
                {
                    pripadaPorodici.Add(houseMap.Count);
                }

                isAlive.Add(int.Parse((parts[(int)Header.isAliveD])));
            }
            //Noramalizujemo podatke

            List <double> brojPojavljivanjaNormalizovani = normalizacijaInt(brojPojavljivanja);
            List <double> ubijenoUOkoliniNormalizovani   = normalizacijaInt(ubijenoUOkolini);
            List <double> pripadaPorodiciNormalizovani   = normalizacijaInt(pripadaPorodici);


            List <List <double> > X = new List <List <double> >();
            List <List <double> > Y = new List <List <double> >();

            // prolazimo kroz sve likove
            for (int i = 389; i < 1946; i++)
            {
                double[] xTemp = { pol[i], popularnost[i], brojPojavljivanjaNormalizovani[i], isNoble[i], ubijenoUOkoliniNormalizovani[i], pripadaPorodiciNormalizovani[i] };
                X.Add(xTemp.ToList());


                double[] yTemp = { (double)isAlive[i] };
                Y.Add(yTemp.ToList());
            }

            Console.WriteLine("Obuka pocela");
            network.fit(X, Y, 0.1, 0.9, 500);

            Console.WriteLine("Gotova obuka");

            double dobrih = 0;

            for (int i = 0; i < 389; i++)
            {
                double[] x1       = { pol[i], popularnost[i], brojPojavljivanjaNormalizovani[i], isNoble[i], ubijenoUOkoliniNormalizovani[i], pripadaPorodiciNormalizovani[i] };
                int      iAmAlive = -1;
                if (network.predict(x1.ToList())[0] < 0.5)
                {
                    iAmAlive = 0;
                }
                else
                {
                    iAmAlive = 1;
                }
                if (isAlive[i] == iAmAlive)
                {
                    dobrih++;
                }
            }
            Console.WriteLine("Dobrih: " + dobrih + "/389");
            double procenattacnosti = (dobrih / 389) * 100;

            Console.WriteLine("Tačnost je = " + procenattacnosti + " %");
            Console.ReadLine();



            List <double> normalizacijaInt(List <int> listaVrednosti)
            {
                List <double> noramlizovanaLista = new List <double>();

                double dataMax = listaVrednosti.Max();
                double dataMin = listaVrednosti.Min();

                //  Console.WriteLine("Maksimalna vrendost je" + dataMax);
                //Console.WriteLine("Minimalna vrendost je" + dataMin);

                foreach (double vrednost in listaVrednosti)
                {
                    noramlizovanaLista.Add((vrednost - dataMin) / (dataMax - dataMin));
                    // Console.WriteLine("Minimalna vrendost je" + (vrednost - dataMin) / (dataMax - dataMin));
                }
                return(noramlizovanaLista);
            }

            double ConvertToDouble(string s)
            {
                char   systemSeparator = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator[0];
                double result          = 0;

                try
                {
                    if (s != null)
                    {
                        if (!s.Contains(","))
                        {
                            result = double.Parse(s, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            result = Convert.ToDouble(s.Replace(".", systemSeparator.ToString()).Replace(",", systemSeparator.ToString()));
                        }
                    }
                }
                catch (Exception e)
                {
                    try
                    {
                        result = Convert.ToDouble(s);
                    }
                    catch
                    {
                        try
                        {
                            result = Convert.ToDouble(s.Replace(",", ";").Replace(".", ",").Replace(";", "."));
                        }
                        catch
                        {
                            throw new Exception("Wrong string-to-double format");
                        }
                    }
                }
                return(result);
            }
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            //Kreiranje neuronske mreze
            NeuralNetwork network = new NeuralNetwork();

            network.Add(new NeuralLayer(4, 3, "sigmoid"));
            network.Add(new NeuralLayer(3, 2, "sigmoid"));
            network.Add(new NeuralLayer(2, 1, "sigmoid"));


            List <double> col_1 = new List <double>();
            List <double> col_2 = new List <double>();
            List <double> col_3 = new List <double>();
            List <double> col_4 = new List <double>();
            List <double> col_5 = new List <double>();

            List <double> col_1Test = new List <double>();
            List <double> col_2Test = new List <double>();
            List <double> col_3Test = new List <double>();
            List <double> col_4Test = new List <double>();
            List <double> col_5Test = new List <double>();


            String[] dataLines = File.ReadAllLines(@"./../../train.csv");

            dataLines = dataLines.Skip(1).ToArray(); //Skipujemo heder u CSV fajlu

            foreach (String linija in dataLines)
            {
                string[] parts = linija.Split(',');
                col_1.Add(double.Parse(parts[(int)Header.col_1], CultureInfo.InvariantCulture));
                col_2.Add(double.Parse(parts[(int)Header.col_2], CultureInfo.InvariantCulture));
                col_3.Add(double.Parse(parts[(int)Header.col_3], CultureInfo.InvariantCulture));
                col_4.Add(double.Parse(parts[(int)Header.col_4], CultureInfo.InvariantCulture));

                String type = (parts[(int)Header.col_5]);
                if (type.Equals("type_1"))
                {
                    col_5.Add(0);
                }
                else if (type.Equals("type_2"))
                {
                    col_5.Add(0.5);
                }
                else
                {
                    col_5.Add(1);
                }
            }

            List <double> col_1Normalizovani = normalizacijaInt(col_1);
            List <double> col_2Normalizovani = normalizacijaInt(col_2);
            List <double> col_3Normalizovani = normalizacijaInt(col_3);
            List <double> col_4Normalizovani = normalizacijaInt(col_4);


            List <List <double> > X = new List <List <double> >();
            List <List <double> > Y = new List <List <double> >();

            for (int i = 0; i < dataLines.Length; i++)
            {
                double[] xTemp = { col_1Normalizovani[i], col_2Normalizovani[i], col_3Normalizovani[i], col_4Normalizovani[i] };
                X.Add(xTemp.ToList());


                double[] yTemp = { col_5[i] };
                Y.Add(yTemp.ToList());
            }

            Console.WriteLine("Training started");
            network.fit(X, Y, 0.1, 0.9, 500);
            Console.WriteLine("Training ended");

            // String[] dataLinesTest = File.ReadAllLines(@"./../../test.csv");
            String[] dataLinesTest = File.ReadAllLines(@"./../../test.csv");
            dataLinesTest = dataLinesTest.Skip(1).ToArray(); //Skipujemo heder u CSV fajlu



            foreach (String linija in dataLinesTest)
            {
                string[] parts = linija.Split(',');

                col_1Test.Add(double.Parse(parts[(int)Header.col_1], CultureInfo.InvariantCulture));
                col_2Test.Add(double.Parse(parts[(int)Header.col_2], CultureInfo.InvariantCulture));
                col_3Test.Add(double.Parse(parts[(int)Header.col_3], CultureInfo.InvariantCulture));
                col_4Test.Add(double.Parse(parts[(int)Header.col_4], CultureInfo.InvariantCulture));
                //  Console.WriteLine(double.Parse(parts[(int)Header.col_4], CultureInfo.InvariantCulture));
                String type = (parts[(int)Header.col_5]);
                if (type.Equals("type_1"))
                {
                    col_5Test.Add(0);
                }
                else if (type.Equals("type_2"))
                {
                    col_5Test.Add(0.5);
                }
                else
                {
                    col_5Test.Add(1);
                }
            }
            List <double> col_1TestNormalizovani = normalizacijaInt(col_1Test);
            List <double> col_2TestNormalizovani = normalizacijaInt(col_2Test);
            List <double> col_3TestNormalizovani = normalizacijaInt(col_3Test);
            List <double> col_4TestNormalizovani = normalizacijaInt(col_4Test);

            /*  foreach (double d in col_4TestNormalizovani)
             * {
             *    Console.WriteLine(d);
             * }*/
            double dobrih = 0;
            double broj   = 0;

            for (int i = 0; i < dataLinesTest.Length; i++)
            {
                double[] x1   = { col_1TestNormalizovani[i], col_2TestNormalizovani[i], col_3TestNormalizovani[i], col_4TestNormalizovani[i] };
                double   type = 0;
                if (network.predict(x1.ToList())[0] < 0.25)
                {
                    type = 0;
                }
                else if (network.predict(x1.ToList())[0] > 0.25 && network.predict(x1.ToList())[0] < 0.75)
                {
                    type = 0.5;
                }
                else
                {
                    type = 1;
                }
                if (col_5Test[i] == type)
                {
                    dobrih++;
                }
                Console.WriteLine("PRediktovana vrednost je " + network.predict(x1.ToList())[0]);
                Console.WriteLine("Stvarna vrednost je " + col_5Test[i]);
                broj++;
            }

            Console.WriteLine("Dobro prediktovanih je  " + dobrih + " od " + broj);
            double procenattacnosti = (dobrih / broj) * 100;

            Console.WriteLine("Tačnost je = " + procenattacnosti + " %");
            Console.ReadLine();



            // Pomocne funckije
            List <double> normalizacijaInt(List <double> listaVrednosti)
            {
                List <double> noramlizovanaLista = new List <double>();

                double dataMax = listaVrednosti.Max();
                double dataMin = listaVrednosti.Min();

                foreach (double vrednost in listaVrednosti)
                {
                    noramlizovanaLista.Add((vrednost - dataMin) / (dataMax - dataMin));
                    // Console.WriteLine("Minimalna vrendost je" + (vrednost - dataMin) / (dataMax - dataMin));
                }
                return(noramlizovanaLista);
            }
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            string[] lines;
            lines = File.ReadAllLines(@"./../../data/heart.csv");
            lines = lines.Skip(1).ToArray();

            List <int>    age               = new List <int>();
            List <int>    pol               = new List <int>();
            List <int>    chestPain         = new List <int>();
            List <int>    RestingBloodP     = new List <int>();
            List <int>    cholesterol       = new List <int>();
            List <int>    fastingBloodSugar = new List <int>();
            List <int>    restingECG        = new List <int>();
            List <int>    thalach           = new List <int>();
            List <int>    excerciseAngina   = new List <int>();
            List <double> oldPeak           = new List <double>();
            List <int>    slope             = new List <int>();
            List <int>    ca       = new List <int>();
            List <int>    thai     = new List <int>();
            List <int>    isTarget = new List <int>();


            Dictionary <String, int> myMap = new Dictionary <String, int>();

            foreach (string line in lines)
            {
                string[] parts = line.Split(',');
                age.Add(int.Parse((parts[(int)HeaderDisese.Age])));
                pol.Add(int.Parse((parts[(int)HeaderDisese.Sex])));
                chestPain.Add(int.Parse((parts[(int)HeaderDisese.Cp])));
                RestingBloodP.Add(int.Parse((parts[(int)HeaderDisese.TrestBps])));
                cholesterol.Add(int.Parse((parts[(int)HeaderDisese.Chol])));
                fastingBloodSugar.Add(int.Parse((parts[(int)HeaderDisese.Fbs])));
                restingECG.Add(int.Parse((parts[(int)HeaderDisese.Restecg])));
                thalach.Add(int.Parse((parts[(int)HeaderDisese.Thalach])));
                excerciseAngina.Add(int.Parse((parts[(int)HeaderDisese.Exang])));
                oldPeak.Add(double.Parse((parts[(int)HeaderDisese.Oldpeak])));
                slope.Add(int.Parse((parts[(int)HeaderDisese.Slope])));
                ca.Add(int.Parse((parts[(int)HeaderDisese.Ca])));
                thai.Add(int.Parse((parts[(int)HeaderDisese.Thal])));
                isTarget.Add(int.Parse((parts[(int)HeaderDisese.Target])));
            }


            NeuralNetwork network = new NeuralNetwork();

            // Two activation functions are supported
            // sigmoid
            // relu
            network.Add(new NeuralLayer(13, 7, "relu"));
            network.Add(new NeuralLayer(7, 4, "relu"));
            network.Add(new NeuralLayer(4, 2, "relu"));
            network.Add(new NeuralLayer(2, 1, "relu"));

            List <List <double> > X = new List <List <double> >();
            List <List <double> > Y = new List <List <double> >();

            // 303 - total
            // 0-30 - test case
            // 30-270 - training data
            // 270-303 - test case
            for (int i = 30; i < 270; i++)
            {
                double[] xTemp = { (double)age[i], (double)pol[i], (double)chestPain[i], (double)RestingBloodP[i], (double)cholesterol[i], (double)fastingBloodSugar[i], (double)restingECG[i], (double)thalach[i], (double)excerciseAngina[i], oldPeak[i], (double)slope[i], (double)ca[i], (double)thai[i] };
                X.Add(xTemp.ToList());


                double[] yTemp = { (double)isTarget[i] };
                Y.Add(yTemp.ToList());
            }

            Console.WriteLine("Training started...");
            network.fit(X, Y, 0.1, 0.9, 500);
            Console.WriteLine("Training finished");

            int hit = 0;

            for (int i = 0; i < 303; i++)
            {
                if (i > 29 && i < 270)
                {
                    continue;
                }
                double[] x1      = { age[i], pol[i], chestPain[i], RestingBloodP[i], cholesterol[i], fastingBloodSugar[i], restingECG[i], thalach[i], excerciseAngina[i], oldPeak[i], slope[i], ca[i], thai[i] };
                int      predict = -1;
                if (network.predict(x1.ToList())[0] < 0.5)
                {
                    predict = 0;
                }
                else
                {
                    predict = 1;
                }
                if (isTarget[i] == predict)
                {
                    hit++;
                }
            }
            Console.WriteLine("Result: " + hit + "/60");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            #region Ucitavanje data seta

            List <int> indeksKolonaInputa = new List <int>();

            // ulazi od col_1 do col_5
            indeksKolonaInputa.Add(1);
            indeksKolonaInputa.Add(2);
            indeksKolonaInputa.Add(3);
            indeksKolonaInputa.Add(4);

            List <int> indeksKolonaOutputa = new List <int>();
            // col_5 izlaz
            indeksKolonaOutputa.Add(5);

            FileDAO fileDAO = new FileDAO(indeksKolonaInputa, indeksKolonaOutputa, 0);
            // fileDAO.imaKategorickihAtributa
            // fileDAO.getKategorickeAtribute

            #endregion

            #region Kreiranje neuronske mreze

            NeuralNetwork network = new NeuralNetwork();
            network.Add(new NeuralLayer(indeksKolonaInputa.Count, 2, "sigmoid"));
            //network.Add(new NeuralLayer(3, 2, "sigmoid"));
            network.Add(new NeuralLayer(2, 1, "sigmoid"));

            #endregion

            #region Vektori za inpute

            List <List <double> > X = new List <List <double> >();
            X = fileDAO.X;

            #endregion

            #region Vektori za outpute

            List <List <double> > Y = new List <List <double> >();
            Y = fileDAO.Y;

            #endregion

            #region Fitovanje

            Console.WriteLine("Obuka pocela.");
            network.fit(X, Y, 0.1, 0.9, 3000);
            Console.WriteLine("Kraj obuke.");

            #endregion

            #region Predikcija

            int ukupnoPogodjenih = 0;
            for (int i = 0; i < fileDAO.XTest.Count; ++i)
            {
                List <Double> prediction = network.predict(fileDAO.XTest[i]);

                Console.WriteLine("Pravi rezultat u test podacima je: {0}, Po proracunu i predikciji dobijam {1}", fileDAO.YTest[i][0], prediction[0]);
                // pravi rezultat mi moze biti 0, 0.5 ili 1 pa onda ove sto sam dobio, moram pretvoriti u tip
                // i proveriti da li su odgovarajuceg tipa

                double tip = -1;
                if (prediction[0] <= 0.33)
                {
                    tip = 0;
                }
                else if (prediction[0] > 0.33 && prediction[0] <= 0.66)
                {
                    tip = 0.5;
                }
                else
                {
                    tip = 1;
                }

                if (fileDAO.YTest[i][0] == tip)
                {
                    ++ukupnoPogodjenih;
                }
            }

            Console.Write("Pogodjeno {0} od {1} ", ukupnoPogodjenih, fileDAO.YTest.Count);
            Console.Write("Tacnost {0} %", ukupnoPogodjenih * 100 / fileDAO.YTest.Count);
            Console.ReadLine();

            #endregion
        }
        static void Main(string[] args)
        {
            #region Ucitavanje data seta

            List <int> indeksKolonaInputa = new List <int>();

            // popularity
            indeksKolonaInputa.Add(31);

            //male
            indeksKolonaInputa.Add(7);

            // books
            indeksKolonaInputa.Add(16);
            indeksKolonaInputa.Add(17);
            indeksKolonaInputa.Add(18);
            indeksKolonaInputa.Add(19);
            indeksKolonaInputa.Add(20);

            // isNoble
            indeksKolonaInputa.Add(26);

            // numDeadRelations
            indeksKolonaInputa.Add(28);

            // house
            indeksKolonaInputa.Add(14);

            List <int> indeksKolonaOutputa = new List <int>();
            // isAlive
            indeksKolonaOutputa.Add(32);

            FileDAO fileDAO = new FileDAO(indeksKolonaInputa, indeksKolonaOutputa, 20);

            #endregion

            #region Vektori za inpute

            List <List <double> > X = new List <List <double> >();
            X = fileDAO.X;

            #endregion

            #region Vektori za outpute

            List <List <double> > Y = new List <List <double> >();
            Y = fileDAO.Y;

            #endregion

            #region Kreiranje neuronske mreze

            NeuralNetwork network = new NeuralNetwork();
            network.Add(new NeuralLayer(X[0].Count, 2, "sigmoid")); // X[0], ali moze i X[bilo koji idx], bitno je da tako dobijem broj ulaza
            network.Add(new NeuralLayer(2, 1, "sigmoid"));

            #endregion

            #region Fitovanje

            Console.WriteLine("Obuka pocela.");
            network.fit(X, Y, 0.1, 0.9, 500);
            Console.WriteLine("Kraj obuke.");

            #endregion

            #region Skor koji postize mreza

            Score score = new Score();
            score.giveMeScore(fileDAO, network);

            #endregion
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            NeuralNetwork network = new NeuralNetwork();

            network.Add(new NeuralLayer(4, 2, "sigmoid"));
            network.Add(new NeuralLayer(2, 1, "sigmoid"));

            List <int> lotArea   = new List <int>();
            List <int> yearBuilt = new List <int>();
            Dictionary <String, int> mapNeighbourhood = new Dictionary <String, int>();
            List <int>    listNeighbourhood           = new List <int>();
            List <double> listBldgType = new List <double>();//5 razlicitih vrsta
            List <int>    salePrice    = new List <int>();

            String[] dataLines = File.ReadAllLines(@"./../../train.csv");
            dataLines = dataLines.Skip(1).ToArray();

            foreach (String linija in dataLines)
            {
                string[] parts = linija.Split(',');
                lotArea.Add(int.Parse(parts[(int)Header.LotArea]));
                yearBuilt.Add(int.Parse(parts[(int)Header.YearBuilt]));

                String neighbourhood = parts[(int)Header.Neighborhood];
                if (!neighbourhood.Equals(""))
                {
                    if (!mapNeighbourhood.ContainsKey(neighbourhood))
                    {
                        mapNeighbourhood.Add(neighbourhood, mapNeighbourhood.Count);
                        listNeighbourhood.Add(mapNeighbourhood.Count);
                    }
                    else
                    {
                        listNeighbourhood.Add(mapNeighbourhood[neighbourhood]);
                    }
                }
                else
                {
                    listNeighbourhood.Add(mapNeighbourhood.Count);
                }

                //listBldgType
                String BldgType = parts[(int)Header.BldgType];
                if (BldgType.Equals("1Fam"))
                {
                    listBldgType.Add(0);
                }
                else if (BldgType.Equals("2fmCon"))
                {
                    listBldgType.Add(0.25);
                }
                else if (BldgType.Equals("Duplex"))
                {
                    listBldgType.Add(0.5);
                }
                else if (BldgType.Equals("Twnhs"))
                {
                    listBldgType.Add(0.75);
                }
                else if (BldgType.Equals("TwnhsE"))
                {
                    listBldgType.Add(1);
                }

                salePrice.Add(int.Parse(parts[(int)Header.SalePrice]));
            }
            List <double> lotAreaNormalizovani           = normalizacijaInt(lotArea);
            List <double> yearBuiltNormalizovani         = normalizacijaInt(yearBuilt);
            List <double> salePriceNormalizovani         = normalizacijaInt(salePrice);
            List <double> listNeighbourhoodNormalizovani = normalizacijaInt(listNeighbourhood);

            List <List <double> > X = new List <List <double> >();
            List <List <double> > Y = new List <List <double> >();

            // prolazimo kroz sve likove
            for (int i = 0; i < 1167; i++)
            {
                double[] xTemp = { lotAreaNormalizovani[i], yearBuiltNormalizovani[i], listNeighbourhoodNormalizovani[i], listBldgType[i] };
                X.Add(xTemp.ToList());


                double[] yTemp = { (double)salePriceNormalizovani[i] };
                Y.Add(yTemp.ToList());
            }

            Console.WriteLine("Obuka pocela");
            network.fit(X, Y, 0.1, 0.9, 250);

            Console.WriteLine("Gotova obuka");

            int tacnoPrediktovanih = 0;
            int brojPrediktovanih  = 0;

            for (int i = 1167; i < 1459; i++)
            {
                brojPrediktovanih++;
                double[] x1             = { lotAreaNormalizovani[i], yearBuiltNormalizovani[i], listNeighbourhoodNormalizovani[i], listBldgType[i] };
                double   predictedPrice = network.predict(x1.ToList())[0];
                double   realPrice      = salePriceNormalizovani[i];
                Console.WriteLine("prediktovana vrednost je " + predictedPrice + "  stvarna je " + salePriceNormalizovani[i]);
                double tacnost = (predictedPrice * 100) / realPrice;
                Console.WriteLine(tacnost);
                if (tacnost >= 80 && tacnost <= 120)
                {
                    tacnoPrediktovanih++;
                }
            }
            Console.WriteLine("Tacnost: " + (tacnoPrediktovanih * 100) / brojPrediktovanih + "%");
            Console.ReadLine();



            List <double> normalizacijaInt(List <int> listaVrednosti)
            {
                List <double> noramlizovanaLista = new List <double>();

                double dataMax = listaVrednosti.Max();
                double dataMin = listaVrednosti.Min();

                //  Console.WriteLine("Maksimalna vrendost je" + dataMax);
                //Console.WriteLine("Minimalna vrendost je" + dataMin);

                foreach (double vrednost in listaVrednosti)
                {
                    noramlizovanaLista.Add((vrednost - dataMin) / (dataMax - dataMin));
                    // Console.WriteLine("Minimalna vrendost je" + (vrednost - dataMin) / (dataMax - dataMin));
                }
                return(noramlizovanaLista);
            }
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            #region Podesavanja koje cemo kolone ucitavati

            List <int> indeksKolonaInputa = new List <int>();

            // ulazi za pojavljivanje u knjigama
            indeksKolonaInputa.Add(16);
            indeksKolonaInputa.Add(17);
            indeksKolonaInputa.Add(18);
            indeksKolonaInputa.Add(19);
            indeksKolonaInputa.Add(20);

            // muski ili zensko ulaz
            indeksKolonaInputa.Add(7);
            // popularnost ulaz
            indeksKolonaInputa.Add(31);

            // plemicko poreklo ulaz
            indeksKolonaInputa.Add(26);

            // num dead relations ulaz
            indeksKolonaInputa.Add(28);

            // porodica kojoj pripada
            indeksKolonaInputa.Add(14);

            List <int> indeksKolonaOutputa = new List <int>();
            // isAlive ulaz
            indeksKolonaOutputa.Add(32);

            FileDAO fileDAO = new FileDAO(indeksKolonaInputa, indeksKolonaOutputa, 20);
            // fileDAO.imaKategorickihAtributa
            // fileDAO.getKategorickeAtribute

            #endregion


            #region Vektori za inpute

            List <List <double> > X = new List <List <double> >();
            X = fileDAO.X;

            #endregion

            #region Vektori za outpute

            List <List <double> > Y = new List <List <double> >();
            Y = fileDAO.Y;

            #endregion

            #region Kreiranje neuronske mreze

            NeuralNetwork network = new NeuralNetwork();
            network.Add(new NeuralLayer(X[0].Count, 4, "sigmoid")); // X[0], ali moze i X[bilo koji idx], bitno je da tako dobijem broj ulaza
            network.Add(new NeuralLayer(4, 2, "sigmoid"));
            network.Add(new NeuralLayer(2, 1, "sigmoid"));

            #endregion

            #region Fitovanje

            Console.WriteLine("Obuka pocela.");
            network.fit(X, Y, 0.1, 0.9, 10);
            Console.WriteLine("Kraj obuke.");

            #endregion

            #region Predikcija

            Score score = new Score();

            score.scoreModel(fileDAO, network);

            #endregion
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            string[] lines = File.ReadAllLines("@./../../../../../novi.csv");

            lines = lines.Skip(1).ToArray( );
            //ulazi
            List <int> typeList = new List <int>( );
            Dictionary <String, int> TypeDict           = new Dictionary <String, int>( );
            List <double>            oldBalanceOrgList  = new List <double>( );
            List <double>            newbalanceOrigList = new List <double>( );
            List <double>            oldBalanceDestList = new List <double>( );
            List <double>            newbalanceDestList = new List <double>( );
            List <double>            amountList         = new List <double>( );
            //izlaz
            List <int> isFraudList = new List <int>( );


            foreach (string line in lines)
            {
                string[] attr = line.Split(',');

                String p = attr[(int)FirstLine.type];
                if (TypeDict.ContainsKey(p))
                {
                    typeList.Add(TypeDict[p]);
                }
                else
                {
                    TypeDict[p] = TypeDict.Count;
                    typeList.Add(TypeDict[p]);
                }
                oldBalanceOrgList.Add(double.Parse(attr[(int)FirstLine.oldbalanceOrg]));
                newbalanceOrigList.Add(double.Parse(attr[(int)FirstLine.newbalanceOrig]));
                oldBalanceDestList.Add(double.Parse(attr[(int)FirstLine.oldbalanceDest]));
                newbalanceDestList.Add(double.Parse(attr[(int)FirstLine.newbalanceDest]));
                amountList.Add(double.Parse(attr[(int)FirstLine.amount]));

                isFraudList.Add(int.Parse(attr[(int)FirstLine.isFraud]));
            }

            //normalizacije
            double max = oldBalanceOrgList.Max( );

            for (int i = 0; i < oldBalanceOrgList.Count; i++)
            {
                oldBalanceOrgList[i] /= max;
            }

            max = newbalanceOrigList.Max( );
            for (int i = 0; i < newbalanceOrigList.Count; i++)
            {
                newbalanceOrigList[i] /= max;
            }

            max = oldBalanceDestList.Max( );
            for (int i = 0; i < oldBalanceDestList.Count; i++)
            {
                oldBalanceDestList[i] /= max;
            }

            max = newbalanceDestList.Max( );
            for (int i = 0; i < newbalanceDestList.Count; i++)
            {
                newbalanceDestList[i] /= max;
            }

            max = amountList.Max( );
            for (int i = 0; i < amountList.Count; i++)
            {
                amountList[i] /= max;
            }

            NeuralNetwork network = new NeuralNetwork( );

            network.Add(new NeuralLayer(6, 6, "sigmoid"));
            network.Add(new NeuralLayer(6, 1, "sigmoid"));

            List <List <double> > X = new List <List <double> >( );
            List <List <double> > Y = new List <List <double> >( );

            for (int i = 0; i < oldBalanceOrgList.Count; i++)
            {
                if (i % 5 != 0)
                {
                    double[] xTemp = { (double)typeList[i], (double)oldBalanceOrgList[i], (double)newbalanceOrigList[i], (double)oldBalanceDestList[i], (double)newbalanceDestList[i], (double)amountList[i] };
                    X.Add(xTemp.ToList( ));

                    double[] yTemp = { (double)isFraudList[i] };
                    Y.Add(yTemp.ToList( ));
                }
            }

            Console.WriteLine("obuka");
            network.fit(X, Y, 0.1, 0.9, 100);
            Console.WriteLine("done");

            //int x = (int)Math.Ceiling(oldBalanceOrgList.Count * 0.5);
            int x   = 0;
            int ok1 = 0;
            int ok0 = 0;
            int uk1 = 0;
            int uk0 = 0;

            for (int i = x; i < oldBalanceDestList.Count; i++)
            {
                if (i % 5 == 0)
                {
                    double[] x1 = { typeList[i], oldBalanceOrgList[i], newbalanceOrigList[i], oldBalanceDestList[i], newbalanceDestList[i], amountList[i] };
                    if (isFraudList[i] == 0)
                    {
                        uk0++;
                        if (network.predict(x1.ToList( ))[0] < 0.5)
                        {
                            ok0++;
                        }
                    }
                    else if (isFraudList[i] == 1)
                    {
                        uk1++;
                        if (network.predict(x1.ToList( ))[0] >= 0.5)
                        {
                            ok1++;
                        }
                    }
                }
            }

            Console.WriteLine("OK 0: " + ok0 + "\nUkupno: " + uk0 + "\nProcenat pogodaka:" + 100 * ((double)ok0 / uk0) + "\n");
            Console.WriteLine("OK 1: " + ok1 + "\nUkupno: " + uk1 + "\nProcenat pogodaka:" + 100 * ((double)ok1 / uk1) + "\n");
            Console.WriteLine("OK UkUPNO: " + (ok1 + ok0) + "\nUkupno: " + (uk0 + uk1) + "\nProcenat pogodaka:" + 100 * ((double)(ok1 + ok0) / (uk0 + uk1)) + "\n");

            Console.ReadLine( );
        }
Exemplo n.º 14
0
        static void Main(string[] args)
        {
            #region Ucitavanje data seta

            List <int> indeksKolonaInputa = new List <int>();

            // Gender
            indeksKolonaInputa.Add(1);

            // Married
            indeksKolonaInputa.Add(2);

            // education
            indeksKolonaInputa.Add(4);

            // ApplicantIncome
            indeksKolonaInputa.Add(6);

            //CoapplicantIncome
            indeksKolonaInputa.Add(7);

            //LoanAmount
            indeksKolonaInputa.Add(8);

            List <int> indeksKolonaOutputa = new List <int>();

            // Loan_Status
            indeksKolonaOutputa.Add(12);

            FileDAO fileDAO = new FileDAO(indeksKolonaInputa, indeksKolonaOutputa, 30); // 30 jer toliko zelim za test podatke

            #endregion

            #region Vektori za inpute

            List <List <double> > X = new List <List <double> >();
            X = fileDAO.X;

            #endregion

            #region Vektori za outpute

            List <List <double> > Y = new List <List <double> >();
            Y = fileDAO.Y;

            #endregion

            #region Kreiranje neuronske mreze

            NeuralNetwork network = new NeuralNetwork();
            network.Add(new NeuralLayer(X[0].Count, 2, "tanh")); // X[0], ali moze i X[bilo koji idx], bitno je da tako dobijem broj ulaza
            network.Add(new NeuralLayer(2, 1, "tanh"));

            #endregion

            #region Fitovanje

            Console.WriteLine("Obuka pocela.");
            network.fit(X, Y, 0.1, 0.9, 300);
            Console.WriteLine("Kraj obuke.");

            #endregion

            #region Skor koji postize mreza

            Score score = new Score();
            score.giveMeScore(fileDAO, network);

            #endregion
        }