示例#1
0
        private static void GetColorValueFromAllOriginalValues(List <List <Countrystats> > allAllValues, int year, List <Response> responses)
        {
            //Find all values for year
            List <YearWithValue> valuesForYear = new List <YearWithValue>();

            foreach (var valuesForOnecountry in allAllValues)
            {
                if (valuesForOnecountry.Any(x => x.ListWithCategoriesWithYearsAndValues.Count > 1))
                {
                    Console.WriteLine("sucks");
                }
                if (valuesForOnecountry.First().doesContainAnyValues())
                {
                    Countrystats country = valuesForOnecountry.First();
                    CategoriesWithYearsAndValues categoriesWithYearsAndValues = country.ListWithCategoriesWithYearsAndValues.Find(z => z.YearsWithValues.Any(b => b.Year == year));
                    if (categoriesWithYearsAndValues != null)
                    {
                        var yearWithValues = categoriesWithYearsAndValues.YearsWithValues;
                        var valueForYear   = yearWithValues.Find(x => x.Year == year);
                        //  var valueForYear = valuesForOnecountry.First(y => y == y).ListWithCategoriesWithYearsAndValues.Find(z => z.YearsWithValues.Any(b => b.Year == year)).YearsWithValues.First(a => a.Year == year);
                        valuesForYear.Add(valueForYear);
                        //Find max of all values in year
                        float maxValue = valuesForYear.Where(x => x == x).Max(y => y.Value.value);
                        //Berechne color-Value für alles und weise es den korrekten responses zu
                        //calc color-value
                        float m = 2 * 255 / maxValue;
                        foreach (var item in valuesForYear)
                        {
                            try
                            {
                                int index = responses.FindIndex(x => x.valuePairList.Any(y => y.value == item.Value.value)); //Get first value-year list where any value is the same as one of the values in the list with values for year
                                var temp  = responses[index];
                                temp.colorVal = m * temp.valuePairList.First(x => x.year == year).value;
                                if (temp.colorVal < 0)
                                {
                                    temp.errorMessage = "colorValue below 0, ich fix es später";
                                    temp.colorVal     = 1;
                                }
                                responses[index] = temp;
                            }
                            catch (InvalidOperationException)
                            {
                                Console.WriteLine("Meier, kann nicht besser nach Kategorien filtern wo keine Werte drin sind");
                            }
                        }
                    }
                }
            }

            var countriesWithNoValuesInYear = responses.Where(x => x.colorVal == 0).ToArray();

            for (int i = 0; i < countriesWithNoValuesInYear.Count(); i++)
            {
                countriesWithNoValuesInYear[i].errorMessage = "Country has no value in the specific year";
            }
        }
示例#2
0
        /// <summary>
        /// Calculates future values based on alreadyknown Parameters
        /// </summary>
        /// <param name="yearWithValues">List that gets extended</param>
        /// <param name="from">startyear</param>
        /// <param name="futureYear">year in the future</param>
        /// <param name="parStor">parameter</param>
        /// <returns></returns>
        public List <YearWithValue> Predict(List <YearWithValue> yearWithValues, int from, int futureYear, ParameterStorage parStor)
        {
            double j          = yearWithValues.Max(k => k.Year);
            float  valueToDiv = CategoriesWithYearsAndValues.GetValuesFromList(yearWithValues).Max();

            float[]       inputs = CategoriesWithYearsAndValues.GetYearsFromList(yearWithValues);
            List <double> listForTheNormalizedInputs = new List <double>();

            foreach (var item in inputs)
            {
                listForTheNormalizedInputs.Add(item); //Small brain schleife?
            }
            Input input = StandardizationYears(listForTheNormalizedInputs, futureYear);

            inputs = input.getAlleJahreNormiert();
            if (j < futureYear)
            {
                float inputsMax = inputs.Max();
                while (j < futureYear)
                {
                    j++;
                    yearWithValues.Add(new YearWithValue(j, new Wert((parStor.W * inputsMax + parStor.b) * valueToDiv)));
                    float[]       inputtemp     = CategoriesWithYearsAndValues.GetYearsFromList(yearWithValues);
                    List <double> fuckinghelpme = new List <double>();
                    foreach (var item in inputtemp)
                    {
                        fuckinghelpme.Add(item); //Small brain schleife?
                    }
                    Input input2 = StandardizationYears(fuckinghelpme, futureYear);
                    inputtemp = input2.getAlleJahreNormiert();
                    inputsMax = inputtemp.Max();
                }
            }
            else //cut list from year to futureyear
            {
                if (futureYear > from)
                {
                    int indexMax = yearWithValues.FindIndex(a => a.Year == Convert.ToInt32(futureYear)); //finde Index von Jahr bis zu dem man Daten braucht
                    yearWithValues.RemoveRange(indexMax, yearWithValues.Count - indexMax);               //Cutte List von Jahr bis zu dem man es braucht bis Ende

                    int indexMin = yearWithValues.FindIndex(b => b.Year == Convert.ToInt32(from));
                    yearWithValues.RemoveRange(0, indexMin);
                }
                else
                {
                    var temp = yearWithValues.Where(x => x.Year == from);
                    yearWithValues = temp.ToList();;
                }
            }
            return(yearWithValues);
        }
示例#3
0
        /// <summary>
        /// Returns categorys with all years and values of a single country
        /// </summary>
        /// <param name="country">Country</param>
        /// <param name="kategorieIDs">List of category IDs</param>
        /// <returns>Liste mit Kategorien mit Jahren und Werten</returns>
        public async Task <List <CategoriesWithYearsAndValues> > GetCategoriesWithValuesAndYearsAsync(string country, List <int> kategorieIDs)
        {
            Console.WriteLine("GetCategoriesWithValuesAndYearsAsync");
            List <CategoriesWithYearsAndValues> keyValuePairs = new List <CategoriesWithYearsAndValues>();

            using (SqlConnection sqlConnection = new SqlConnection(ki_read_input))
            {
                await sqlConnection.OpenAsync();

                SqlCommand    command = sqlConnection.CreateCommand();
                List <string> vs      = await GetNamesOfCategoriesByIDsAsync(kategorieIDs);

                foreach (var item in vs)
                {
                    CategoriesWithYearsAndValues kmjw = new CategoriesWithYearsAndValues
                    {
                        category = new Category(item)
                    };
                    command.CommandText = $"SELECT year, ROUND(value,5) AS ROUND, c.cat_id FROM input_data JOIN category c on input_data.cat_id = c.cat_id JOIN country_or_area coa on input_data.coa_id = coa.coa_id WHERE c.name = '{item}' AND coa.name = '{country}';";
                    Console.WriteLine(command.CommandText);
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        List <YearWithValue> temp = new List <YearWithValue>();
                        while (await reader.ReadAsync()) //fraglich ob es nicht eine bessere Methode gibt
                        {
                            int year  = Convert.ToInt32(reader["year"]);
                            var value = Convert.ToDecimal(reader["ROUND"].ToString());
                            int catid = Convert.ToInt32(reader["cat_id"]);

                            temp.Add(new YearWithValue(year, new Wert((decimal)value, false), item, catid));
                        }
                        kmjw.YearsWithValues = temp;
                        keyValuePairs.Add(kmjw);
                    }
                }
            }

            return(keyValuePairs);
        }
示例#4
0
文件: CNTK.cs 项目: actopozipc/Telluz
        public async Task <List <YearWithValue> > TrainLinearOneOutputAsync(List <YearWithValue> KnownValues, int FutureYear)
        {
            var device = DeviceDescriptor.UseDefaultDevice();
            ////Step 2: define values, and variables
            Variable x = Variable.InputVariable(new NDShape(1, 1), DataType.Float, "input");
            Variable y = Variable.InputVariable(new NDShape(1, 1), DataType.Float, "output");

            ////Step 2: define training data set from table above
            float[]       inputs = CategoriesWithYearsAndValues.GetYearsFromList(KnownValues);
            List <double> temp   = new List <double>();

            foreach (var item in inputs)
            {
                temp.Add(item); //Small brain schleife?
            }
            Input input = StandardizationYears(temp, FutureYear);

            inputs = input.getAlleJahreNormiert();
            float[] outputs = CategoriesWithYearsAndValues.GetValuesFromList(KnownValues);
            //Value.CreateBatch(Tensor(Achsen, Dimension), Werte, cpu/gpu)
            float[] outputsnormiert   = new float[outputs.Count()];
            float   WertZumDividieren = outputs.Max();

            for (int i = 0; i < outputs.Length; i++)
            {
                outputsnormiert[i] = outputs[i] / WertZumDividieren;
            }
            //Werte normiert lassen, sonst stackoverflow :>
            var xValues = Value.CreateBatch(new NDShape(1, 1), GetLastNValues(inputs, inputs.Length, input.step), device);
            var yValues = Value.CreateBatch(new NDShape(1, 1), GetLastNValues(outputsnormiert, outputs.Length, input.step), device);
            ////Step 3: create linear regression model
            var lr = createLRModel(x, device);
            ////Network model contains only two parameters b and w, so we query
            ////the model in order to get parameter values
            var paramValues     = lr.Inputs.Where(z => z.IsParameter).ToList();
            var totalParameters = paramValues.Sum(c => c.Shape.TotalSize);
            ////Step 4: create trainer
            var trainer = createTrainer(lr, y);
            ////Ştep 5: training
            double b = 0, w = 0;
            int    max = 2000;

            for (int i = 1; i <= max; i++)
            {
                var d = new Dictionary <Variable, Value>();
                d.Add(x, xValues);
                d.Add(y, yValues);
                //
                trainer.TrainMinibatch(d, true, device);
                //
                var loss = trainer.PreviousMinibatchLossAverage();
                var eval = trainer.PreviousMinibatchEvaluationAverage();
                //
                if (i % 200 == 0)
                {
                    Console.WriteLine($"It={i}, Loss={loss}, Eval={eval}");
                }

                if (i == max)
                {
                    //print weights
                    var b0_name = paramValues[0].Name;
                    var b0      = new Value(paramValues[0].GetValue()).GetDenseData <float>(paramValues[0]);
                    var b1_name = paramValues[1].Name;
                    var b1      = new Value(paramValues[1].GetValue()).GetDenseData <float>(paramValues[1]);
                    Console.WriteLine($" ");
                    Console.WriteLine($"Training process finished with the following regression parameters:");
                    Console.WriteLine($"b={b0[0][0]}, w={b1[0][0]}");
                    b = b0[0][0];
                    w = b1[0][0];
                    ParameterStorage ps = new ParameterStorage(float.Parse(w.ToString()), float.Parse(b.ToString()));
                    int coaid           = await dB.GetCountryByNameAsync(KnownValues.Where(k => k.Name != null).First().Name);

                    await dB.SaveParameterAsync(ps, coaid, KnownValues.Where(k => k.cat_id != 0).First().cat_id, loss);

                    Console.WriteLine(KnownValues.Min(k => k.Year));
                    KnownValues = Predict(KnownValues, Convert.ToInt32(KnownValues.Min(k => k.Year)), FutureYear, ps);
                }
            }


            return(KnownValues);
        }