/** * Draw a instance out from a cluster. * * @param instIdx index of the instance to be drawn out * @param t index of the cluster which the instance previously belong to * @param T the current working partition * @param input the input statistics */ private void reduce_x(int instIdx, int t, Partition T, Input input) { // Update the prior probability of the cluster ArrayList<Integer> indices = T.find(t); double sum = 0.0; for (int i = 0; i < indices.size(); i++) { if (indices.get(i) == instIdx) { continue; } sum += input.Px[indices.get(i)]; } T.Pt[t] = sum; if (T.Pt[t] < 0) { System.out.format("Warning: probability < 0 (%s)\n", T.Pt[t]); T.Pt[t] = 0; } // Update prob of each attribute in the cluster double[][] mArray = input.Pyx.getArray(); for (int i = 0; i < m_numAttributes; i++) { sum = 0.0; for (int j = 0; j < indices.size(); j++) { if (indices.get(j) == instIdx) { continue; } sum += mArray[i][indices.get(j)]; } T.Py_t.set(i, t, sum / T.Pt[t]); } }
/** * Initialize the partition * * @param input object holding the statistics of the training data * @return the initialized partition */ private Partition sIB_InitT(Input input) { Partition T = new Partition(); int avgSize = (int) Math.ceil((double) m_numInstances / m_numCluster); ArrayList<Integer> permInstsIdx = new ArrayList<Integer>(); ArrayList<Integer> unassigned = new ArrayList<Integer>(); for (int i = 0; i < m_numInstances; i++) { unassigned.add(i); } while (unassigned.size() != 0) { int t = random.nextInt(unassigned.size()); permInstsIdx.add(unassigned.get(t)); unassigned.remove(t); } for (int i = 0; i < m_numCluster; i++) { int r2 = avgSize > permInstsIdx.size() ? permInstsIdx.size() : avgSize; for (int j = 0; j < r2; j++) { T.Pt_x[permInstsIdx.get(j)] = i; } for (int j = 0; j < r2; j++) { permInstsIdx.remove(0); } } // initialize the prior prob of each cluster, and the probability // for each attribute within the cluster for (int i = 0; i < m_numCluster; i++) { ArrayList<Integer> indices = T.find(i); for (int j = 0; j < indices.size(); j++) { T.Pt[i] += input.Px[indices.get(j)]; } double[][] mArray = input.Pyx.getArray(); for (int j = 0; j < m_numAttributes; j++) { double sum = 0.0; for (int k = 0; k < indices.size(); k++) { sum += mArray[j][indices.get(k)]; } sum /= T.Pt[i]; T.Py_t.set(j, i, sum); } } if (m_verbose) { System.out.println("Initializing..."); } return T; }