コード例 #1
0
ファイル: SVM.cs プロジェクト: MPPECS/Twitter_RWR_Recommender
        /*************************** Secondary Methods *******************************/
        private Tuple<Double[][], int[]> convertToTrainIntputTable(DataSet trainSet)
        {
            // Raw Data
            int egoCount = trainSet.egoNetworkList.Count;
            EgoNetwork[] egoList = new EgoNetwork[egoCount];
            trainSet.egoNetworkList.CopyTo(egoList);
            double[][] inputArray = new double[egoCount][];
            int[] outputVector = new int[egoCount];

            for (int i = 0; i < inputArray.Length; i++)
            {
                inputArray[i] = egoList[i].attributes;
                outputVector[i] = egoList[i].optimalLabel;
            }

            // Normalization: Data Preprocess
            int height = inputArray.Length, width = inputArray[0].Length;
            mean = new double[width];
            leftMost = new double[width];
            rightMost = new double[width];

            for (var j = 0; j < width; j++)
            {
                double average = 0.0;
                double min = double.PositiveInfinity;
                double max = double.NegativeInfinity;

                for (var i = 0; i < height; i++)
                {
                    // Left Most Value
                    if (inputArray[i][j] < min)
                        min = inputArray[i][j];
                    // Right Most Value
                    if (inputArray[i][j] > max)
                        max = inputArray[i][j];
                    // Average
                    average += inputArray[i][j];
                }
                this.leftMost[j] = min;
                this.rightMost[j] = max;
                this.mean[j] = average / height;
            }

            // Normalization: Scaling --> [-1, +1]
            for (var j = 0; j < width; j++)
            {
                for (var i = 0; i < height; i++)
                {
                    double previous = inputArray[i][j];
                    if (previous <= this.mean[j])
                        inputArray[i][j] = (previous - this.mean[j]) / (this.mean[j] - this.leftMost[j]);
                    else
                        inputArray[i][j] = (previous - this.mean[j]) / (this.rightMost[j] - this.mean[j]);
                }
            }

            return new Tuple<Double[][], int[]>(inputArray, outputVector);
        }
コード例 #2
0
 /***************************** Override *********************************/
 public override bool Equals(Object obj)
 {
     if (obj is EgoNetwork)
     {
         EgoNetwork otherTweet = (EgoNetwork)obj;
         return(this.egoID == otherTweet.egoID);
     }
     else
     {
         throw new ArgumentException("Object is not EgoNetwork");
     }
 }
コード例 #3
0
 /*************************** Primary Methods *******************************/
 public void addEgoNetwork(EgoNetwork egoNetwork)
 {
     this.egoNetworkList.Add(egoNetwork);
 }
コード例 #4
0
        /*************************** Secondary Methods *******************************/
        private Tuple <Double[][], int[]> convertToTrainIntputTable(DataSet trainSet)
        {
            // Raw Data
            int egoCount = trainSet.egoNetworkList.Count;

            EgoNetwork[] egoList = new EgoNetwork[egoCount];
            trainSet.egoNetworkList.CopyTo(egoList);
            double[][] inputArray   = new double[egoCount][];
            int[]      outputVector = new int[egoCount];

            for (int i = 0; i < inputArray.Length; i++)
            {
                inputArray[i]   = egoList[i].attributes;
                outputVector[i] = egoList[i].optimalLabel;
            }

            // Normalization: Data Preprocess
            int height = inputArray.Length, width = inputArray[0].Length;

            mean      = new double[width];
            leftMost  = new double[width];
            rightMost = new double[width];

            for (var j = 0; j < width; j++)
            {
                double average = 0.0;
                double min     = double.PositiveInfinity;
                double max     = double.NegativeInfinity;

                for (var i = 0; i < height; i++)
                {
                    // Left Most Value
                    if (inputArray[i][j] < min)
                    {
                        min = inputArray[i][j];
                    }
                    // Right Most Value
                    if (inputArray[i][j] > max)
                    {
                        max = inputArray[i][j];
                    }
                    // Average
                    average += inputArray[i][j];
                }
                this.leftMost[j]  = min;
                this.rightMost[j] = max;
                this.mean[j]      = average / height;
            }

            // Normalization: Scaling --> [-1, +1]
            for (var j = 0; j < width; j++)
            {
                for (var i = 0; i < height; i++)
                {
                    double previous = inputArray[i][j];
                    if (previous <= this.mean[j])
                    {
                        inputArray[i][j] = (previous - this.mean[j]) / (this.mean[j] - this.leftMost[j]);
                    }
                    else
                    {
                        inputArray[i][j] = (previous - this.mean[j]) / (this.rightMost[j] - this.mean[j]);
                    }
                }
            }

            return(new Tuple <Double[][], int[]>(inputArray, outputVector));
        }
コード例 #5
0
        /*************************** Primary Methods *******************************/
        public void dataSetConfiguration(SortedList columnList, string rwrFilePath, string networkFilePath)
        {
            ArrayList egoIDList = new ArrayList();
            SortedDictionary <long, int>      egoOptimalLabelDictionary = new SortedDictionary <long, int>();
            SortedDictionary <long, double[]> egoRwrResultsDictionary   = new SortedDictionary <long, double[]>();
            SortedDictionary <long, double[]> egoAttributesDictionary   = new SortedDictionary <long, double[]>();

            using (StreamReader rwrReader = new StreamReader(rwrFilePath))
            {
                string line = null;
                while ((line = rwrReader.ReadLine()) != null)
                {
                    long     egoID;
                    int      egoOptimalLabel;
                    string[] rwrResult = line.Split('\t');
                    double[] MAP       = new double[rwrResult.Length - 2];

                    egoID           = long.Parse(rwrResult[0]);
                    egoOptimalLabel = int.Parse(rwrResult[1]);
                    for (int i = 2; i < rwrResult.Length; i++)
                    {
                        MAP[i - 2] = double.Parse(rwrResult[i]);
                    }

                    egoOptimalLabelDictionary.Add(egoID, egoOptimalLabel);
                    egoRwrResultsDictionary.Add(egoID, MAP);
                    egoIDList.Add(egoID);
                }
            }

            using (StreamReader networkReader = new StreamReader(networkFilePath))
            {
                string line = null;
                while ((line = networkReader.ReadLine()) != null)
                {
                    long     egoID;
                    string[] networkResults = line.Split('\t');
                    double[] attributes     = new double[columnList.Count];

                    egoID = long.Parse(networkResults[0]);
                    int i = 0;
                    for (int j = 1; j < networkResults.Length; j++)
                    {
                        if (columnList.ContainsKey(j - 1))
                        {
                            attributes[i++] = double.Parse(networkResults[j]);
                        }
                    }
                    egoAttributesDictionary.Add(egoID, attributes);
                }
            }

            // K-Fold Split DataSets
            SortedList <long, EgoNetwork> egoNetworkList = new SortedList <long, EgoNetwork>();

            egoIDList.Sort(); // Ascending Order
            foreach (long egoID in egoIDList)
            {
                egoNetworkList.Add(egoID, new EgoNetwork(egoID, egoOptimalLabelDictionary[egoID],
                                                         egoRwrResultsDictionary[egoID], egoAttributesDictionary[egoID]));
            }

            int boundary = egoNetworkList.Count / nFold;

            for (int i = 0; i < nFold; i++)
            {
                this.dataSets[i] = new DataSet();
                if (i != (nFold - 1))
                {
                    for (int j = i * boundary; j < (i + 1) * boundary; j++) // Each sub-dataset boundary
                    {
                        EgoNetwork egoNetWork = egoNetworkList[(long)egoIDList[j]];
                        dataSets[i].addEgoNetwork(egoNetWork);
                    }
                }
                else // Incase: Last sub-dataset
                {
                    for (int j = i * boundary; j < egoNetworkList.Count; j++)
                    {
                        EgoNetwork egoNetWork = egoNetworkList[(long)egoIDList[j]];
                        dataSets[i].addEgoNetwork(egoNetWork);
                    }
                }
            }
        }
コード例 #6
0
 /*************************** Primary Methods *******************************/
 public void addEgoNetwork(EgoNetwork egoNetwork)
 {
     this.egoNetworkList.Add(egoNetwork);
 }