Search code examples
c#methodsreturn

return data from method c#


I use this method and need return weights8spr, but I don't know how. Can anybody help me? simplest return(weights8spr) don't work, because in input Im not give the double array.

public class Run {
    public Run(List<dataVM2> TrainDataForStations)
    {
        double[] resultMAX1 = new double[] { 40.0, 1200.0, 100.0, 100.0, 10000.0 };
        double[] resultMIN1 = new double[] { -50.0, 0.0, 0.0, 0.0, 0.0 };
        double d1 = 0.0;
        double d2 = 1.0;
        int numItemsspr = TrainDataForStations.Count;
        double[][] trainData = new double[numItemsspr][];
        Random rnd = new Random(1);
        double[][] MassiveDataspr8 = new double[numItemsspr][];
        for (var i = 0; i < numItemsspr; ++i)
        {

            trainData[i] = new double[] { TrainDataForStations[i].TemperatureC1, TrainDataForStations[i].SolarRadiation1, TrainDataForStations[i].Wetness1, TrainDataForStations[i].WindSpeed1, TrainDataForStations[i].gen1 };
        }
        int maxcol = 0;
        for (int i = 0; i < trainData.Length; i++)
        {
            if (trainData[i].Length > maxcol)
                maxcol = trainData[i].Length;
        }
        //data normalization 
        for (int j = 0; j < MassiveDataspr8.Length; j++)
        {
            MassiveDataspr8[j] = new double[maxcol];
            for (int i = 0; i < maxcol; i++)
            {
                MassiveDataspr8[j][i] = (((trainData[j][i] - resultMIN1[i]) * (d2 - d1)) / (resultMAX1[i] - resultMIN1[i])) + d1;

            }
        }
        int NumInput = 4;
        int NumHidden = 25;
        int NumOutput = 1;
        int rndSeed = 0;

        NeuralNetworkData neuralform = new NeuralNetworkData(NumInput, NumHidden, NumOutput, rnd);
        int maxEpochs = 1000;
        double learnRate = 0.005;
        double momentum = 0.001;
        double[] weights8spr = new NeuralNetworkTrainer(neuralform, rnd).Train(MassiveDataspr8, maxEpochs, learnRate, momentum);

    }
}

Solution

  • You are executing that code in the constructor of a class and you can't change the return type of a constructor.

    You should create a method in the class and refactor the code a little.

    public class Run 
    {        
        // default constructor not needed
        public Run()
        {
        }
    
        public double[] RunMethod(List<dataVM2> TrainDataForStations)
        {
            // put your code here
            // ...
    
            // return the double[]
            return weights8spr;
        }
    }
    

    And execute like:

    var run = new Run();
    var weights = run.RunMethod(listOfTrainDataForStations);