示例#1
0
 public Problem(Experiments d, Features f)
 {
     data      = d;
     functions = f;
     exSize    = d.Size();
     fSize     = f.Size();
 }
示例#2
0
        /// <summary>This is if we are given an array of double with a value for each training sample in the order of their occurrence.</summary>
        public Feature(Experiments e, double[] vals, IIndex <IntPair> instanceIndex)
        {
            // todo [cdm 2013]: This needs to be removed! Try to put field in Features class, rather than adding as field to every object.
            // the sum of all values
            this.instanceIndex = instanceIndex;
            IDictionary <int, double> setNonZeros = Generics.NewHashMap();

            for (int i = 0; i < vals.Length; i++)
            {
                if (vals[i] != 0.0)
                {
                    int @in = int.Parse(IndexOf(e.Get(i)[0], e.Get(i)[1]));
                    // new Integer(e.get(i)[0]*e.ySize+e.get(i)[1]);
                    double oldVal = setNonZeros[@in] = double.ValueOf(vals[i]);
                    if (oldVal != null && oldVal != vals[i])
                    {
                        throw new InvalidOperationException("Incorrect function specification: Feature has two values at one point: " + oldVal + " and " + vals[i]);
                    }
                }
            }
            //if
            // for
            int[] keys = Sharpen.Collections.ToArray(setNonZeros.Keys, new int[setNonZeros.Keys.Count]);
            indexedValues = new int[keys.Length];
            valuesI       = new double[keys.Length];
            for (int j = 0; j < keys.Length; j++)
            {
                indexedValues[j] = keys[j];
                valuesI[j]       = setNonZeros[keys[j]];
            }
            // for
            domain = e;
        }
示例#3
0
 /// <param name="indexes">The pairs (x,y) for which the feature is non-zero. They are coded as x*ySize+y</param>
 /// <param name="vals">The values at these points.</param>
 public Feature(Experiments e, int[] indexes, double[] vals, IIndex <IntPair> instanceIndex)
 {
     domain             = e;
     indexedValues      = indexes;
     valuesI            = vals;
     this.instanceIndex = instanceIndex;
 }
示例#4
0
        /// <param name="vals">a value for each (x,y) pair</param>
        public Feature(Experiments e, double[][] vals, IIndex <IntPair> instanceIndex)
        {
            this.instanceIndex = instanceIndex;
            domain             = e;
            int num = 0;

            for (int x = 0; x < e.xSize; x++)
            {
                for (int y = 0; y < e.ySize; y++)
                {
                    if (vals[x][y] != 0)
                    {
                        num++;
                    }
                }
            }
            indexedValues = new int[num];
            valuesI       = new double[num];
            int current = 0;

            for (int x_1 = 0; x_1 < e.xSize; x_1++)
            {
                for (int y = 0; y < e.ySize; y++)
                {
                    if (vals[x_1][y] != 0)
                    {
                        indexedValues[current] = IndexOf(x_1, y);
                        valuesI[current]       = vals[x_1][y];
                        current++;
                    }
                }
            }
        }
示例#5
0
 public Feature(Experiments e, int numElems, IIndex <IntPair> instanceIndex)
 {
     //if
     //for
     this.instanceIndex = instanceIndex;
     domain             = e;
     indexedValues      = new int[numElems];
     valuesI            = new double[numElems];
 }
        /// <summary>
        /// reads in the features from a file, having already read the
        /// experiments
        /// </summary>
        public Features(string filename, Experiments domain)
        {
            Exception        e1            = new Exception("Incorrect data file format!");
            IIndex <IntPair> instanceIndex = domain.CreateIndex();

            try
            {
                using (BufferedReader @in = new BufferedReader(new FileReader(filename)))
                {
                    string s;
                    while (true)
                    {
                        s = @in.ReadLine();
                        if (s.Equals("<features>"))
                        {
                            break;
                        }
                    }
                    if (s == null)
                    {
                        throw e1;
                    }
                    s = @in.ReadLine();
                    if (!s.StartsWith("<fSize>"))
                    {
                        throw e1;
                    }
                    if (!s.EndsWith("</fSize>"))
                    {
                        throw e1;
                    }
                    int    index1 = s.IndexOf(">");
                    int    index2 = s.LastIndexOf("<");
                    string fSt    = Sharpen.Runtime.Substring(s, index1 + 1, index2);
                    System.Console.Out.WriteLine(fSt);
                    int number = System.Convert.ToInt32(fSt);
                    System.Console.Out.WriteLine("fSize is " + number);
                    int[]    arrIndexes = new int[maxValue];
                    double[] arrValues  = new double[maxValue];
                    for (int f = 0; f < number; f++)
                    {
                        string line    = @in.ReadLine();
                        int    indSp   = -1;
                        int    current = 0;
                        while ((indSp = line.IndexOf(" ")) > -1)
                        {
                            int x = System.Convert.ToInt32(Sharpen.Runtime.Substring(line, 0, indSp));
                            line  = Sharpen.Runtime.Substring(line, indSp + 1);
                            indSp = line.IndexOf(" ");
                            if (indSp == -1)
                            {
                                indSp = line.Length;
                            }
                            int y = System.Convert.ToInt32(Sharpen.Runtime.Substring(line, 0, indSp));
                            line  = Sharpen.Runtime.Substring(line, indSp + 1);
                            indSp = line.IndexOf(" ");
                            if (indSp == -1)
                            {
                                indSp = line.Length;
                            }
                            double val = double.ParseDouble(Sharpen.Runtime.Substring(line, 0, indSp));
                            if (indSp < line.Length)
                            {
                                line = Sharpen.Runtime.Substring(line, indSp + 1);
                            }
                            arrIndexes[current] = instanceIndex.IndexOf(new IntPair(x, y));
                            arrValues[current]  = val;
                            current++;
                        }
                        int[]    indValues = new int[current];
                        double[] values    = new double[current];
                        for (int j = 0; j < current; j++)
                        {
                            indValues[j] = arrIndexes[j];
                            values[j]    = arrValues[j];
                        }
                        Feature bf = new Feature(domain, indValues, values, instanceIndex);
                        this.Add(bf);
                    }
                }
            }
            catch (Exception e)
            {
                // for f
                log.Warn(e);
            }
        }