diff --git a/java/libsvm/svm.java b/java/libsvm/svm.java index 50dea8ef..cf187b97 100644 --- a/java/libsvm/svm.java +++ b/java/libsvm/svm.java @@ -6,6 +6,8 @@ package libsvm; import java.io.*; import java.util.*; +import static java.lang.Math.*; +import static libsvm.svm_parameter.*; // // Kernel Cache @@ -33,7 +35,7 @@ private final class head_t for(int i=0;i y[j].index) ++j; } - return Math.exp(-param.gamma*sum); + return exp(-param.gamma*sum); } - case svm_parameter.SIGMOID: - return Math.tanh(param.gamma*dot(x,y)+param.coef0); - case svm_parameter.PRECOMPUTED: + case SIGMOID: + return tanh(param.gamma*dot(x,y)+param.coef0); + case PRECOMPUTED: //x: test (validation), y: SV return x[(int)(y[0].value)].value; default: - return 0; // java + return 0; // Unreachable } } } @@ -335,7 +338,7 @@ else if(alpha[i] <= 0) } boolean is_upper_bound(int i) { return alpha_status[i] == UPPER_BOUND; } boolean is_lower_bound(int i) { return alpha_status[i] == LOWER_BOUND; } - boolean is_free(int i) { return alpha_status[i] == FREE; } + boolean is_free(int i) { return alpha_status[i] == FREE; } // java: information about solution except alpha, // because we cannot return multiple values otherwise... @@ -457,8 +460,8 @@ void Solve(int l, QMatrix Q, double[] p_, byte[] y_, // optimization step int iter = 0; - int max_iter = Math.max(10000000, l>Integer.MAX_VALUE/100 ? Integer.MAX_VALUE : 100*l); - int counter = Math.min(l,1000)+1; + int max_iter = max(10000000, l>Integer.MAX_VALUE/100 ? Integer.MAX_VALUE : 100*l); + int counter = min(l,1000)+1; int[] working_set = new int[2]; while(iter < max_iter) @@ -467,7 +470,7 @@ void Solve(int l, QMatrix Q, double[] p_, byte[] y_, if(--counter == 0) { - counter = Math.min(l,1000); + counter = min(l,1000); if(shrinking!=0) do_shrinking(); svm.info("."); } @@ -674,7 +677,7 @@ int select_working_set(int[] working_set) { // return i,j such that // i: maximizes -y_i * grad(f)_i, i in I_up(\alpha) - // j: mimimizes the decrease of obj value + // j: minimizes the decrease of obj value // (if quadratic coefficeint <= 0, replace it with tau) // -y_j*grad(f)_j < -y_i*grad(f)_i, j in I_low(\alpha) @@ -706,7 +709,7 @@ int select_working_set(int[] working_set) int i = Gmax_idx; float[] Q_i = null; - if(i != -1) // null Q_i not accessed: Gmax=-INF if i=-1 + if(i != -1) // NULL Q_i not accessed: Gmax=-INF if i=-1 Q_i = Q.get_Q(i,active_size); for(int j=0;j 0) - ub = Math.min(ub,yG); + if(y[i] < 0) + ub = min(ub,yG); else - lb = Math.max(lb,yG); + lb = max(lb,yG); } - else if(is_upper_bound(i)) + else if(is_lower_bound(i)) { - if(y[i] < 0) - ub = Math.min(ub,yG); + if(y[i] > 0) + ub = min(ub,yG); else - lb = Math.max(lb,yG); + lb = max(lb,yG); } - else + else { ++nr_free; sum_free += yG; @@ -950,7 +954,7 @@ int select_working_set(int[] working_set) int in = Gmaxn_idx; float[] Q_ip = null; float[] Q_in = null; - if(ip != -1) // null Q_ip not accessed: Gmaxp=-INF if ip=-1 + if(ip != -1) // NULL Q_ip not accessed: Gmaxp=-INF if ip=-1 Q_ip = Q.get_Q(ip,active_size); if(in != -1) Q_in = Q.get_Q(in,active_size); @@ -1007,7 +1011,7 @@ int select_working_set(int[] working_set) } } - if(Math.max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2) < eps || Gmin_idx == -1) + if(max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2) < eps || Gmin_idx == -1) return 1; if(y[Gmin_idx] == +1) @@ -1068,7 +1072,7 @@ void do_shrinking() } } - if(unshrink == false && Math.max(Gmax1+Gmax2,Gmax3+Gmax4) <= eps*10) + if(unshrink == false && max(Gmax1+Gmax2,Gmax3+Gmax4) <= eps*10) { unshrink = true; reconstruct_gradient(); @@ -1102,10 +1106,10 @@ void do_shrinking() { if(y[i]==+1) { - if(is_lower_bound(i)) - ub1 = Math.min(ub1,G[i]); - else if(is_upper_bound(i)) - lb1 = Math.max(lb1,G[i]); + if(is_upper_bound(i)) + lb1 = max(lb1,G[i]); + else if(is_lower_bound(i)) + ub1 = min(ub1,G[i]); else { ++nr_free1; @@ -1114,10 +1118,10 @@ else if(is_upper_bound(i)) } else { - if(is_lower_bound(i)) - ub2 = Math.min(ub2,G[i]); - else if(is_upper_bound(i)) - lb2 = Math.max(lb2,G[i]); + if(is_upper_bound(i)) + lb2 = max(lb2,G[i]); + else if(is_lower_bound(i)) + ub2 = min(ub2,G[i]); else { ++nr_free2; @@ -1365,12 +1369,12 @@ private static void solve_nu_svc(svm_problem prob, svm_parameter param, for(i=0;i 0) + if(abs(alpha[i]) > 0) { ++nSV; if(prob.y[i] > 0) { - if(Math.abs(alpha[i]) >= si.upper_bound_p) + if(abs(alpha[i]) >= si.upper_bound_p) ++nBSV; } else { - if(Math.abs(alpha[i]) >= si.upper_bound_n) + if(abs(alpha[i]) >= si.upper_bound_n) ++nBSV; } } @@ -1579,7 +1583,7 @@ private static void sigmoid_train(int l, double[] dec_values, double[] labels, int iter; // Initial Point and Initial Fun Value - A=0.0; B=Math.log((prior0+1.0)/(prior1+1.0)); + A=0.0; B=log((prior0+1.0)/(prior1+1.0)); double fval = 0.0; for (i=0;i=0) - fval += t[i]*fApB + Math.log(1+Math.exp(-fApB)); + fval += t[i]*fApB + log(1+exp(-fApB)); else - fval += (t[i] - 1)*fApB +Math.log(1+Math.exp(fApB)); + fval += (t[i] - 1)*fApB +log(1+exp(fApB)); } for (iter=0;iter= 0) { - p=Math.exp(-fApB)/(1.0+Math.exp(-fApB)); - q=1.0/(1.0+Math.exp(-fApB)); + p=exp(-fApB)/(1.0+exp(-fApB)); + q=1.0/(1.0+exp(-fApB)); } else { - p=1.0/(1.0+Math.exp(fApB)); - q=Math.exp(fApB)/(1.0+Math.exp(fApB)); + p=1.0/(1.0+exp(fApB)); + q=exp(fApB)/(1.0+exp(fApB)); } d2=p*q; h11+=dec_values[i]*dec_values[i]*d2; @@ -1621,7 +1625,7 @@ private static void sigmoid_train(int l, double[] dec_values, double[] labels, } // Stopping Criteria - if (Math.abs(g1)= 0) - newf += t[i]*fApB + Math.log(1+Math.exp(-fApB)); + newf += t[i]*fApB + log(1+exp(-fApB)); else - newf += (t[i] - 1)*fApB +Math.log(1+Math.exp(fApB)); + newf += (t[i] - 1)*fApB +log(1+exp(fApB)); } // Check sufficient decrease if (newf= 0) - return Math.exp(-fApB)/(1.0+Math.exp(-fApB)); + return exp(-fApB)/(1.0+exp(-fApB)); else - return 1.0/(1+Math.exp(fApB)) ; + return 1.0/(1+exp(fApB)) ; } // Method 2 from the multiclass_prob paper by Wu, Lin, and Weng private static void multiclass_probability(int k, double[][] r, double[] p) { int t,j; - int iter = 0, max_iter=Math.max(100,k); + int iter = 0, max_iter=max(100,k); double[][] Q=new double[k][k]; double[] Qp=new double[k]; double pQp, eps=0.005/k; @@ -1716,7 +1721,7 @@ private static void multiclass_probability(int k, double[][] r, double[] p) double max_error=0; for (t=0;tmax_error) max_error=error; } @@ -1833,17 +1838,17 @@ private static double svm_svr_probability(svm_problem prob, svm_parameter param) for(i=0;i 5*std) + if (abs(ymv[i]) > 5*std) count=count+1; else - mae+=Math.abs(ymv[i]); + mae+=abs(ymv[i]); mae /= (prob.l-count); svm.info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma="+mae+"\n"); return mae; @@ -1937,9 +1942,9 @@ public static svm_model svm_train(svm_problem prob, svm_parameter param) svm_model model = new svm_model(); model.param = param; - if(param.svm_type == svm_parameter.ONE_CLASS || - param.svm_type == svm_parameter.EPSILON_SVR || - param.svm_type == svm_parameter.NU_SVR) + if(param.svm_type == ONE_CLASS || + param.svm_type == EPSILON_SVR || + param.svm_type == NU_SVR) { // regression or one-class-svm model.nr_class = 2; @@ -1949,8 +1954,8 @@ public static svm_model svm_train(svm_problem prob, svm_parameter param) model.sv_coef = new double[1][]; if(param.probability == 1 && - (param.svm_type == svm_parameter.EPSILON_SVR || - param.svm_type == svm_parameter.NU_SVR)) + (param.svm_type == EPSILON_SVR || + param.svm_type == NU_SVR)) { model.probA = new double[1]; model.probA[0] = svm_svr_probability(prob,param); @@ -1963,14 +1968,14 @@ public static svm_model svm_train(svm_problem prob, svm_parameter param) int nSV = 0; int i; for(i=0;i 0) ++nSV; + if(abs(f.alpha[i]) > 0) ++nSV; model.l = nSV; model.SV = new svm_node[nSV][]; model.sv_coef[0] = new double[nSV]; model.sv_indices = new int[nSV]; int j = 0; for(i=0;i 0) + if(abs(f.alpha[i]) > 0) { model.SV[j] = prob.x[i]; model.sv_coef[0][j] = f.alpha[i]; @@ -2066,10 +2071,10 @@ public static svm_model svm_train(svm_problem prob, svm_parameter param) f[p] = svm_train_one(sub_prob,param,weighted_C[i],weighted_C[j]); for(k=0;k 0) + if(!nonzero[si+k] && abs(f[p].alpha[k]) > 0) nonzero[si+k] = true; for(k=0;k 0) + if(!nonzero[sj+k] && abs(f[p].alpha[ci+k]) > 0) nonzero[sj+k] = true; ++p; } @@ -2102,7 +2107,7 @@ public static svm_model svm_train(svm_problem prob, svm_parameter param) model.probB=null; } - int nnz = 0; + int total_sv = 0; int[] nz_count = new int[nr_class]; model.nSV = new int[nr_class]; for(i=0;i some folds may have zero elements - if((param.svm_type == svm_parameter.C_SVC || - param.svm_type == svm_parameter.NU_SVC) && nr_fold < l) + if((param.svm_type == C_SVC || + param.svm_type == NU_SVC) && nr_fold < l) { int[] tmp_nr_class = new int[1]; int[][] tmp_label = new int[1][]; @@ -2266,8 +2271,8 @@ public static void svm_cross_validation(svm_problem prob, svm_parameter param, i } svm_model submodel = svm_train(subprob,param); if(param.probability==1 && - (param.svm_type == svm_parameter.C_SVC || - param.svm_type == svm_parameter.NU_SVC)) + (param.svm_type == C_SVC || + param.svm_type == NU_SVC)) { double[] prob_estimates= new double[svm_get_nr_class(submodel)]; for(j=begin;j0)?1:-1; else return sum; @@ -2397,9 +2402,9 @@ public static double svm_predict(svm_model model, svm_node[] x) { int nr_class = model.nr_class; double[] dec_values; - if(model.param.svm_type == svm_parameter.ONE_CLASS || - model.param.svm_type == svm_parameter.EPSILON_SVR || - model.param.svm_type == svm_parameter.NU_SVR) + if(model.param.svm_type == ONE_CLASS || + model.param.svm_type == EPSILON_SVR || + model.param.svm_type == NU_SVR) dec_values = new double[1]; else dec_values = new double[nr_class*(nr_class-1)/2]; @@ -2409,7 +2414,7 @@ public static double svm_predict(svm_model model, svm_node[] x) public static double svm_predict_probability(svm_model model, svm_node[] x, double[] prob_estimates) { - if ((model.param.svm_type == svm_parameter.C_SVC || model.param.svm_type == svm_parameter.NU_SVC) && + if ((model.param.svm_type == C_SVC || model.param.svm_type == NU_SVC) && model.probA!=null && model.probB!=null) { int i; @@ -2424,7 +2429,7 @@ public static double svm_predict_probability(svm_model model, svm_node[] x, doub for(i=0;i 1) return "nu <= 0 or nu > 1"; - if(svm_type == svm_parameter.EPSILON_SVR) + if(svm_type == EPSILON_SVR) if(param.p < 0) return "p < 0"; @@ -2780,12 +2786,12 @@ public static String svm_check_parameter(svm_problem prob, svm_parameter param) return "probability != 0 and probability != 1"; if(param.probability == 1 && - svm_type == svm_parameter.ONE_CLASS) + svm_type == ONE_CLASS) return "one-class SVM probability output not supported yet"; // check whether nu-svc is feasible - if(svm_type == svm_parameter.NU_SVC) + if(svm_type == NU_SVC) { int l = prob.l; int max_nr_class = 16; @@ -2830,7 +2836,7 @@ public static String svm_check_parameter(svm_problem prob, svm_parameter param) for(int j=i+1;j Math.min(n1,n2)) + if(param.nu*(n1+n2)/2 > min(n1,n2)) return "specified nu is infeasible"; } } @@ -2841,9 +2847,9 @@ public static String svm_check_parameter(svm_problem prob, svm_parameter param) public static int svm_check_probability_model(svm_model model) { - if (((model.param.svm_type == svm_parameter.C_SVC || model.param.svm_type == svm_parameter.NU_SVC) && + if (((model.param.svm_type == C_SVC || model.param.svm_type == NU_SVC) && model.probA!=null && model.probB!=null) || - ((model.param.svm_type == svm_parameter.EPSILON_SVR || model.param.svm_type == svm_parameter.NU_SVR) && + ((model.param.svm_type == EPSILON_SVR || model.param.svm_type == NU_SVR) && model.probA!=null)) return 1; else diff --git a/java/libsvm/svm.m4 b/java/libsvm/svm.m4 index 8e11835a..1c9a8d23 100644 --- a/java/libsvm/svm.m4 +++ b/java/libsvm/svm.m4 @@ -2,10 +2,14 @@ define(`swap',`do {$1 tmp=$2; $2=$3; $3=tmp;} while(false)') define(`Qfloat',`float') define(`SIZE_OF_QFLOAT',4) define(`TAU',1e-12) +define(`NULL',null) +define(`bool',boolean) changecom(`//',`') package libsvm; import java.io.*; import java.util.*; +import static java.lang.Math.*; +import static libsvm.svm_parameter.*; // // Kernel Cache @@ -33,7 +37,7 @@ class Cache { for(int i=0;iInteger.MAX_VALUE/100 ? Integer.MAX_VALUE : 100*l); - int counter = Math.min(l,1000)+1; + int max_iter = max(10000000, l>Integer.MAX_VALUE/100 ? Integer.MAX_VALUE : 100*l); + int counter = min(l,1000)+1; int[] working_set = new int[2]; while(iter < max_iter) @@ -467,7 +472,7 @@ class Solver { if(--counter == 0) { - counter = Math.min(l,1000); + counter = min(l,1000); if(shrinking!=0) do_shrinking(); svm.info("."); } @@ -601,8 +606,8 @@ class Solver { // update alpha_status and G_bar { - boolean ui = is_upper_bound(i); - boolean uj = is_upper_bound(j); + bool ui = is_upper_bound(i); + bool uj = is_upper_bound(j); update_alpha_status(i); update_alpha_status(j); int k; @@ -674,7 +679,7 @@ class Solver { { // return i,j such that // i: maximizes -y_i * grad(f)_i, i in I_up(\alpha) - // j: mimimizes the decrease of obj value + // j: minimizes the decrease of obj value // (if quadratic coefficeint <= 0, replace it with tau) // -y_j*grad(f)_j < -y_i*grad(f)_i, j in I_low(\alpha) @@ -705,8 +710,8 @@ class Solver { } int i = Gmax_idx; - Qfloat[] Q_i = null; - if(i != -1) // null Q_i not accessed: Gmax=-INF if i=-1 + Qfloat[] Q_i = NULL; + if(i != -1) // NULL Q_i not accessed: Gmax=-INF if i=-1 Q_i = Q.get_Q(i,active_size); for(int j=0;j 0) - ub = Math.min(ub,yG); + if(y[i] < 0) + ub = min(ub,yG); else - lb = Math.max(lb,yG); + lb = max(lb,yG); } - else if(is_upper_bound(i)) + else if(is_lower_bound(i)) { - if(y[i] < 0) - ub = Math.min(ub,yG); + if(y[i] > 0) + ub = min(ub,yG); else - lb = Math.max(lb,yG); + lb = max(lb,yG); } - else + else { ++nr_free; sum_free += yG; @@ -948,9 +954,9 @@ final class Solver_NU extends Solver int ip = Gmaxp_idx; int in = Gmaxn_idx; - Qfloat[] Q_ip = null; - Qfloat[] Q_in = null; - if(ip != -1) // null Q_ip not accessed: Gmaxp=-INF if ip=-1 + Qfloat[] Q_ip = NULL; + Qfloat[] Q_in = NULL; + if(ip != -1) // NULL Q_ip not accessed: Gmaxp=-INF if ip=-1 Q_ip = Q.get_Q(ip,active_size); if(in != -1) Q_in = Q.get_Q(in,active_size); @@ -1007,7 +1013,7 @@ final class Solver_NU extends Solver } } - if(Math.max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2) < eps || Gmin_idx == -1) + if(max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2) < eps || Gmin_idx == -1) return 1; if(y[Gmin_idx] == +1) @@ -1019,7 +1025,7 @@ final class Solver_NU extends Solver return 0; } - private boolean be_shrunk(int i, double Gmax1, double Gmax2, double Gmax3, double Gmax4) + private bool be_shrunk(int i, double Gmax1, double Gmax2, double Gmax3, double Gmax4) { if(is_upper_bound(i)) { @@ -1068,7 +1074,7 @@ final class Solver_NU extends Solver } } - if(unshrink == false && Math.max(Gmax1+Gmax2,Gmax3+Gmax4) <= eps*10) + if(unshrink == false && max(Gmax1+Gmax2,Gmax3+Gmax4) <= eps*10) { unshrink = true; reconstruct_gradient(); @@ -1102,10 +1108,10 @@ final class Solver_NU extends Solver { if(y[i]==+1) { - if(is_lower_bound(i)) - ub1 = Math.min(ub1,G[i]); - else if(is_upper_bound(i)) - lb1 = Math.max(lb1,G[i]); + if(is_upper_bound(i)) + lb1 = max(lb1,G[i]); + else if(is_lower_bound(i)) + ub1 = min(ub1,G[i]); else { ++nr_free1; @@ -1114,10 +1120,10 @@ final class Solver_NU extends Solver } else { - if(is_lower_bound(i)) - ub2 = Math.min(ub2,G[i]); - else if(is_upper_bound(i)) - lb2 = Math.max(lb2,G[i]); + if(is_upper_bound(i)) + lb2 = max(lb2,G[i]); + else if(is_lower_bound(i)) + ub2 = min(ub2,G[i]); else { ++nr_free2; @@ -1365,12 +1371,12 @@ public class svm { for(i=0;i 0) + if(abs(alpha[i]) > 0) { ++nSV; if(prob.y[i] > 0) { - if(Math.abs(alpha[i]) >= si.upper_bound_p) + if(abs(alpha[i]) >= si.upper_bound_p) ++nBSV; } else { - if(Math.abs(alpha[i]) >= si.upper_bound_n) + if(abs(alpha[i]) >= si.upper_bound_n) ++nBSV; } } @@ -1579,7 +1585,7 @@ public class svm { int iter; // Initial Point and Initial Fun Value - A=0.0; B=Math.log((prior0+1.0)/(prior1+1.0)); + A=0.0; B=log((prior0+1.0)/(prior1+1.0)); double fval = 0.0; for (i=0;i=0) - fval += t[i]*fApB + Math.log(1+Math.exp(-fApB)); + fval += t[i]*fApB + log(1+exp(-fApB)); else - fval += (t[i] - 1)*fApB +Math.log(1+Math.exp(fApB)); + fval += (t[i] - 1)*fApB +log(1+exp(fApB)); } for (iter=0;iter= 0) { - p=Math.exp(-fApB)/(1.0+Math.exp(-fApB)); - q=1.0/(1.0+Math.exp(-fApB)); + p=exp(-fApB)/(1.0+exp(-fApB)); + q=1.0/(1.0+exp(-fApB)); } else { - p=1.0/(1.0+Math.exp(fApB)); - q=Math.exp(fApB)/(1.0+Math.exp(fApB)); + p=1.0/(1.0+exp(fApB)); + q=exp(fApB)/(1.0+exp(fApB)); } d2=p*q; h11+=dec_values[i]*dec_values[i]*d2; @@ -1621,7 +1627,7 @@ public class svm { } // Stopping Criteria - if (Math.abs(g1)= 0) - newf += t[i]*fApB + Math.log(1+Math.exp(-fApB)); + newf += t[i]*fApB + log(1+exp(-fApB)); else - newf += (t[i] - 1)*fApB +Math.log(1+Math.exp(fApB)); + newf += (t[i] - 1)*fApB +log(1+exp(fApB)); } // Check sufficient decrease if (newf= 0) - return Math.exp(-fApB)/(1.0+Math.exp(-fApB)); + return exp(-fApB)/(1.0+exp(-fApB)); else - return 1.0/(1+Math.exp(fApB)) ; + return 1.0/(1+exp(fApB)) ; } // Method 2 from the multiclass_prob paper by Wu, Lin, and Weng private static void multiclass_probability(int k, double[][] r, double[] p) { int t,j; - int iter = 0, max_iter=Math.max(100,k); + int iter = 0, max_iter=max(100,k); double[][] Q=new double[k][k]; double[] Qp=new double[k]; double pQp, eps=0.005/k; @@ -1716,7 +1723,7 @@ public class svm { double max_error=0; for (t=0;tmax_error) max_error=error; } @@ -1833,17 +1840,17 @@ public class svm { for(i=0;i 5*std) + if (abs(ymv[i]) > 5*std) count=count+1; else - mae+=Math.abs(ymv[i]); + mae+=abs(ymv[i]); mae /= (prob.l-count); svm.info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma="+mae+"\n"); return mae; @@ -1937,20 +1944,20 @@ public class svm { svm_model model = new svm_model(); model.param = param; - if(param.svm_type == svm_parameter.ONE_CLASS || - param.svm_type == svm_parameter.EPSILON_SVR || - param.svm_type == svm_parameter.NU_SVR) + if(param.svm_type == ONE_CLASS || + param.svm_type == EPSILON_SVR || + param.svm_type == NU_SVR) { // regression or one-class-svm model.nr_class = 2; - model.label = null; - model.nSV = null; - model.probA = null; model.probB = null; + model.label = NULL; + model.nSV = NULL; + model.probA = NULL; model.probB = NULL; model.sv_coef = new double[1][]; if(param.probability == 1 && - (param.svm_type == svm_parameter.EPSILON_SVR || - param.svm_type == svm_parameter.NU_SVR)) + (param.svm_type == EPSILON_SVR || + param.svm_type == NU_SVR)) { model.probA = new double[1]; model.probA[0] = svm_svr_probability(prob,param); @@ -1963,14 +1970,14 @@ public class svm { int nSV = 0; int i; for(i=0;i 0) ++nSV; + if(abs(f.alpha[i]) > 0) ++nSV; model.l = nSV; model.SV = new svm_node[nSV][]; model.sv_coef[0] = new double[nSV]; model.sv_indices = new int[nSV]; int j = 0; for(i=0;i 0) + if(abs(f.alpha[i]) > 0) { model.SV[j] = prob.x[i]; model.sv_coef[0][j] = f.alpha[i]; @@ -2022,12 +2029,12 @@ public class svm { // train k*(k-1)/2 models - boolean[] nonzero = new boolean[l]; + bool[] nonzero = new bool[l]; for(i=0;i 0) + if(!nonzero[si+k] && abs(f[p].alpha[k]) > 0) nonzero[si+k] = true; for(k=0;k 0) + if(!nonzero[sj+k] && abs(f[p].alpha[ci+k]) > 0) nonzero[sj+k] = true; ++p; } @@ -2098,11 +2105,11 @@ public class svm { } else { - model.probA=null; - model.probB=null; + model.probA=NULL; + model.probB=NULL; } - int nnz = 0; + int total_sv = 0; int[] nz_count = new int[nr_class]; model.nSV = new int[nr_class]; for(i=0;i some folds may have zero elements - if((param.svm_type == svm_parameter.C_SVC || - param.svm_type == svm_parameter.NU_SVC) && nr_fold < l) + if((param.svm_type == C_SVC || + param.svm_type == NU_SVC) && nr_fold < l) { int[] tmp_nr_class = new int[1]; int[][] tmp_label = new int[1][]; @@ -2266,8 +2273,8 @@ public class svm { } svm_model submodel = svm_train(subprob,param); if(param.probability==1 && - (param.svm_type == svm_parameter.C_SVC || - param.svm_type == svm_parameter.NU_SVC)) + (param.svm_type == C_SVC || + param.svm_type == NU_SVC)) { double[] prob_estimates= new double[svm_get_nr_class(submodel)]; for(j=begin;j0)?1:-1; else return sum; @@ -2397,9 +2404,9 @@ public class svm { { int nr_class = model.nr_class; double[] dec_values; - if(model.param.svm_type == svm_parameter.ONE_CLASS || - model.param.svm_type == svm_parameter.EPSILON_SVR || - model.param.svm_type == svm_parameter.NU_SVR) + if(model.param.svm_type == ONE_CLASS || + model.param.svm_type == EPSILON_SVR || + model.param.svm_type == NU_SVR) dec_values = new double[1]; else dec_values = new double[nr_class*(nr_class-1)/2]; @@ -2409,8 +2416,8 @@ public class svm { public static double svm_predict_probability(svm_model model, svm_node[] x, double[] prob_estimates) { - if ((model.param.svm_type == svm_parameter.C_SVC || model.param.svm_type == svm_parameter.NU_SVC) && - model.probA!=null && model.probB!=null) + if ((model.param.svm_type == C_SVC || model.param.svm_type == NU_SVC) && + model.probA!=NULL && model.probB!=NULL) { int i; int nr_class = model.nr_class; @@ -2424,7 +2431,7 @@ public class svm { for(i=0;i 1) return "nu <= 0 or nu > 1"; - if(svm_type == svm_parameter.EPSILON_SVR) + if(svm_type == EPSILON_SVR) if(param.p < 0) return "p < 0"; @@ -2780,12 +2788,12 @@ public class svm { return "probability != 0 and probability != 1"; if(param.probability == 1 && - svm_type == svm_parameter.ONE_CLASS) + svm_type == ONE_CLASS) return "one-class SVM probability output not supported yet"; // check whether nu-svc is feasible - if(svm_type == svm_parameter.NU_SVC) + if(svm_type == NU_SVC) { int l = prob.l; int max_nr_class = 16; @@ -2830,21 +2838,21 @@ public class svm { for(int j=i+1;j Math.min(n1,n2)) + if(param.nu*(n1+n2)/2 > min(n1,n2)) return "specified nu is infeasible"; } } } - return null; + return NULL; } public static int svm_check_probability_model(svm_model model) { - if (((model.param.svm_type == svm_parameter.C_SVC || model.param.svm_type == svm_parameter.NU_SVC) && - model.probA!=null && model.probB!=null) || - ((model.param.svm_type == svm_parameter.EPSILON_SVR || model.param.svm_type == svm_parameter.NU_SVR) && - model.probA!=null)) + if (((model.param.svm_type == C_SVC || model.param.svm_type == NU_SVC) && + model.probA!=NULL && model.probB!=NULL) || + ((model.param.svm_type == EPSILON_SVR || model.param.svm_type == NU_SVR) && + model.probA!=NULL)) return 1; else return 0; @@ -2852,7 +2860,7 @@ public class svm { public static void svm_set_print_string_function(svm_print_interface print_func) { - if (print_func == null) + if (print_func == NULL) svm_print_string = svm_print_stdout; else svm_print_string = print_func; diff --git a/java/svm_predict.java b/java/svm_predict.java index 0868f12b..a44ea3a4 100644 --- a/java/svm_predict.java +++ b/java/svm_predict.java @@ -38,7 +38,7 @@ private static void predict(BufferedReader input, DataOutputStream output, svm_m int correct = 0; int total = 0; double error = 0; - double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; + double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0; int svm_type=svm.svm_get_svm_type(model); int nr_class=svm.svm_get_nr_class(model); @@ -69,7 +69,7 @@ private static void predict(BufferedReader input, DataOutputStream output, svm_m StringTokenizer st = new StringTokenizer(line," \t\n\r\f:"); - double target = atof(st.nextToken()); + double target_label = atof(st.nextToken()); int m = st.countTokens()/2; svm_node[] x = new svm_node[m]; for(int j=0;j