コード例 #1
0
    // Evaluate the algorithm using cross validation and return average score
    public double CrossValidationEvaluation(double[][] X, double[] y, int k_folds, int n_epoch)
    {
        // Get the k fold validation split points
        int[] dataset_split = new int[k_folds];
        int   fold_size;

        (dataset_split, fold_size) = ML_tools.CrossValidationSplit(X, k_folds);

        // Set up some more parameters
        double[][] train_X;
        double[]   train_y;
        double[][] test_X;
        double[]   test_y;

        double score = 0;

        double[] predictions = new double[fold_size];

        // Run throught the algorithm k times
        for (int k = 0; k < k_folds - 1; k++)
        {
            // Bit fiddly here to get the right data in the training set
            if (!(k == 0))
            {
                List <double[]> list_X = new List <double[]>();
                list_X.AddRange(ML_tools.Slice(X, dataset_split[0], dataset_split[k] - 1));
                List <double> list_y = new List <double>();
                list_y.AddRange(ML_tools.Slice(y, dataset_split[0], dataset_split[k] - 1));

                if (!(k == k_folds - 1 || k == k_folds - 2))
                {
                    list_X.AddRange(ML_tools.Slice(X, dataset_split[k + 1], dataset_split[k + 2] - 1));
                    list_y.AddRange(ML_tools.Slice(y, dataset_split[k + 1], dataset_split[k + 2] - 1));
                }
                train_X = list_X.ToArray();
                train_y = list_y.ToArray();
            }
            else
            {
                train_X = ML_tools.Slice(X, dataset_split[k + 1], dataset_split[k + 2] - 1);
                train_y = ML_tools.Slice(y, dataset_split[k + 1], dataset_split[k + 2] - 1);
            }
            test_X = ML_tools.Slice(X, dataset_split[k], dataset_split[k + 1] - 1);
            test_y = ML_tools.Slice(y, dataset_split[k], dataset_split[k + 1] - 1);

            // Train the network
            Train(n_epoch, train_X, train_y);

            // Make predictions with it
            predictions = Predict(test_X);

            // Find the ABSE and average it
            score = ((score * k) + AbsoluteError(predictions, test_y)) / (k + 1);
        }
        return(score);
    }
コード例 #2
0
ファイル: Program.cs プロジェクト: Leo552/Neural_Networks_cs
        static void Main(string[] args)
        {
            // Saved data properties
            string filename = "C:\\Users\\maxel\\OneDrive\\Machine learning\\Neural Networks\\Data_sets\\Boston_housing.csv";

            int[] X_columns = new int[13] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13
            };
            int y_columns = 11;

            // Load the data
            (double[][] X, double[] y) = ML_tools.ReadCSV(filename, X_columns, y_columns);

            // Scale the data
            ML_tools.MinMaxTransformation_X(X);
            (double minimum, double range) = ML_tools.MinMaxTransformation_y(y);

            // Neural network parameters
            int hidden_layers = 2;

            int[] hidden_nodes = new int[2] {
                5, 3
            };                                        // Must be the same length as the hidden layers
            double l_rate_weights  = 2;
            double l_rate_bias     = 2;
            string activation_func = "sigmoid";

            NeuralNetwork Neural_Net = new NeuralNetwork(hidden_layers, hidden_nodes, l_rate_weights, l_rate_bias, 13, 1, activation_func);

            // Testing parameters
            int n_epoch = 10000;
            int k_folds = 5;

            // Time how long it takes to execute
            Stopwatch stopwatch = Stopwatch.StartNew();

            // Use cross validation to evaluate the algorithm
            Console.WriteLine("Cross validation absolute error: {0}", Neural_Net.CrossValidationEvaluation(X, y, k_folds, n_epoch));
            stopwatch.Stop();
            Console.WriteLine("Execution time: {0} seconds", (stopwatch.ElapsedMilliseconds / 1000));
            Console.ReadLine();
        }