public Matrix<double> determineEuclideanDistMX(Matrix<double> objCoords)
 {
     int nObj = objCoords.RowCount;
     Matrix<double> distanceMX = Matrix<double>.Build.Dense(nObj, nObj);
     for (int idx = 0; idx < nObj; idx++)
     {
         for (int subIdx = 0; subIdx < nObj; subIdx++)
         {
             distanceMX[idx, subIdx] = Distance.Euclidean<double>(objCoords.Row(idx), objCoords.Row(subIdx));
         }
     }
     return distanceMX;
 }
Example #2
0
        /// <summary>
        /// Get the top K neighbors of the target user.
        /// </summary>
        /// <param name="similaritiesByUser">The similarities matrix.</param>
        /// <param name="indexOfTargetUser">The index of target user.</param>
        /// <param name="K">The number of neighbors.</param>
        /// <returns>Each key is one user of the top K neighbors, 
        /// and the value is his similarity to the target user.</returns>
        public static Dictionary<int, double> GetTopKNeighborsByUser(Matrix<double> similaritiesByUser, int indexOfTargetUser, int K)
        {
            Debug.Assert(similaritiesByUser.RowCount == similaritiesByUser.ColumnCount, "The similarities should be in a square matrix.");
            Debug.Assert(similaritiesByUser.RowCount > K, "The total number of users is less than K + 1 ");

            Dictionary<int, double> similaritiesByTopKUser = new Dictionary<int, double>(K);

            // They will be sorted soon
            List<double> similaritiesOfNeighborsSortedBySimilarity = similaritiesByUser.Row(indexOfTargetUser).ToList();
            List<int> indexesOfNeighborsSortedBySimilarity = Enumerable.Range(0, similaritiesOfNeighborsSortedBySimilarity.Count).ToList();

            // To exclute the target user himself from neighbors
            similaritiesOfNeighborsSortedBySimilarity[indexOfTargetUser] = double.MinValue;

            // Sort the neighbors' indexes according to their similarities to the target user
            Sorting.Sort<double, int>(similaritiesOfNeighborsSortedBySimilarity, indexesOfNeighborsSortedBySimilarity);

            // Make it descending order by similarity
            similaritiesOfNeighborsSortedBySimilarity.Reverse();
            indexesOfNeighborsSortedBySimilarity.Reverse();

            for (int i = 0; i < K; ++i)
            {
                // Be very careful about the index
                // indexesOfNeighborsSortedBySimilarity[0] will give 
                // the index (in similaritiesByUser) of the most similar neighbor
                // and i is the index in the sorted similaritiesOfNeighbors 
                similaritiesByTopKUser[indexesOfNeighborsSortedBySimilarity[i]] = similaritiesOfNeighborsSortedBySimilarity[i];
            }

            return similaritiesByTopKUser;
        }
Example #3
0
        public Molecule(double[,] inputAtoms, bool getDirections)
        {
            numAtoms = inputAtoms.GetLength(0);
            atoms = DenseMatrix.OfArray(inputAtoms);
            centroid = atoms.ColumnSums() / (double)numAtoms;

            if (getDirections)
            {
                for (int i = 0; i < 3; i++)
                {
                    atoms.SetColumn(i, atoms.Column(i) - centroid[i]);
                }
                direction = atoms.Svd(true).VT.Row(0);
                if ((atoms.Row(atoms.RowCount - 1) - atoms.Row(0)).DotProduct(direction) < 0)
                {
                    direction *= -1;
                }
            }
        }
 public Matrix<double> determineWeightedEuclideanDistMX(Matrix<double> objCoords, double[] weights)
 {
     int nObj = objCoords.RowCount;
     int dim = objCoords.ColumnCount;
     Matrix<double> distanceMX = Matrix<double>.Build.Dense(nObj, nObj);
     for (int idx = 0; idx < nObj; idx++)
     {
         for (int subIdx = 0; subIdx < nObj; subIdx++)
         {
             double currentDist = 0.0;
             for (int idxDim = 0; idxDim < dim; idxDim++)
             {
                 currentDist += weights[idxDim] *
                     Math.Pow(objCoords.Row(idx)[idxDim] - objCoords.Row(subIdx)[idxDim], 2);
             }
             distanceMX[idx, subIdx] = Math.Sqrt(currentDist);
         }
     }
     return distanceMX;
 }
 private Dictionary<double, int> computeMiddlenessOfObjs(Matrix<double> distanceMX)
 {
     int nObj = distanceMX.RowCount;
     Matrix<double> middlenessMX = Matrix<double>.Build.Dense(nObj, nObj,
         (i, j) => distanceMX[i, j] / distanceMX.Row(i).Sum());
     Dictionary<double, int> dictOfMiddleness = new Dictionary<double, int>();
     for (int idx = 0; idx < nObj; idx++)
     {
         double currentMiddleness = middlenessMX.Column(idx).Sum();
         dictOfMiddleness[currentMiddleness] = idx;
     }
     return dictOfMiddleness;
 }
 static Matrix<double> SplitDataSet(Matrix<double> dataSet, int axis, double value)
 {
     var retDataList = new List<Vector<double>>();
     for(var i = 0;i<dataSet.RowCount;i++)
     {
         var featVec = dataSet.Row(i);
         if (featVec[axis] == value)
         {
             var reducedFeatVec = Vector.Build.DenseOfEnumerable(
                 featVec.Take(axis).Concat(featVec.Skip(axis+1)));
             retDataList.Add(reducedFeatVec);
         }
     }
     return Matrix.Build.DenseOfRowVectors(retDataList);
 }
 static double CalcShannonEnt(Matrix<double> dataSet, string[] labels)
 {
     var numEntries = dataSet.RowCount;
     var labelCounts = new Dictionary<string, int>();
     for(var i = 0;i<numEntries;i++)
     {
         var featVec = dataSet.Row(i);
         var currentLabel = labels[i];
         if (!labelCounts.ContainsKey(currentLabel)) labelCounts.Add(currentLabel, 0);
         labelCounts[currentLabel] += 1;
     }
     var shannonEnt = 0.0;
     foreach(var key in labelCounts.Keys)
     {
         var prob = (double)(labelCounts[key]) / (double)numEntries;
         shannonEnt -= prob * Math.Log(prob, 2);
     }
     return shannonEnt;
 }
        public ILinearRegressionModel BuildModel(Matrix<double> matrixX, Vector<double> vectorY, ILinearRegressionParams regressionParams)
        {
            var weightsVector = Vector<double>.Build.DenseOfArray(
                Enumerable.Range(0, matrixX.ColumnCount)
                .Select(fIdx => randomizer.NextDoubleInRange(weightsMin, weightsMax))
                .ToArray());
            double previousIterError = double.PositiveInfinity;
            for (int iterNo = 0; iterNo < iterations; iterNo++)
            {
                var vectorYHat = matrixX.Multiply(weightsVector);
                var diffVector = vectorYHat.Subtract(vectorY);
                var currentIterError = diffVector.Select(Math.Abs).Sum();
                if ((currentIterError <= stopLerningError) ||
                    currentIterError.AlmostEqual(previousIterError, 5))
                {
                    break;
                }
                for (int weightIdx = 0; weightIdx < weightsVector.Count; weightIdx++)
                {
                    var diffSums = 0.0;
                    for (int rowIdx = 0; rowIdx < matrixX.RowCount; rowIdx++)
                    {
                        diffSums += diffVector[rowIdx] * matrixX.Row(rowIdx)[weightIdx];
                    }
                    var newWeightValue = CalculateWeightUpdate(
                        matrixX,
                        regressionParams,
                        diffSums,
                        weightsVector[weightIdx]);
                    weightsVector[weightIdx] = newWeightValue;
                }
                previousIterError = currentIterError;
            }

            return new LinearRegressionModel(weightsVector);
        }
Example #9
0
        /// <summary>
        /// Generate primal coefficients from dual coefficients
        /// for the one-vs-one multi class LibSVM in the case
        /// of a linear kernel.
        /// </summary>
        private static Vector<double>[] OneVsOneCoef(
            Matrix<double> dualCoef,
            int[] nSupport,
            Matrix<double> supportVectors)
        {
            // get 1vs1 weights for all n*(n-1) classifiers.
            // this is somewhat messy.
            // shape of dual_coef_ is nSV * (n_classes -1)
            // see docs for details
            int nClass = dualCoef.RowCount + 1;

            // XXX we could do preallocation of coef but
            // would have to take care in the sparse case
            var coef = new List<Vector<double>>();
            var svLocs = CumSum(new[] { 0 }.Concat(nSupport).ToArray());
            for (int class1 = 0; class1 < nClass; class1++)
            {
                // SVs for class1:
                var sv1 = supportVectors.SubMatrix(
                    svLocs[class1],
                    svLocs[class1 + 1] - svLocs[class1],
                    0,
                    supportVectors.ColumnCount);

                for (int class2 = class1 + 1; class2 < nClass; class2++)
                {
                    // SVs for class1:
                    var sv2 = supportVectors.SubMatrix(
                        svLocs[class2],
                        svLocs[class2 + 1] - svLocs[class2],
                        0,
                        supportVectors.ColumnCount);

                    // dual coef for class1 SVs:
                    var alpha1 = dualCoef.Row(class2 - 1).SubVector(
                        svLocs[class1],
                        svLocs[class1 + 1] - svLocs[class1]);

                    // dual coef for class2 SVs:
                    var alpha2 = dualCoef.Row(class1).SubVector(
                        svLocs[class2],
                        svLocs[class2 + 1] - svLocs[class2]);

                    // build weight for class1 vs class2
                    coef.Add((alpha1 * sv1) + (alpha2 * sv2));
                }
            }

            return coef.ToArray();
        }
        private static IList<string> ColumnsHead(HtmlNode head)
        {
#if NET20
            var rows = IEnumerableExtensionMethods.ToList(head.Descendants("tr"));
#else
            var rows = head.Descendants("tr").ToList();
#endif
            if (0 == rows.Count)
            {
                return new List<string>();
            }

            var matrix = new Matrix<string>();
            foreach (var cell in rows[0].Descendants("th"))
            {
                var colspan = cell.Attributes["colspan"];
                if (null == colspan)
                {
                    matrix.Width++;
                    continue;
                }

                for (var i = 0; i < XmlConvert.ToInt32(colspan.Value); i++)
                {
                    matrix.Width++;
                }
            }

            var carry = new List<int>();
            for (var i = 0; i < matrix.Width; i++)
            {
                carry.Add(0);
            }

            var y = 0;
            foreach (var row in rows)
            {
                var x = 0;
                matrix.Height++;
                foreach (var cell in row.Descendants("th"))
                {
                    while (0 != carry[x])
                    {
                        matrix[x, y] = matrix[x, y - 1];
                        carry[x]--;
                        x++;
                    }

                    var rowspan = cell.Attributes["rowspan"];
                    if (null != rowspan)
                    {
                        carry[x] = XmlConvert.ToInt32(rowspan.Value);
                    }

                    var colspan = cell.Attributes["colspan"];
                    var name = ColumnName(cell);
                    var index = 1;
                    for (var i = 0; i < (null == colspan ? 1 : XmlConvert.ToInt32(colspan.Value)); i++)
                    {
                        matrix[x++, y] = string.Format(CultureInfo.InvariantCulture, null == colspan ? "{0}" : "{0} ({1})", name, index++);
                    }
                }

                y++;
            }

#if NET20
            var list = new List<string>();
            foreach (var element in matrix.Row(matrix.Height - 1))
            {
                list.Add(element);
            }

            return list;
#else
            return matrix.Row(matrix.Height - 1).ToList();
#endif
        }
Example #11
0
        private List<CorrelatedWord> GetCorrelateedWords(string inputWord, List<string> allWords, Matrix<double> wordsWordsMatrix)
        {
            var stemedInputWord = _ps.stemTerm(inputWord);
            var simillarWords = allWords.Where(w => _ps.stemTerm(w).Equals(stemedInputWord));
            var simillarWordsIndexes = simillarWords.Select(w => allWords.IndexOf(w));
            var correlatedWords = new List<CorrelatedWord>();
            foreach (var correlatedWordIndex in simillarWordsIndexes)
            {
                for (int i = 0; i < wordsWordsMatrix.Row(correlatedWordIndex).Count; i++)
                {
                    correlatedWords.Add(new CorrelatedWord() { Word = allWords[i], Correlation = wordsWordsMatrix.Row(correlatedWordIndex)[i] });
                }
            }

            return correlatedWords;
        }
Example #12
0
        public void op_Row_int_whenEmpty()
        {
            var matrix = new Matrix<string>();

            Assert.Throws<ArgumentOutOfRangeException>(() => matrix.Row(0).ToList());
        }
Example #13
0
        public void op_Row_intNegative()
        {
            var matrix = new Matrix<string>();

            Assert.Throws<ArgumentOutOfRangeException>(() => matrix.Row(-1).ToList());
        }
Example #14
0
        public void op_Row_int()
        {
            var matrix = new Matrix<string>(3, 3);
            matrix[0, 0] = "0,0";
            matrix[0, 1] = "0,1";
            matrix[0, 2] = "0,2";
            matrix[1, 0] = "1,0";
            matrix[1, 1] = "1,1";
            matrix[1, 2] = "1,2";
            matrix[2, 0] = "2,0";
            matrix[2, 1] = "2,1";
            matrix[2, 2] = "2,2";

            for (var y = 0; y < 3; y++)
            {
                var x = 0;
                foreach (var actual in matrix.Row(y))
                {
                    var expected = "{0},{1}".FormatWith(x, y);

                    Assert.Equal(expected, actual);
                    x++;
                }
            }
        }
Example #15
0
        private void CalcSquaredError(Matrix<double> trainingData, Matrix<double> trainingClasses, out double squaredError, out double percentError)
        {
            squaredError = 0;
            percentError = 0;
            for (int i = 0; i < trainingData.RowCount; i++)
            {
                var a = Classify(trainingData.Row(i));
                var expected = trainingClasses.Row(i);

                squaredError += (a.Subtract(expected).Select(x => x * x).Sum());

                if (expected[a.MaximumIndex()] == 0)
                    percentError++;
            }

            percentError /= trainingData.RowCount;
        }
Example #16
0
        private static void ComputeSimilarities(Matrix<double> R, 
            Metric.SimilarityMetric similarityMetric, int maxCountOfNeighbors,
            double minSimilarityThreshold,  out SimilarityData neighborsByObject,
            out HashSet<Tuple<int, int>> strongSimilarityIndicators)
        {
            int dimension = R.RowCount;
            List<Vector<double>> rows = new List<Vector<double>>(R.EnumerateRows());

            // I assume that the rows are enumerated from first to last
            Debug.Assert(rows[0].Sum() == R.Row(0).Sum());
            Debug.Assert(rows[rows.Count - 1].Sum() == R.Row(rows.Count - 1).Sum());

            List<Tuple<int, int>> strongSimilarityIndicators_out = new List<Tuple<int, int>>();

            SimilarityData neighborsByObject_out = new SimilarityData(maxCountOfNeighbors);

            Object lockMe = new Object();
            Parallel.For(0, dimension, indexOfRow =>
            {
                Utils.PrintEpoch("Progress current/total", indexOfRow, dimension);
                Dictionary<Tuple<int, int>,double> similarityCache = new Dictionary<Tuple<int, int>,double>();
                List<Tuple<int, int>> strongSimilarityIndocatorCache = new List<Tuple<int, int>>();

                for (int indexOfNeighbor = 0; indexOfNeighbor < dimension; indexOfNeighbor++)
                {
                    if (indexOfRow == indexOfNeighbor) { continue; } // Skip self similarity

                    else if (indexOfRow > indexOfNeighbor)
                    {
                        switch (similarityMetric)
                        {
                            case Metric.SimilarityMetric.CosineRating:
                                // TODO: make a note that it really matters to make it sparse, it computes differently!
                                double cosine = Metric.CosineR((SparseVector)rows[indexOfRow], (SparseVector)rows[indexOfNeighbor]);
                                    if(cosine >  minSimilarityThreshold)
                                    {
                                        strongSimilarityIndocatorCache.Add(new Tuple<int, int>(indexOfRow, indexOfNeighbor));
                                        strongSimilarityIndocatorCache.Add(new Tuple<int, int>(indexOfNeighbor, indexOfRow));
                                    }
                                    similarityCache[new Tuple<int, int>(indexOfRow, indexOfNeighbor)] = cosine;
                                    similarityCache[new Tuple<int, int>(indexOfNeighbor, indexOfRow)] = cosine;

                                break;
                            case Metric.SimilarityMetric.PearsonRating:
                                double pearson = Metric.PearsonR((SparseVector)rows[indexOfRow], (SparseVector)rows[indexOfNeighbor]);
                                    if (pearson> minSimilarityThreshold)
                                    {
                                        strongSimilarityIndocatorCache.Add(new Tuple<int, int>(indexOfRow, indexOfNeighbor));
                                        strongSimilarityIndocatorCache.Add(new Tuple<int, int>(indexOfNeighbor, indexOfRow));
                                    }
                                    similarityCache[new Tuple<int, int>(indexOfRow, indexOfNeighbor)] = pearson;
                                    similarityCache[new Tuple<int, int>(indexOfNeighbor, indexOfRow)] = pearson;

                                break;
                        }
                    }
                }

                lock (lockMe)
                {
                    foreach(var entry in similarityCache)
                    {
                        neighborsByObject_out.AddSimilarityData(entry.Key.Item1, entry.Key.Item2, entry.Value);
                    }
                    strongSimilarityIndicators_out.AddRange(strongSimilarityIndocatorCache);
                }
            });

            neighborsByObject = neighborsByObject_out;
            neighborsByObject.SortAndRemoveNeighbors();
            strongSimilarityIndicators = new HashSet<Tuple<int,int>>(strongSimilarityIndicators_out);
        }
Example #17
0
 //Lista di indice e index รจ indice nella lista
 public static double WeightedDegree(Matrix<double> res, int index)
 {
     return ((1.0 / res.ColumnCount) * (res.Row(index).Sum()));
 }
Example #18
0
 private Matrix<Double> addBias(Matrix<Double> inMatrix)
 {
     var patternsWithBias = Matrix<double>.Build.Dense(inMatrix.RowCount + 1, inMatrix.ColumnCount);
     for (int r = 0; r < inMatrix.RowCount; r++)
         patternsWithBias.SetRow(r, inMatrix.Row(r));
     //patternsWithBias.Row(patternsWithBias.RowCount - 1).SetValues(Enumerable.Repeat(1.0, patternsWithBias.ColumnCount).ToArray());
     for (int i = 0; i < patternsWithBias.ColumnCount; i++)
         patternsWithBias[patternsWithBias.RowCount - 1, i] = 1.0;
     return patternsWithBias;
 }
Example #19
0
        private Tuple<Matrix<double>, Matrix<double>> CalcNewWeights(Matrix<double> trainingData, Matrix<double> trainingClasses)
        {
            Matrix<double> localHL = hiddenLayer.Clone();
            Matrix<double> localOL = outputLayer.Clone();
            for (int i = 0; i < trainingData.RowCount; i++)
            {
                var sample = trainingData.Row(i);
                var target = trainingClasses.Row(i);
                UpdateWeightsIndividual(ref localHL, ref localOL, sample, target);

            }

            return new Tuple<Matrix<double>, Matrix<double>>(localHL, localOL);
        }