List of usage examples for weka.core Instances instance
publicInstance instance(int index)
From source file:milk.classifiers.MIWrapper.java
License:Open Source License
public Instances transform(Exemplars train) throws Exception { Instances data = new Instances(m_Attributes);// Data to learn a model data.deleteAttributeAt(m_IdIndex);// ID attribute useless Instances dataset = new Instances(data, 0); double sumNi = 0, // Total number of instances N = train.numExemplars(); // Number of exemplars for (int i = 0; i < N; i++) sumNi += train.exemplar(i).getInstances().numInstances(); // Initialize weights for (int i = 0; i < N; i++) { Exemplar exi = train.exemplar(i); // m_Prior[(int)exi.classValue()]++; Instances insts = exi.getInstances(); double ni = (double) insts.numInstances(); for (int j = 0; j < ni; j++) { Instance ins = new Instance(insts.instance(j));// Copy ins.deleteAttributeAt(m_IdIndex); ins.setDataset(dataset);// w w w . j ava 2s. c o m ins.setWeight(sumNi / (N * ni)); //ins.setWeight(1); data.add(ins); } } return data; }
From source file:milk.classifiers.MIWrapper.java
License:Open Source License
/** * Computes the distribution for a given exemplar *//from w ww .j a v a 2 s.co m * @param exmp the exemplar for which distribution is computed * @return the distribution * @exception Exception if the distribution can't be computed successfully */ public double[] distributionForExemplar(Exemplar exmp) throws Exception { // Extract the data Instances insts = new Instances(exmp.getInstances()); double nI = (double) insts.numInstances(); insts.deleteAttributeAt(m_IdIndex);// ID attribute useless // Compute the log-probability of the bag double[] distribution = new double[m_NumClasses]; for (int i = 0; i < nI; i++) { double[] dist = m_Classifier.distributionForInstance(insts.instance(i)); for (int j = 0; j < m_NumClasses; j++) { switch (m_Method) { case 1: distribution[j] += dist[j] / nI; break; case 2: // Avoid 0/1 probability if (dist[j] < 0.001) dist[j] = 0.001; else if (dist[j] > 0.999) dist[j] = 0.999; distribution[j] += Math.log(dist[j]) / nI; break; /*case 3: distribution[j] += Math.exp(Math.log(dist[j])/nI + Math.log(m_Prior[j])*(nI-1.0)/nI); */ } } } if (m_Method == 2) for (int j = 0; j < m_NumClasses; j++) distribution[j] = Math.exp(distribution[j]); Utils.normalize(distribution); return distribution; }
From source file:milk.classifiers.SimpleMI.java
License:Open Source License
/** * Get the minimal and maximal value of a certain attribute in a certain data *//from ww w . ja va 2s.com * @param data the data * @param attIndex the index of the attribute * @return the double array containing in entry 0 for min and 1 for max. */ public static double[] minimax(Instances data, int attIndex) { double[] rt = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }; for (int i = 0; i < data.numInstances(); i++) { double val = data.instance(i).value(attIndex); if (val > rt[1]) rt[1] = val; if (val < rt[0]) rt[0] = val; } for (int j = 0; j < 2; j++) if (Double.isInfinite(rt[j])) rt[j] = Double.NaN; return rt; }
From source file:milk.classifiers.TLD.java
License:Open Source License
/** * * @param exs the training exemplars/* w w w.j a v a 2 s. c o m*/ * @exception if the model cannot be built properly */ public void buildClassifier(Exemplars exs) throws Exception { m_ClassIndex = exs.classIndex(); m_IdIndex = exs.idIndex(); int numegs = exs.numExemplars(); m_Dimension = exs.numAttributes() - 2; Exemplars pos = new Exemplars(exs, 0), neg = new Exemplars(exs, 0); for (int u = 0; u < numegs; u++) { Exemplar example = exs.exemplar(u); if (example.classValue() == 0) pos.add(example); else neg.add(example); } int pnum = pos.numExemplars(), nnum = neg.numExemplars(); m_MeanP = new double[pnum][m_Dimension]; m_VarianceP = new double[pnum][m_Dimension]; m_SumP = new double[pnum][m_Dimension]; m_MeanN = new double[nnum][m_Dimension]; m_VarianceN = new double[nnum][m_Dimension]; m_SumN = new double[nnum][m_Dimension]; m_ParamsP = new double[4 * m_Dimension]; m_ParamsN = new double[4 * m_Dimension]; // Estimation of the parameters: as the start value for search double[] pSumVal = new double[m_Dimension], // for m nSumVal = new double[m_Dimension]; double[] maxVarsP = new double[m_Dimension], // for a maxVarsN = new double[m_Dimension]; // Mean of sample variances: for b, b=a/E(\sigma^2)+2 double[] varMeanP = new double[m_Dimension], varMeanN = new double[m_Dimension]; // Variances of sample means: for w, w=E[var(\mu)]/E[\sigma^2] double[] meanVarP = new double[m_Dimension], meanVarN = new double[m_Dimension]; // number of exemplars without all values missing double[] numExsP = new double[m_Dimension], numExsN = new double[m_Dimension]; // Extract metadata fro both positive and negative bags for (int v = 0; v < pnum; v++) { Exemplar px = pos.exemplar(v); m_MeanP[v] = px.meanOrMode(); m_VarianceP[v] = px.variance(); Instances pxi = px.getInstances(); for (int w = 0, t = 0; w < m_Dimension; w++, t++) { if ((t == m_ClassIndex) || (t == m_IdIndex)) t++; if (!Double.isNaN(m_MeanP[v][w])) { for (int u = 0; u < pxi.numInstances(); u++) { Instance ins = pxi.instance(u); if (!ins.isMissing(t)) m_SumP[v][w] += ins.weight(); } numExsP[w]++; pSumVal[w] += m_MeanP[v][w]; meanVarP[w] += m_MeanP[v][w] * m_MeanP[v][w]; if (maxVarsP[w] < m_VarianceP[v][w]) maxVarsP[w] = m_VarianceP[v][w]; varMeanP[w] += m_VarianceP[v][w]; m_VarianceP[v][w] *= (m_SumP[v][w] - 1.0); if (m_VarianceP[v][w] < 0.0) m_VarianceP[v][w] = 0.0; } } } for (int v = 0; v < nnum; v++) { Exemplar nx = neg.exemplar(v); m_MeanN[v] = nx.meanOrMode(); m_VarianceN[v] = nx.variance(); Instances nxi = nx.getInstances(); for (int w = 0, t = 0; w < m_Dimension; w++, t++) { if ((t == m_ClassIndex) || (t == m_IdIndex)) t++; if (!Double.isNaN(m_MeanN[v][w])) { for (int u = 0; u < nxi.numInstances(); u++) if (!nxi.instance(u).isMissing(t)) m_SumN[v][w] += nxi.instance(u).weight(); numExsN[w]++; nSumVal[w] += m_MeanN[v][w]; meanVarN[w] += m_MeanN[v][w] * m_MeanN[v][w]; if (maxVarsN[w] < m_VarianceN[v][w]) maxVarsN[w] = m_VarianceN[v][w]; varMeanN[w] += m_VarianceN[v][w]; m_VarianceN[v][w] *= (m_SumN[v][w] - 1.0); if (m_VarianceN[v][w] < 0.0) m_VarianceN[v][w] = 0.0; } } } for (int w = 0; w < m_Dimension; w++) { pSumVal[w] /= numExsP[w]; nSumVal[w] /= numExsN[w]; if (numExsP[w] > 1) meanVarP[w] = meanVarP[w] / (numExsP[w] - 1.0) - pSumVal[w] * numExsP[w] / (numExsP[w] - 1.0); if (numExsN[w] > 1) meanVarN[w] = meanVarN[w] / (numExsN[w] - 1.0) - nSumVal[w] * numExsN[w] / (numExsN[w] - 1.0); varMeanP[w] /= numExsP[w]; varMeanN[w] /= numExsN[w]; } //Bounds and parameter values for each run double[][] bounds = new double[2][4]; double[] pThisParam = new double[4], nThisParam = new double[4]; // Initial values for parameters double a, b, w, m; // Optimize for one dimension for (int x = 0; x < m_Dimension; x++) { System.err.println("\n\n!!!!!!!!!!!!!!!!!!!!!!???Dimension #" + x); // Positive examplars: first run a = (maxVarsP[x] > ZERO) ? maxVarsP[x] : 1.0; b = a / varMeanP[x] + 2.0; // a/(b-2) = E(\sigma^2) w = meanVarP[x] / varMeanP[x]; // E[var(\mu)] = w*E[\sigma^2] if (w <= ZERO) w = 1.0; m = pSumVal[x]; pThisParam[0] = a; // a pThisParam[1] = b; // b pThisParam[2] = w; // w pThisParam[3] = m; // m // Negative examplars: first run a = (maxVarsN[x] > ZERO) ? maxVarsN[x] : 1.0; b = a / varMeanN[x] + 2.0; // a/(b-2) = E(\sigma^2) w = meanVarN[x] / varMeanN[x]; // E[var(\mu)] = w*E[\sigma^2] if (w <= ZERO) w = 1.0; m = nSumVal[x]; nThisParam[0] = a; // a nThisParam[1] = b; // b nThisParam[2] = w; // w nThisParam[3] = m; // m // Bound constraints bounds[0][0] = ZERO; // a > 0 bounds[0][1] = 2.0 + ZERO; // b > 2 bounds[0][2] = ZERO; // w > 0 bounds[0][3] = Double.NaN; for (int t = 0; t < 4; t++) { bounds[1][t] = Double.NaN; m_ParamsP[4 * x + t] = pThisParam[t]; m_ParamsN[4 * x + t] = nThisParam[t]; } double pminVal = Double.MAX_VALUE, nminVal = Double.MAX_VALUE; Random whichEx = new Random(m_Seed); TLD_Optm pOp = null, nOp = null; boolean isRunValid = true; double[] sumP = new double[pnum], meanP = new double[pnum], varP = new double[pnum]; double[] sumN = new double[nnum], meanN = new double[nnum], varN = new double[nnum]; // One dimension for (int p = 0; p < pnum; p++) { sumP[p] = m_SumP[p][x]; meanP[p] = m_MeanP[p][x]; varP[p] = m_VarianceP[p][x]; } for (int q = 0; q < nnum; q++) { sumN[q] = m_SumN[q][x]; meanN[q] = m_MeanN[q][x]; varN[q] = m_VarianceN[q][x]; } for (int y = 0; y < m_Run;) { System.err.println("\n\n!!!!!!!!!!!!!!!!!!!!!!???Run #" + y); double thisMin; System.err.println("\nPositive exemplars"); pOp = new TLD_Optm(); pOp.setNum(sumP); pOp.setSSquare(varP); pOp.setXBar(meanP); pThisParam = pOp.findArgmin(pThisParam, bounds); while (pThisParam == null) { pThisParam = pOp.getVarbValues(); System.err.println("!!! 200 iterations finished, not enough!"); pThisParam = pOp.findArgmin(pThisParam, bounds); } thisMin = pOp.getMinFunction(); if (!Double.isNaN(thisMin) && (thisMin < pminVal)) { pminVal = thisMin; for (int z = 0; z < 4; z++) m_ParamsP[4 * x + z] = pThisParam[z]; } if (Double.isNaN(thisMin)) { pThisParam = new double[4]; isRunValid = false; } System.err.println("\nNegative exemplars"); nOp = new TLD_Optm(); nOp.setNum(sumN); nOp.setSSquare(varN); nOp.setXBar(meanN); nThisParam = nOp.findArgmin(nThisParam, bounds); while (nThisParam == null) { nThisParam = nOp.getVarbValues(); System.err.println("!!! 200 iterations finished, not enough!"); nThisParam = nOp.findArgmin(nThisParam, bounds); } thisMin = nOp.getMinFunction(); if (!Double.isNaN(thisMin) && (thisMin < nminVal)) { nminVal = thisMin; for (int z = 0; z < 4; z++) m_ParamsN[4 * x + z] = nThisParam[z]; } if (Double.isNaN(thisMin)) { nThisParam = new double[4]; isRunValid = false; } if (!isRunValid) { y--; isRunValid = true; } if (++y < m_Run) { // Change the initial parameters and restart int pone = whichEx.nextInt(pnum), // Randomly pick one pos. exmpl. none = whichEx.nextInt(nnum); // Positive exemplars: next run while ((m_SumP[pone][x] <= 1.0) || Double.isNaN(m_MeanP[pone][x])) pone = whichEx.nextInt(pnum); a = m_VarianceP[pone][x] / (m_SumP[pone][x] - 1.0); if (a <= ZERO) a = m_ParamsN[4 * x]; // Change to negative params m = m_MeanP[pone][x]; double sq = (m - m_ParamsP[4 * x + 3]) * (m - m_ParamsP[4 * x + 3]); b = a * m_ParamsP[4 * x + 2] / sq + 2.0; // b=a/Var+2, assuming Var=Sq/w' if ((b <= ZERO) || Double.isNaN(b) || Double.isInfinite(b)) b = m_ParamsN[4 * x + 1]; w = sq * (m_ParamsP[4 * x + 1] - 2.0) / m_ParamsP[4 * x];//w=Sq/Var, assuming Var=a'/(b'-2) if ((w <= ZERO) || Double.isNaN(w) || Double.isInfinite(w)) w = m_ParamsN[4 * x + 2]; pThisParam[0] = a; // a pThisParam[1] = b; // b pThisParam[2] = w; // w pThisParam[3] = m; // m // Negative exemplars: next run while ((m_SumN[none][x] <= 1.0) || Double.isNaN(m_MeanN[none][x])) none = whichEx.nextInt(nnum); a = m_VarianceN[none][x] / (m_SumN[none][x] - 1.0); if (a <= ZERO) a = m_ParamsP[4 * x]; m = m_MeanN[none][x]; sq = (m - m_ParamsN[4 * x + 3]) * (m - m_ParamsN[4 * x + 3]); b = a * m_ParamsN[4 * x + 2] / sq + 2.0; // b=a/Var+2, assuming Var=Sq/w' if ((b <= ZERO) || Double.isNaN(b) || Double.isInfinite(b)) b = m_ParamsP[4 * x + 1]; w = sq * (m_ParamsN[4 * x + 1] - 2.0) / m_ParamsN[4 * x];//w=Sq/Var, assuming Var=a'/(b'-2) if ((w <= ZERO) || Double.isNaN(w) || Double.isInfinite(w)) w = m_ParamsP[4 * x + 2]; nThisParam[0] = a; // a nThisParam[1] = b; // b nThisParam[2] = w; // w nThisParam[3] = m; // m } } } for (int x = 0, y = 0; x < m_Dimension; x++, y++) { if ((x == exs.classIndex()) || (x == exs.idIndex())) y++; a = m_ParamsP[4 * x]; b = m_ParamsP[4 * x + 1]; w = m_ParamsP[4 * x + 2]; m = m_ParamsP[4 * x + 3]; System.err.println( "\n\n???Positive: ( " + exs.attribute(y) + "): a=" + a + ", b=" + b + ", w=" + w + ", m=" + m); a = m_ParamsN[4 * x]; b = m_ParamsN[4 * x + 1]; w = m_ParamsN[4 * x + 2]; m = m_ParamsN[4 * x + 3]; System.err.println( "???Negative: (" + exs.attribute(y) + "): a=" + a + ", b=" + b + ", w=" + w + ", m=" + m); } if (m_UseEmpiricalCutOff) { // Find the empirical cut-off double[] pLogOdds = new double[pnum], nLogOdds = new double[nnum]; for (int p = 0; p < pnum; p++) pLogOdds[p] = likelihoodRatio(m_SumP[p], m_MeanP[p], m_VarianceP[p]); for (int q = 0; q < nnum; q++) nLogOdds[q] = likelihoodRatio(m_SumN[q], m_MeanN[q], m_VarianceN[q]); // Update m_Cutoff findCutOff(pLogOdds, nLogOdds); } else m_Cutoff = -Math.log((double) pnum / (double) nnum); System.err.println("???Cut-off=" + m_Cutoff); }
From source file:milk.classifiers.TLD.java
License:Open Source License
/** * * @param ex the given test exemplar/*from w ww. java2 s .c o m*/ * @return the classification * @exception Exception if the exemplar could not be classified * successfully */ public double classifyExemplar(Exemplar e) throws Exception { Exemplar ex = new Exemplar(e); Instances exi = ex.getInstances(); double[] n = new double[m_Dimension], xBar = ex.meanOrMode(), sSq = ex.variance(); for (int w = 0, t = 0; w < m_Dimension; w++, t++) { if ((t == m_ClassIndex) || (t == m_IdIndex)) t++; for (int u = 0; u < exi.numInstances(); u++) if (!exi.instance(u).isMissing(t)) n[w] += exi.instance(u).weight(); sSq[w] = sSq[w] * (n[w] - 1.0); if (sSq[w] <= 0.0) sSq[w] = 0.0; } double logOdds = likelihoodRatio(n, xBar, sSq); return (logOdds > m_Cutoff) ? 0 : 1; }
From source file:milk.classifiers.TLDSimple.java
License:Open Source License
/** * * @param exs the training exemplars/*from w w w. j a v a 2 s .c o m*/ * @exception if the model cannot be built properly */ public void buildClassifier(Exemplars exs) throws Exception { m_ClassIndex = exs.classIndex(); m_IdIndex = exs.idIndex(); int numegs = exs.numExemplars(); m_Dimension = exs.numAttributes() - 2; m_Attribute = new Instances(exs.exemplar(0).getInstances(), 0); Exemplars pos = new Exemplars(exs, 0), neg = new Exemplars(exs, 0); // Divide into two groups for (int u = 0; u < numegs; u++) { Exemplar example = exs.exemplar(u); if (example.classValue() == 0) pos.add(example); else neg.add(example); } int pnum = pos.numExemplars(), nnum = neg.numExemplars(); // xBar, n m_MeanP = new double[pnum][m_Dimension]; m_SumP = new double[pnum][m_Dimension]; m_MeanN = new double[nnum][m_Dimension]; m_SumN = new double[nnum][m_Dimension]; // w, m m_ParamsP = new double[2 * m_Dimension]; m_ParamsN = new double[2 * m_Dimension]; // \sigma^2 m_SgmSqP = new double[m_Dimension]; m_SgmSqN = new double[m_Dimension]; // S^2 double[][] varP = new double[pnum][m_Dimension], varN = new double[nnum][m_Dimension]; // numOfEx 'e' without all missing double[] effNumExP = new double[m_Dimension], effNumExN = new double[m_Dimension]; // For the starting values double[] pMM = new double[m_Dimension], nMM = new double[m_Dimension], pVM = new double[m_Dimension], nVM = new double[m_Dimension]; // # of exemplars with only one instance double[] numOneInsExsP = new double[m_Dimension], numOneInsExsN = new double[m_Dimension]; // sum_i(1/n_i) double[] pInvN = new double[m_Dimension], nInvN = new double[m_Dimension]; // Extract metadata from both positive and negative bags for (int v = 0; v < pnum; v++) { Exemplar px = pos.exemplar(v); m_MeanP[v] = px.meanOrMode(); varP[v] = px.variance(); Instances pxi = px.getInstances(); for (int w = 0, t = 0; w < m_Dimension; w++, t++) { if ((t == m_ClassIndex) || (t == m_IdIndex)) t++; if (varP[v][w] <= 0.0) varP[v][w] = 0.0; if (!Double.isNaN(m_MeanP[v][w])) { for (int u = 0; u < pxi.numInstances(); u++) if (!pxi.instance(u).isMissing(t)) m_SumP[v][w] += pxi.instance(u).weight(); pMM[w] += m_MeanP[v][w]; pVM[w] += m_MeanP[v][w] * m_MeanP[v][w]; if ((m_SumP[v][w] > 1) && (varP[v][w] > ZERO)) { m_SgmSqP[w] += varP[v][w] * (m_SumP[v][w] - 1.0) / m_SumP[v][w]; //m_SgmSqP[w] += varP[v][w]*(m_SumP[v][w]-1.0); effNumExP[w]++; // Not count exemplars with 1 instance pInvN[w] += 1.0 / m_SumP[v][w]; //pInvN[w] += m_SumP[v][w]; } else numOneInsExsP[w]++; } } } for (int v = 0; v < nnum; v++) { Exemplar nx = neg.exemplar(v); m_MeanN[v] = nx.meanOrMode(); varN[v] = nx.variance(); Instances nxi = nx.getInstances(); for (int w = 0, t = 0; w < m_Dimension; w++, t++) { if ((t == m_ClassIndex) || (t == m_IdIndex)) t++; if (varN[v][w] <= 0.0) varN[v][w] = 0.0; if (!Double.isNaN(m_MeanN[v][w])) { for (int u = 0; u < nxi.numInstances(); u++) if (!nxi.instance(u).isMissing(t)) m_SumN[v][w] += nxi.instance(u).weight(); nMM[w] += m_MeanN[v][w]; nVM[w] += m_MeanN[v][w] * m_MeanN[v][w]; if ((m_SumN[v][w] > 1) && (varN[v][w] > ZERO)) { m_SgmSqN[w] += varN[v][w] * (m_SumN[v][w] - 1.0) / m_SumN[v][w]; //m_SgmSqN[w] += varN[v][w]*(m_SumN[v][w]-1.0); effNumExN[w]++; // Not count exemplars with 1 instance nInvN[w] += 1.0 / m_SumN[v][w]; //nInvN[w] += m_SumN[v][w]; } else numOneInsExsN[w]++; } } } // Expected \sigma^2 for (int u = 0; u < m_Dimension; u++) { // For exemplars with only one instance, use avg(\sigma^2) of other exemplars m_SgmSqP[u] /= (effNumExP[u] - pInvN[u]); m_SgmSqN[u] /= (effNumExN[u] - nInvN[u]); //m_SgmSqP[u] /= (pInvN[u]-effNumExP[u]); //m_SgmSqN[u] /= (nInvN[u]-effNumExN[u]); effNumExP[u] += numOneInsExsP[u]; effNumExN[u] += numOneInsExsN[u]; pMM[u] /= effNumExP[u]; nMM[u] /= effNumExN[u]; pVM[u] = pVM[u] / (effNumExP[u] - 1.0) - pMM[u] * pMM[u] * effNumExP[u] / (effNumExP[u] - 1.0); nVM[u] = nVM[u] / (effNumExN[u] - 1.0) - nMM[u] * nMM[u] * effNumExN[u] / (effNumExN[u] - 1.0); } //Bounds and parameter values for each run double[][] bounds = new double[2][2]; double[] pThisParam = new double[2], nThisParam = new double[2]; // Initial values for parameters double w, m; Random whichEx = new Random(m_Seed); // Optimize for one dimension for (int x = 0; x < m_Dimension; x++) { // System.out.println("\n\n!!!!!!!!!!!!!!!!!!!!!!???Dimension #"+x); // Positive examplars: first run pThisParam[0] = pVM[x]; // w if (pThisParam[0] <= ZERO) pThisParam[0] = 1.0; pThisParam[1] = pMM[x]; // m // Negative examplars: first run nThisParam[0] = nVM[x]; // w if (nThisParam[0] <= ZERO) nThisParam[0] = 1.0; nThisParam[1] = nMM[x]; // m // Bound constraints bounds[0][0] = ZERO; // w > 0 bounds[0][1] = Double.NaN; bounds[1][0] = Double.NaN; bounds[1][1] = Double.NaN; double pminVal = Double.MAX_VALUE, nminVal = Double.MAX_VALUE; TLDSimple_Optm pOp = null, nOp = null; boolean isRunValid = true; double[] sumP = new double[pnum], meanP = new double[pnum]; double[] sumN = new double[nnum], meanN = new double[nnum]; // One dimension for (int p = 0; p < pnum; p++) { sumP[p] = m_SumP[p][x]; meanP[p] = m_MeanP[p][x]; } for (int q = 0; q < nnum; q++) { sumN[q] = m_SumN[q][x]; meanN[q] = m_MeanN[q][x]; } for (int y = 0; y < m_Run; y++) { //System.out.println("\n\n!!!!!!!!!Positive exemplars: Run #"+y); double thisMin; pOp = new TLDSimple_Optm(); pOp.setNum(sumP); pOp.setSgmSq(m_SgmSqP[x]); pOp.setXBar(meanP); //pOp.setDebug(true); pThisParam = pOp.findArgmin(pThisParam, bounds); while (pThisParam == null) { pThisParam = pOp.getVarbValues(); System.out.println("!!! 200 iterations finished, not enough!"); pThisParam = pOp.findArgmin(pThisParam, bounds); } thisMin = pOp.getMinFunction(); if (!Double.isNaN(thisMin) && (thisMin < pminVal)) { pminVal = thisMin; for (int z = 0; z < 2; z++) m_ParamsP[2 * x + z] = pThisParam[z]; } if (Double.isNaN(thisMin)) { pThisParam = new double[2]; isRunValid = false; } if (!isRunValid) { y--; isRunValid = true; } // Change the initial parameters and restart int pone = whichEx.nextInt(pnum); // Positive exemplars: next run while (Double.isNaN(m_MeanP[pone][x])) pone = whichEx.nextInt(pnum); m = m_MeanP[pone][x]; w = (m - pThisParam[1]) * (m - pThisParam[1]); pThisParam[0] = w; // w pThisParam[1] = m; // m } for (int y = 0; y < m_Run; y++) { //System.out.println("\n\n!!!!!!!!!Negative exemplars: Run #"+y); double thisMin; nOp = new TLDSimple_Optm(); nOp.setNum(sumN); nOp.setSgmSq(m_SgmSqN[x]); nOp.setXBar(meanN); //nOp.setDebug(true); nThisParam = nOp.findArgmin(nThisParam, bounds); while (nThisParam == null) { nThisParam = nOp.getVarbValues(); System.out.println("!!! 200 iterations finished, not enough!"); nThisParam = nOp.findArgmin(nThisParam, bounds); } thisMin = nOp.getMinFunction(); if (!Double.isNaN(thisMin) && (thisMin < nminVal)) { nminVal = thisMin; for (int z = 0; z < 2; z++) m_ParamsN[2 * x + z] = nThisParam[z]; } if (Double.isNaN(thisMin)) { nThisParam = new double[2]; isRunValid = false; } if (!isRunValid) { y--; isRunValid = true; } // Change the initial parameters and restart int none = whichEx.nextInt(nnum);// Randomly pick one pos. exmpl. // Negative exemplars: next run while (Double.isNaN(m_MeanN[none][x])) none = whichEx.nextInt(nnum); m = m_MeanN[none][x]; w = (m - nThisParam[1]) * (m - nThisParam[1]); nThisParam[0] = w; // w nThisParam[1] = m; // m } } m_LkRatio = new double[m_Dimension]; if (m_UseEmpiricalCutOff) { // Find the empirical cut-off double[] pLogOdds = new double[pnum], nLogOdds = new double[nnum]; for (int p = 0; p < pnum; p++) pLogOdds[p] = likelihoodRatio(m_SumP[p], m_MeanP[p]); for (int q = 0; q < nnum; q++) nLogOdds[q] = likelihoodRatio(m_SumN[q], m_MeanN[q]); // Update m_Cutoff findCutOff(pLogOdds, nLogOdds); } else m_Cutoff = -Math.log((double) pnum / (double) nnum); /* for(int x=0, y=0; x<m_Dimension; x++, y++){ if((x==exs.classIndex()) || (x==exs.idIndex())) y++; w=m_ParamsP[2*x]; m=m_ParamsP[2*x+1]; System.err.println("\n\n???Positive: ( "+exs.attribute(y)+ "): w="+w+", m="+m+", sgmSq="+m_SgmSqP[x]); w=m_ParamsN[2*x]; m=m_ParamsN[2*x+1]; System.err.println("???Negative: ("+exs.attribute(y)+ "): w="+w+", m="+m+", sgmSq="+m_SgmSqN[x]+ "\nAvg. log-likelihood ratio in training data=" +(m_LkRatio[x]/(pnum+nnum))); } */ System.err.println("\n\n???Cut-off=" + m_Cutoff); }
From source file:milk.classifiers.TLDSimple.java
License:Open Source License
/** * * @param ex the given test exemplar//from w w w . jav a2s.c o m * @return the classification * @exception Exception if the exemplar could not be classified * successfully */ public double classifyExemplar(Exemplar e) throws Exception { Exemplar ex = new Exemplar(e); Instances exi = ex.getInstances(); double[] n = new double[m_Dimension], xBar = ex.meanOrMode(); for (int w = 0, t = 0; w < m_Dimension; w++, t++) { if ((t == m_ClassIndex) || (t == m_IdIndex)) t++; for (int u = 0; u < exi.numInstances(); u++) if (!exi.instance(u).isMissing(t)) n[w] += exi.instance(u).weight(); } double logOdds = likelihoodRatio(n, xBar); return (logOdds > m_Cutoff) ? 0 : 1; }
From source file:milk.core.Exemplar.java
License:Open Source License
/** * Main method for testing this class -- just prints out a set * of Exemplars. Assume the ID index is 0. * * @param argv should contain one element: the name of an ARFF file *///www. jav a 2 s . c o m public static void main(String[] args) { try { Reader r = null; if (args.length > 1) { throw (new Exception("Usage: Instances <filename>")); } else if (args.length == 0) { r = new BufferedReader(new InputStreamReader(System.in)); } else { r = new BufferedReader(new FileReader(args[0])); } Instances i = new Instances(r); i.setClassIndex(i.numAttributes() - 1); Attribute id = i.attribute(0); if (!id.isNominal()) throw new Exception("The first attribute is not nominal"); Exemplar[] egs = new Exemplar[id.numValues()]; for (int j = 0; j < egs.length; j++) egs[j] = null; for (int j = 0; j < i.numInstances(); j++) { Instance ins = i.instance(j); int idv = (int) ins.value(0); if (egs[idv] == null) egs[idv] = new Exemplar(ins, 0); else egs[idv].add(ins); } for (int j = 0; j < egs.length; j++) System.out.println(egs[j].toString()); } catch (Exception ex) { System.err.println(ex.getMessage()); } }
From source file:milk.core.Exemplars.java
License:Open Source License
/** * Constructor using the given dataset and set ID index to * the given ID index. Any instances with class value or ID * value missing will be dropped.//from w w w .j av a 2 s.c om * * @param dataset the instances from which the header * information is to be taken * @param idIndex the ID attribute's index * @exception Exception if the class index of the dataset * is not set(i.e. -1) or the data is not a multi-instance data */ public Exemplars(Instances dataset, int idIndex) throws Exception { if (dataset.classIndex() == -1) throw new Exception(" Class Index negative (class not set yet)!"); m_ClassIndex = dataset.classIndex(); m_RelationName = dataset.relationName(); int numAttr = dataset.numAttributes(); m_Attributes = new Attribute[numAttr]; for (int i = 0; i < numAttr; i++) m_Attributes[i] = dataset.attribute(i); m_IdIndex = idIndex; Attribute id = m_Attributes[m_IdIndex]; if ((m_IdIndex > numAttr) || (m_IdIndex < 0) || (!id.isNominal())) throw new Exception("ID index is wrong!"); m_Exemplars = new Vector(id.numValues()); for (int j = 0; j < dataset.numInstances(); j++) { Instance ins = dataset.instance(j); add(ins); } }
From source file:milk.experiment.MIInstanceQuery.java
License:Open Source License
/** * Test the class from the command line. The instance * query should be specified with -Q sql_query * * @param args contains options for the instance query *//* w w w.j a v a2 s .c o m*/ public static void main(String args[]) { try { MIInstanceQuery iq = new MIInstanceQuery(); String query = Utils.getOption('Q', args); if (query.length() == 0) { iq.setQuery("select * from Experiment_index"); } else { iq.setQuery(query); } iq.setOptions(args); try { Utils.checkForRemainingOptions(args); } catch (Exception e) { System.err.println("Options for weka.experiment.InstanceQuery:\n"); Enumeration en = iq.listOptions(); while (en.hasMoreElements()) { Option o = (Option) en.nextElement(); System.err.println(o.synopsis() + "\n" + o.description()); } System.exit(1); } Instances aha = iq.retrieveInstances(); iq.disconnectFromDatabase(); // The dataset may be large, so to make things easier we'll // output an instance at a time (rather than having to convert // the entire dataset to one large string) System.out.println(new Instances(aha, 0)); for (int i = 0; i < aha.numInstances(); i++) { System.out.println(aha.instance(i)); } } catch (Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } }