Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright [2013-2014] eBay Software Foundation
   *  
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *  
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package ml.shifu.guagua.mapreduce.example.kmeans;
 
 import java.util.List;
 
KMeansWorkerParams is the worker results for KMeans distributed guagua application. Sum values for each k categories are stored and counts are also stored for master to compute global average k center new points.
 
 public class KMeansWorkerParams extends HaltBytable {

    
K category pre-defined
 
     private int k;

    
Dimensions used for each record.
 
     private int c;

    
If first iteration,
 
     private boolean isFirstIteration;

    
For the first iteration, this is the k initial centroids selected in worker. For other iterations, this is sum value of the new centriods in worker iteration.
 
     private List<double[]> pointList;

    
Counts all records for each category.
 
     private List<IntegercountList;
 
     @Override
     public void doWrite(DataOutput outthrows IOException {
         validate();
         out.writeInt(this.);
         out.writeInt(this.);
         for(int i = 0; i < this.i++) {
             for(int j = 0; j < this.j++) {
                 out.writeDouble(this..get(i)[j]);
             }
         }
         out.writeBoolean(this.);
         if(!this.) {
             if(this. != null) {
                 for(int i = 0; i < this.i++) {
                     out.writeInt(this..get(i));
                 }
             }
         }
     }
 
     private void validate() {
         validateK();
         validateC();
         if(this. != this..size()) {
             throw new IllegalArgumentException("In-consistent sum list.");
         }
         if(!this.) {
             if(this. != this..size()) {
                 throw new IllegalArgumentException("In-consistent count list.");
             }
         }
     }
 
     @Override
     public void doReadFields(DataInput inthrows IOException {
         this. = in.readInt();
         validateK();
         this. = in.readInt();
         validateC();
         this. = new LinkedList<double[]>();
        for(int i = 0; i < this.i++) {
            double[] units = new double[this.];
            for(int j = 0; j < this.j++) {
                units[j] = in.readDouble();
            }
            this..add(units);
        }
        boolean isFirstIteration = in.readBoolean();
        if(!isFirstIteration) {
            this. = new LinkedList<Integer>();
            for(int i = 0; i < this.i++) {
                this..add(in.readInt());
            }
        }
    }
    private void validateK() {
        if(this. <= 0) {
            throw new IllegalArgumentException("'k' should be a positive number.");
        }
    }
    private void validateC() {
        if(this. <= 0) {
            throw new IllegalArgumentException("'c' (cloumn number) should be a positive number.");
        }
    }
    public int getK() {
        return ;
    }
    public void setK(int k) {
        this. = k;
    }
    public int getC() {
        return ;
    }
    public void setC(int c) {
        this. = c;
    }
    public List<double[]> getPointList() {
        return ;
    }
    public void setPointList(List<double[]> sumList) {
        this. = sumList;
    }
    public List<IntegergetCountList() {
        return ;
    }
    public void setCountList(List<IntegercountList) {
        this. = countList;
    }
    public boolean isFirstIteration() {
        return ;
    }
    public void setFirstIteration(boolean isFirstIteration) {
        this. = isFirstIteration;
    }
    @Override
    public String toString() {
        return "KMeansWorkerParams [k=" +  + ", c=" +  + ", sumList=" +  + ", countList=" +  + "]";
    }
New to GrepCode? Check out our FAQ X