Ejemplo n.º 1
0
        //---------------------------------------------------------------------------------------
        //This function computes the Confident Matrix and Weight Matrix, forllowing the formulars
        //---------------------------------------------------------------------------------------
        public void ComputeConfident(Predict_DAO_MCol dao)
        {
            dao.CLEAN_CONF_Matrix();
            dao.CLEAN_WEIG_Matrix();

            List<string> list_ClusterID = dao.getAllClusterID();
            foreach (var item in list_ClusterID)
            {
                List<Transac> u_transacs = dao.getTransac_ForMatrix_ByClusterID_V2(item);
                if (u_transacs.Count > 0)
                {
                    // Get QuantityMatrix - MC
                    Dictionary<string, int> QM_dic_users = new Dictionary<string, int>();
                    Dictionary<string, int> QM_dic_items = new Dictionary<string, int>();
                    double[][] QuantityMatrix = Util.getQuantityMatrix_FromTransac_ByMetaItemID(u_transacs, out QM_dic_users, out QM_dic_items);

                    if (QuantityMatrix[0].Length > 1)
                    {
                        // Compute Support One Item Matrix - MC
                        double[] S = new double[QuantityMatrix[0].Length];
                        for (int j = 0; j < QuantityMatrix[0].Length; j++)
                        {
                            double count = 0.0;
                            for (int i = 0; i < QuantityMatrix.Length; i++)
                                if (QuantityMatrix[i][j] > 0)
                                    count++;
                            S[j] = count;
                        }

                        // Compute Support Matrix - MC
                        double[][] SupportMatrix = new double[QuantityMatrix[0].Length][];
                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                            SupportMatrix[i] = new double[QuantityMatrix[0].Length];

                        for (int i = 0; i < QuantityMatrix.Length; i++)
                        {
                            for (int j = 0; j < QuantityMatrix[0].Length; j++)
                                for (int j2 = 0; j2 < QuantityMatrix[0].Length; j2++)
                                    if (QuantityMatrix[i][j] > 0 & QuantityMatrix[i][j2] > 0 & j != j2)
                                        SupportMatrix[j][j2]++;
                        }

                        // Compute CONF Matrix - MC
                        double[][] CONF = new double[QuantityMatrix[0].Length][];
                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                            CONF[i] = new double[QuantityMatrix[0].Length];

                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                        {
                            for (int j = 0; j < QuantityMatrix[0].Length; j++)
                                if (i == j)
                                    CONF[i][j] = 1.0;
                                else if (S[i] > 0)
                                    CONF[i][j] = SupportMatrix[i][j] / S[i];
                        }

                        // Compute WEIG Matrix - MC
                        double[][] WEIG = new double[QuantityMatrix[0].Length][];
                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                            WEIG[i] = new double[QuantityMatrix[0].Length];

                        for (int i = 0; i < QuantityMatrix[0].Length; i++)
                        {
                            for (int j = 0; j < QuantityMatrix[0].Length; j++)
                                if (i != j)
                                    WEIG[i][j] = computeWEIG(i, j, SupportMatrix, QuantityMatrix);
                        }

                        // Save CONF Matrix - MC
                        for (int i = 0; i < CONF.Length; i++)
                            for (int j = 0; j < CONF[0].Length; j++)
                                if (CONF[i][j] > 0)
                                {
                                    MatrixItem matrixItem = new MatrixItem();
                                    matrixItem.Row = Util.FindKeyByValue(QM_dic_items, i);
                                    matrixItem.Column = Util.FindKeyByValue(QM_dic_items, j);
                                    matrixItem.Cell = CONF[i][j];
                                    matrixItem.ClusterID = item;
                                    dao.setCONF_Matrix(matrixItem);
                                }

                        // Save WEIG Matrix
                        for (int i = 0; i < WEIG.Length; i++)
                            for (int j = 0; j < WEIG[0].Length; j++)
                                if (WEIG[i][j] > 0)
                                {
                                    MatrixItem matrixItem = new MatrixItem();
                                    matrixItem.Row = Util.FindKeyByValue(QM_dic_items, i);
                                    matrixItem.Column = Util.FindKeyByValue(QM_dic_items, j);
                                    matrixItem.Cell = WEIG[i][j];
                                    matrixItem.ClusterID = item;
                                    dao.setWEIG_Matrix(matrixItem);
                                }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        //---------------------------------------------------------------------------------------
        //This function computes the DISTANCE matrix and inserts it into the database
        //This is the distance between the two columns of the Rating Matrix
        //---------------------------------------------------------------------------------------
        public void ComputeDIST(Predict_DAO_MCol dao)
        {
            //dao.CLEAN_DIST_Matrix();

            List<string> list_ClusterID = dao.getAllClusterID();
            foreach (var item in list_ClusterID)
            {
                Dictionary<string, int> RM_dic_users = new Dictionary<string, int>();
                Dictionary<string, int> RM_dic_items = new Dictionary<string, int>();
                // Compute ratting matrix
                List<MatrixItem> lstRMI = dao.getRattingMatrixItem_ByClusterID(item);
                double[][] RattingMatrix = Util.toMatrix_MatrixItem(lstRMI, out RM_dic_users, out RM_dic_items);

                if (RattingMatrix.Length > 0 & RM_dic_items.Count > 1)
                {
                    double[][] DIST = new double[RattingMatrix[0].Length][];
                    for (int i = 0; i < RattingMatrix[0].Length; i++)
                        DIST[i] = new double[RattingMatrix[0].Length];

                    // Compute DIST Matrix
                    for (int i1 = 0; i1 < RattingMatrix[0].Length - 1; i1++)
                        for (int i2 = i1 + 1; i2 < RattingMatrix[0].Length; i2++)
                        {
                            double value = 0.0;
                            for (int t = 0; t < RattingMatrix.Length; t++)
                                value += Math.Pow(RattingMatrix[t][i1] - RattingMatrix[t][i2], 2);
                            value = Math.Sqrt(value);
                            DIST[i1][i2] = value;
                            DIST[i2][i1] = value;
                        }

                    if (DIST.Length > 0)
                    {
                        double max = DIST[0][0];
                        // Find Max
                        for (int i = 0; i < DIST.Length; i++)
                        {
                            double localMax = DIST[i].Max();
                            if (localMax > max)
                                max = localMax;
                        }

                        // Standardization
                        if (max != 0)
                        {
                            for (int i = 0; i < DIST.Length; i++)
                                for (int j = 0; j < DIST[0].Length; j++)
                                    DIST[i][j] /= max;
                        }

                        // Save to Database
                        for (int i = 0; i < DIST.Length; i++)
                            for (int j = 0; j < DIST[0].Length; j++)
                                if (DIST[i][j] > 0)
                                {
                                    MatrixItem matrixItem = new MatrixItem();
                                    matrixItem.Row = Util.FindKeyByValue(RM_dic_items, i);
                                    matrixItem.Column = Util.FindKeyByValue(RM_dic_items, j);
                                    matrixItem.Cell = DIST[i][j];
                                    matrixItem.ClusterID = item;
                                    dao.setDIST_Matrix(matrixItem);
                                }
                    }
                }
            }
        }