use of structures._thetaStar in project IR_Base by Linda-sunshine.
the class CLRWithDP method calculateClusterProbPerUser.
// After we finish estimating the clusters, we calculate the probability of each user belongs to each cluster.
protected void calculateClusterProbPerUser() {
double prob;
_DPAdaptStruct user;
double[] probs = new double[m_kBar];
_thetaStar oldTheta;
for (int i = 0; i < m_userList.size(); i++) {
user = (_DPAdaptStruct) m_userList.get(i);
if (user.getTestSize() == 0)
continue;
oldTheta = user.getThetaStar();
for (int k = 0; k < m_kBar; k++) {
user.setThetaStar(m_thetaStars[k]);
// this proportion includes the user's current cluster assignment
prob = calcLogLikelihood(user) + Math.log(m_thetaStars[k].getMemSize());
// this will be in real space!
probs[k] = Math.exp(prob);
}
Utils.L1Normalization(probs);
user.setClusterPosterior(probs);
// restore the cluster assignment during EM iterations
user.setThetaStar(oldTheta);
}
}
use of structures._thetaStar in project IR_Base by Linda-sunshine.
the class CLRWithDP method predict.
public int predict(_AdaptStruct user, _thetaStar theta) {
double[] As;
double sum;
int m, n, predL = 0, count = 0;
for (_Review r : user.getReviews()) {
if (r.getType() == rType.TEST) {
As = theta.getModel();
// Bias term: w_s0*a0+b0.
sum = As[0] * MTCLinAdaptWithDP.m_supWeights[0] + As[m_dim];
for (_SparseFeature fv : r.getSparse()) {
n = fv.getIndex() + 1;
m = m_featureGroupMap[n];
sum += (As[m] * MTCLinAdaptWithDP.m_supWeights[n] + As[m_dim + m]) * fv.getValue();
}
if (sum > 0.5)
predL = 1;
if (predL == r.getYLabel())
count++;
}
}
return count;
}
use of structures._thetaStar in project IR_Base by Linda-sunshine.
the class CLRWithDP method printTopWords.
void printTopWords(_thetaStar cluster) {
MyPriorityQueue<_RankItem> wordRanker = new MyPriorityQueue<_RankItem>(10);
double[] phi = cluster.getModel();
// we will skip the bias term!
System.out.format("Cluster %d (%d)\n[positive]: ", cluster.getIndex(), cluster.getMemSize());
for (int i = 1; i < phi.length; i++) // top positive words with expected polarity
wordRanker.add(new _RankItem(i, phi[i]));
for (_RankItem it : wordRanker) System.out.format("%s:%.3f\t", m_features[it.m_index], phi[it.m_index]);
System.out.format("\n[negative]: ");
wordRanker.clear();
for (int i = 1; i < phi.length; i++) // top negative words
wordRanker.add(new _RankItem(i, -phi[i]));
for (_RankItem it : wordRanker) System.out.format("%s:%.3f\t", m_features[it.m_index], phi[it.m_index]);
}
use of structures._thetaStar in project IR_Base by Linda-sunshine.
the class CLRWithDP method calculate_E_step.
// The main MCMC algorithm, assign each user to clusters.
protected void calculate_E_step() {
_thetaStar curThetaStar;
_DPAdaptStruct user;
for (int i = 0; i < m_userList.size(); i++) {
user = (_DPAdaptStruct) m_userList.get(i);
if (user.getAdaptationSize() == 0)
continue;
curThetaStar = user.getThetaStar();
curThetaStar.updateMemCount(-1);
if (curThetaStar.getMemSize() == 0) {
// No data associated with the cluster.
// move it back to \theta*
swapTheta(m_kBar - 1, findThetaStar(curThetaStar));
m_kBar--;
}
sampleOneInstance(user);
}
}
use of structures._thetaStar in project IR_Base by Linda-sunshine.
the class MTCLinAdaptWithDP method writeNorms.
public void writeNorms(String supFile, String clusterFile, int k) {
ArrayList<_thetaStar> thetas = new ArrayList<_thetaStar>();
for (int i = 0; i < m_kBar; i++) thetas.add(m_thetaStars[i]);
Collections.sort(thetas);
PrintWriter writer;
double[] Ac;
int size = m_supModels.size();
ArrayList<double[]> Acs;
double[] wc = new double[m_featureSize + 1];
try {
for (int i = 0; i < k; i++) {
writer = new PrintWriter(new File(clusterFile + "_" + i));
if (thetas.get(i).getAllModels().size() != size)
continue;
Acs = thetas.get(i).getAllModels();
for (int j = 0; j < Acs.size(); j++) {
Ac = Acs.get(j);
wc = mapCluster(Ac, j);
for (double w : wc) writer.write(w + ",");
writer.write("\n");
}
writer.close();
}
writer = new PrintWriter(new File(supFile));
for (double[] ws : m_supModels) {
for (double w : ws) writer.write(w + ",");
writer.write("\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Aggregations