Exemple #1
0
        /// <summary>
        ///     Calculates the rotation and translation from a set of points in one coordinate system (orig) to another (transf).
        ///     You can use to create a new Matrix4 and transform points from (orig) to (trans) or take the inverse of the
        ///     resulting Matrix4
        ///     and transform (transf) back to (orig).
        /// </summary>
        /// <param name="orig">the points in the first coordinate system</param>
        /// <param name="transf">theh points in the second coordinate system</param>
        /// <returns>a tuple containing the rotation matrix as Item1 and the translation vector as Item2</returns>
        public static Matrix4 TransformBetween(IEnumerable <Vector3> orig, IEnumerable <Vector3> transf)
        {
            int rowCountA = orig.Count();
            int rowCountB = transf.Count();

            if (rowCountA != rowCountB)
            {
                throw new Exception("Data must be paired. The number of rows should be equal in both input matrices!");
            }
            Vector3 aCentroid = orig.GetCentroid();
            Vector3 bCentroid = transf.GetCentroid();

            double[,] aCentroidMatrix = aCentroid.RepMat(rowCountA);
            double[,] bCentroidMatrix = bCentroid.RepMat(rowCountB);

            double[,] a = orig.ToRowMatrix();
            double[,] b = transf.ToRowMatrix();

            double[,] h = (a.Subtract(aCentroidMatrix)).Transpose().Multiply(b.Subtract(bCentroidMatrix));

            SvdResult svd = h.Svd();

            double[,] U = svd.U;
            double[,] V = svd.Vt.Transpose();

            //TODO Consider reflection
            double[,] r     = V.Multiply(U.Transpose());
            double[,] first = r.Multiply(-1);
            double[] second = first.Multiply(aCentroid);
            double[] t      = r.Multiply(-1).Multiply(aCentroid).Add(bCentroid);
            return(new Matrix4(new Matrix3(r), new Vector3(t)));
        }
Exemple #2
0
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserArticleRatingsTable(rater);

            SingularValueDecomposition factorizer = new SingularValueDecomposition(numFeatures, learningIterations);

            svd = factorizer.FactorizeMatrix(ratings);

            numUsers    = ratings.UserIndexToID.Count;
            numArticles = ratings.ArticleIndexToID.Count;
        }
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserArticleRatingsTable(rater);

            if (latentUserFeatureCount > 0)
            {
                SingularValueDecomposition svd = new SingularValueDecomposition(latentUserFeatureCount, 100);
                SvdResult results = svd.FactorizeMatrix(ratings);

                ratings.AppendUserFeatures(results.UserFeatures);
            }
        }
Exemple #4
0
        public void Load(string file)
        {
            ratings = new UserArticleRatingsTable();

            using (FileStream fs = new FileStream(file, FileMode.Open))
                using (GZipStream zip = new GZipStream(fs, CompressionMode.Decompress))
                    using (StreamReader r = new StreamReader(zip))
                    {
                        numUsers    = int.Parse(r.ReadLine());
                        numArticles = int.Parse(r.ReadLine());
                        numFeatures = int.Parse(r.ReadLine());

                        double averageGlobalRating = double.Parse(r.ReadLine());

                        double[] userBiases = new double[numUsers];
                        for (int userIndex = 0; userIndex < numUsers; userIndex++)
                        {
                            userBiases[userIndex] = double.Parse(r.ReadLine());
                        }

                        double[] articleBiases = new double[numArticles];
                        for (int articleIndex = 0; articleIndex < numArticles; articleIndex++)
                        {
                            articleBiases[articleIndex] = double.Parse(r.ReadLine());
                        }

                        double[][] userFeatures = new double[numUsers][];
                        for (int userIndex = 0; userIndex < numUsers; userIndex++)
                        {
                            userFeatures[userIndex] = new double[numFeatures];

                            for (int featureIndex = 0; featureIndex < numFeatures; featureIndex++)
                            {
                                userFeatures[userIndex][featureIndex] = double.Parse(r.ReadLine());
                            }
                        }

                        double[][] articleFeatures = new double[numArticles][];
                        for (int articleIndex = 0; articleIndex < numUsers; articleIndex++)
                        {
                            articleFeatures[articleIndex] = new double[numFeatures];

                            for (int featureIndex = 0; featureIndex < numFeatures; featureIndex++)
                            {
                                articleFeatures[articleIndex][featureIndex] = double.Parse(r.ReadLine());
                            }
                        }

                        svd = new SvdResult(averageGlobalRating, userBiases, articleBiases, userFeatures, articleFeatures);

                        for (int i = 0; i < numUsers; i++)
                        {
                            int userId             = int.Parse(r.ReadLine());
                            UserArticleRatings uat = new UserArticleRatings(userId, numArticles);

                            for (int x = 0; x < numArticles; x++)
                            {
                                uat.ArticleRatings[x] = double.Parse(r.ReadLine());
                            }

                            ratings.Users.Add(uat);
                        }

                        for (int i = 0; i < numUsers; i++)
                        {
                            ratings.UserIndexToID.Add(int.Parse(r.ReadLine()));
                        }

                        for (int i = 0; i < numArticles; i++)
                        {
                            ratings.ArticleIndexToID.Add(int.Parse(r.ReadLine()));
                        }
                    }
        }