Examples of SparseMatrix


Examples of ch.akuhn.hapax.linalg.SparseMatrix

    private SparseMatrix matrix;
    private AssociativeList<String> terms; // rows


    public TermDocumentMatrix() {
        this.matrix = new SparseMatrix(0, 0);
        this.terms = new AssociativeList<String>();
        this.documents = new AssociativeList<String>();
        this.lengthArray = new IntArray();
    }
View Full Code Here

Examples of ch.akuhn.hapax.linalg.SparseMatrix

        this.documents = new AssociativeList<String>();
        this.lengthArray = new IntArray();
    }

    private TermDocumentMatrix(AssociativeList<String> terms, AssociativeList<String> documents, IntArray lengthArray) {
        this.matrix = new SparseMatrix(terms.size(), documents.size());
        this.terms = terms.clone();
        this.documents = documents.clone();
        this.lengthArray = lengthArray.clone();
    }
View Full Code Here

Examples of ch.akuhn.matrix.SparseMatrix

  }

  public static void main(String... args) {
    Stopwatch.enter();
    Stopwatch.p();
    SparseMatrix A = Matrix.sparse(4000, 10000);
    Random rand = new Random(1);
    for (int i = 0; i < A.rowCount(); i++) {
      for (int j = 0; j < A.columnCount(); j++) {
        if (rand.nextDouble() > 0.2) continue;
        A.put(i, j, rand.nextDouble() * 23);
      }
    }
    SingularValues singular = new SingularValues(A, 10).decompose();
    Stopwatch.p();
    Out.puts(singular.value);
View Full Code Here

Examples of com.rapidminer.data.SparseMatrix

    if (entity_type !=0 && entity_type != 1)
      throw new IllegalArgumentException("entity type must be either USER or ITEM, not " + entity_type);

    ArrayList<ArrayList<Integer>> ratings_by_other_entity = (entity_type == 0) ? ratings.ByItem() : ratings.ByUser();

    SparseMatrix freqs   = new SparseMatrix(num_entities, num_entities);
    SparseMatrix_d i_sums  = new SparseMatrix_d(num_entities, num_entities);
    SparseMatrix_d j_sums  = new SparseMatrix_d(num_entities, num_entities);
    SparseMatrix_d ij_sums = new SparseMatrix_d(num_entities, num_entities);
    SparseMatrix_d ii_sums = new SparseMatrix_d(num_entities, num_entities);
    SparseMatrix_d jj_sums = new SparseMatrix_d(num_entities, num_entities);

    for(int i1=0;i1<ratings_by_other_entity.size();i1++){
      ArrayList<Integer> other_entity_ratings = ratings_by_other_entity.get(i1);
      for (int i = 0; i < other_entity_ratings.size(); i++)
      {
        Integer index1 = other_entity_ratings.get(i);
        int x = (entity_type == 0) ? ratings.GetUsers().get(index1) : ratings.GetItems().get(index1);

        // update pairwise scalar product and frequency
            for (int j = i + 1; j < other_entity_ratings.size(); j++)
        {
          Integer index2 = other_entity_ratings.get(j);
          int y = (entity_type == 0) ? ratings.GetUsers().get(index2) : ratings.GetItems().get(index2);

          double rating1 = ratings.GetValues(index1);
          double rating2 = ratings.GetValues(index2);
         

          // update sums
          if (x < y)
          {
            freqs.setLocation(x, y, freqs.getLocation1(x, y)+1);
            i_sums.setLocation(x, y,i_sums.getLocation1(x, y)+rating1);
            j_sums.setLocation(x, y, j_sums.getLocation1(x, y)+rating2);
            ij_sums.setLocation(x, y,ij_sums.getLocation1(x, y)+rating1*rating2);
            ii_sums.setLocation(x, y, ii_sums.getLocation1(x, y)+rating1*rating1);
            jj_sums.setLocation(x, y, jj_sums.getLocation1(x, y)+rating2*rating2);
          }
          else
          {
            freqs.setLocation(y, x, freqs.getLocation1(y, x)+1);
            i_sums.setLocation(y, x, i_sums.getLocation1(y, x)+rating1);
            j_sums.setLocation(y, x, j_sums.getLocation1(y, x)+rating2);
            ij_sums.setLocation(y, x, ij_sums.getLocation1(y, x)+rating1*rating2);
            ii_sums.setLocation(y, x, ii_sums.getLocation1(y, x)+rating1*rating1);
            jj_sums.setLocation(y, x, jj_sums.getLocation1(y, x)+rating2*rating2);
          }
            }
      }
    }
   
    
     for (int i = 0; i < num_entities; i++)
        this.setLocation(i, i, 1);
   
     List<Tupel<Integer,Integer>> elementi=freqs.NonEmptyEntryIDs();
   
    // fill the entries with interactions
    for (int i1=0;i1<elementi.size();i1++)
    {
     
      Tupel<Integer,Integer> par=elementi.get(i1);
     
      int i=par.getFirst();
      int j=par.getSecond();
      int n = freqs.getLocation(i, j);
     
     
      if (n < 2)
      {
        this.setLocation(i, j, 0);
View Full Code Here

Examples of com.rapidminer.data.SparseMatrix

    public void RetrainUsers(List<Integer> users){
      super.RetrainUsers(users);
     
        ArrayList<ArrayList<Integer>> ratings_by_other_entity = ratings.ByUser();

        SparseMatrix freqs   = new SparseMatrix(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
        SparseMatrix_d i_sums  = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
        SparseMatrix_d j_sums  = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
        SparseMatrix_d ij_sums = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
        SparseMatrix_d ii_sums = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
        SparseMatrix_d jj_sums = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());

        for(int i1=0;i1<users.size();i1++){
          ArrayList<Integer> other_entity_ratings = ratings_by_other_entity.get(users.get(i1));
          for (int i = 0; i < other_entity_ratings.size(); i++)
          {
            Integer index1 = other_entity_ratings.get(i);
            int x =  ratings.GetUsers().get(index1);

            // update pairwise scalar product and frequency
               for (int j = i + 1; j < other_entity_ratings.size(); j++)
            {
              Integer index2 = other_entity_ratings.get(j);
              int y = ratings.GetUsers().get(index2);

              double rating1 = ratings.GetValues(index1);
              double rating2 = ratings.GetValues(index2);
             

              // update sums
              if (x < y)
              {
                freqs.setLocation(x, y, freqs.getLocation1(x, y)+1);
                i_sums.setLocation(x, y,i_sums.getLocation1(x, y)+rating1);
                j_sums.setLocation(x, y, j_sums.getLocation1(x, y)+rating2);
                ij_sums.setLocation(x, y,ij_sums.getLocation1(x, y)+rating1*rating2);
                ii_sums.setLocation(x, y, ii_sums.getLocation1(x, y)+rating1*rating1);
                jj_sums.setLocation(x, y, jj_sums.getLocation1(x, y)+rating2*rating2);
              }
              else
              {
                freqs.setLocation(y, x, freqs.getLocation1(y, x)+1);
                i_sums.setLocation(y, x, i_sums.getLocation1(y, x)+rating1);
                j_sums.setLocation(y, x, j_sums.getLocation1(y, x)+rating2);
                ij_sums.setLocation(y, x, ij_sums.getLocation1(y, x)+rating1*rating2);
                ii_sums.setLocation(y, x, ii_sums.getLocation1(y, x)+rating1*rating1);
                jj_sums.setLocation(y, x, jj_sums.getLocation1(y, x)+rating2*rating2);
              }
               }
          }
        }
       
         List<Tupel<Integer,Integer>> elementi=freqs.NonEmptyEntryIDs();
       
        // fill the entries with interactions
        for (int i1=0;i1<elementi.size();i1++)
        {
         
          Tupel<Integer,Integer> par=elementi.get(i1);
         
          int i=par.getFirst();
          int j=par.getSecond();
          int n = freqs.getLocation(i, j);
         
         
          if (n < 2)
          {
            continue;
View Full Code Here

Examples of com.rapidminer.data.SparseMatrix

      correlation.Grow(correlation.NumberOfRows()+users.size(), correlation.NumberOfColumns()+users.size());
      correlation.Setnum_entities(MaxUserID+1);
     
      IMatrix_b transpose = data_user.Transpose();
     
      SparseMatrix overlap = new SparseMatrix(data_user.NumberOfRows(), data_user.NumberOfRows());
      com.rapidminer.data.CompactHashSet<Integer> viewed=new com.rapidminer.data.CompactHashSet<Integer>();
      int prevus=-1;
     
      for (int row_id = 0; row_id <items.size(); row_id++) //should be item sorted
      {
        List<Integer> row = ((IBooleanMatrix) transpose).GetEntriesByRow(items.get(row_id));
        if(prevus!=users.get(row_id))
           viewed.clear();
       

          int x=users.get(row_id);
   
          for (int j = 0; j < row.size(); j++)
          {
            int y = row.get(j);
            if(viewed.contains(y))
              continue;
           
            if (x < y){
             
              int t=overlap.getLocation1(x, y);
              t++;
              overlap.setLocation(x, y, t);
            }
            else{
             
              int t=overlap.getLocation1(y, x);
              t++;
              overlap.setLocation(y, x, t);
            }

          }
          prevus=users.get(row_id);
      }
     
      List<Tupel<Integer,Integer>> temp=overlap.NonEmptyEntryIDs();

     
      for(int i=0;i<temp.size();i++){
       
        int x=temp.get(i).getFirst();
       
        int y=temp.get(i).getSecond();
        
         float value= (float) (overlap.getLocation(x, y) / Math.sqrt(data_user.NumEntriesByRow(x) * data_user.NumEntriesByRow(y) ));

         this.correlation.setLocation(x, y, this.correlation.getLocation(x, y)*0.9999f+value);
      }
     
      return 1;
View Full Code Here

Examples of com.rapidminer.data.SparseMatrix

     correlation.Setnum_entities(MaxItemID+1);
    
     IMatrix_b transpose = data_item.Transpose();

     
      SparseMatrix overlap = new SparseMatrix(data_item.NumberOfRows(), data_item.NumberOfRows());
      com.rapidminer.data.CompactHashSet<Integer> viewed=new com.rapidminer.data.CompactHashSet<Integer>();
      int prevus=-1;

      for (int row_id = 0; row_id < users.size(); row_id++)
      {
        List<Integer> row = ((IBooleanMatrix) transpose).GetEntriesByRow(users.get(row_id));
        if(prevus!=users.get(row_id))
           viewed.clear();
       
          int x=items.get(row_id);
          viewed.add(x);
     
          for (int j = 0; j < row.size(); j++)
          {
            int y = row.get(j);

            if (x < y){
             
              int t=overlap.getLocation1(x, y);
              t++;
              overlap.setLocation(x, y, t);
            }
            else{
             
              int t=overlap.getLocation1(y, x);
              t++;
              overlap.setLocation(y, x, t);
            }
            prevus=users.get(row_id);
        }
      }
     
      List<Tupel<Integer,Integer>> temp=overlap.NonEmptyEntryIDs();

     
      for(int i=0;i<temp.size();i++){
       
        int x=temp.get(i).getFirst();
       
        int y=temp.get(i).getSecond();
        
         float value= (float) (overlap.getLocation(x, y) / Math.sqrt(data_item.NumEntriesByRow(x) * data_item.NumEntriesByRow(y) ));

         this.correlation.setLocation(x, y, this.correlation.getLocation(x, y)*0.9999f+value);
      }
    
   
View Full Code Here

Examples of com.rapidminer.data.SparseMatrix

  public void RetrainItems(List<Integer> items){
     super.RetrainItems(items);
    
     ArrayList<ArrayList<Integer>> ratings_by_other_entity = ratings.ByItem();

    SparseMatrix freqs   = new SparseMatrix(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
    SparseMatrix_d i_sums  = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
    SparseMatrix_d j_sums  = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
    SparseMatrix_d ij_sums = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
    SparseMatrix_d ii_sums = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());
    SparseMatrix_d jj_sums = new SparseMatrix_d(this.correlation.Getnum_entities(), this.correlation.Getnum_entities());

    for(int i1=0;i1<items.size();i1++){
      ArrayList<Integer> other_entity_ratings = ratings_by_other_entity.get(items.get(i1));
      for (int i = 0; i < other_entity_ratings.size(); i++)
      {
        Integer index1 = other_entity_ratings.get(i);
        int x =  ratings.GetItems().get(index1);

        // update pairwise scalar product and frequency
           for (int j = i + 1; j < other_entity_ratings.size(); j++)
        {
          Integer index2 = other_entity_ratings.get(j);
          int y = ratings.GetItems().get(index2);

          double rating1 = ratings.GetValues(index1);
          double rating2 = ratings.GetValues(index2);
         

          // update sums
          if (x < y)
          {
            freqs.setLocation(x, y, freqs.getLocation1(x, y)+1);
            i_sums.setLocation(x, y,i_sums.getLocation1(x, y)+rating1);
            j_sums.setLocation(x, y, j_sums.getLocation1(x, y)+rating2);
            ij_sums.setLocation(x, y,ij_sums.getLocation1(x, y)+rating1*rating2);
            ii_sums.setLocation(x, y, ii_sums.getLocation1(x, y)+rating1*rating1);
            jj_sums.setLocation(x, y, jj_sums.getLocation1(x, y)+rating2*rating2);
          }
          else
          {
            freqs.setLocation(y, x, freqs.getLocation1(y, x)+1);
            i_sums.setLocation(y, x, i_sums.getLocation1(y, x)+rating1);
            j_sums.setLocation(y, x, j_sums.getLocation1(y, x)+rating2);
            ij_sums.setLocation(y, x, ij_sums.getLocation1(y, x)+rating1*rating2);
            ii_sums.setLocation(y, x, ii_sums.getLocation1(y, x)+rating1*rating1);
            jj_sums.setLocation(y, x, jj_sums.getLocation1(y, x)+rating2*rating2);
          }
           }
      }
    }
   
     List<Tupel<Integer,Integer>> elementi=freqs.NonEmptyEntryIDs();
   
    // fill the entries with interactions
    for (int i1=0;i1<elementi.size();i1++)
    {
     
      Tupel<Integer,Integer> par=elementi.get(i1);
     
      int i=par.getFirst();
      int j=par.getSecond();
      int n = freqs.getLocation(i, j);
     
     
      if (n < 2)
      {
        continue;
View Full Code Here

Examples of com.rapidminer.data.SparseMatrix

      if(users==null || items==null)
        return 1;
     
      if(users.size()!=0){
      IMatrix_b transpose = GetFeedback().GetItemMatrix().Transpose();
      SparseMatrix overlap = new SparseMatrix(GetFeedback().GetItemMatrix().NumberOfRows(), GetFeedback().GetItemMatrix().NumberOfRows());
      com.rapidminer.data.CompactHashSet<Integer> viewed=new com.rapidminer.data.CompactHashSet<Integer>();
      int prevus=-1;
     
      for(int i1=0;i1<users.size();i1++)//users list must be sorted
      {
        List<Integer> row = ((IBooleanMatrix) transpose).GetEntriesByRow(users.get(i1));
        if(prevus!=users.get(i1))
           viewed.clear();
       
          int x=items.get(i1);
          viewed.add(x);
         
          for (int j = 0; j < row.size(); j++)
          {
            int y = row.get(j);
           
            if(viewed.contains(y))
              continue;
             
            if (x < y){
             
              int t=overlap.getLocation1(x, y);
              t++;
              overlap.setLocation(x, y, t);
            }
            else{
             
              int t=overlap.getLocation1(y, x);
              t++;
              overlap.setLocation(y, x, t);
            }

          }
          prevus=users.get(i1);
      }
     
      List<Tupel<Integer,Integer>> temp=overlap.NonEmptyEntryIDs();
     
      for(int i=0;i<temp.size();i++){
       
        int x=temp.get(i).getFirst();
       
        int y=temp.get(i).getSecond();
        
         float value= (float) (overlap.getLocation(x, y) / Math.sqrt(GetFeedback().GetItemMatrix().NumEntriesByRow(x) * GetFeedback().GetItemMatrix().NumEntriesByRow(y) ));
         correlation.setLocation(x, y,correlation.getLocation(x, y)*0.9999f+value);
      }
    }
      return 1;
    }
View Full Code Here

Examples of edu.ucla.sspace.matrix.SparseMatrix

        for (Integer index : indicesToKeep)
            indexMap.put(index, newIndex++);

        // Create a reduced matrix that will have only the selected columns in
        // the final space.
        SparseMatrix reduced = new YaleSparseMatrix(
                words, indicesToKeep.size());

        // Iterate over the sparse values in the matrix for added efficiency.
        for (int row = 0; row < words; ++ row) {
            SparseDoubleVector sv = cooccurrenceMatrix.getRowVector(row);
            for (int col : sv.getNonZeroIndices()) {
                double v = cooccurrenceMatrix.get(row, col);

                // If the original column was retained, get it's new index
                // value and add it to the reduced matrix.
                Integer newColIndex = indexMap.get(col);
                if (newColIndex != null)
                    reduced.set(row, newColIndex, v);

                // If the transposed row column was retained, get it's new index
                // value and add it to the reduced matrix.  This turns the col
                // value into the row and the new index as the column.
                newColIndex = indexMap.get(row + words);
                if (newColIndex != null)
                    reduced.set(col, newColIndex, v);
            }
        }

        return reduced;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.