给定循环,我想打印最多的四次。有total_A
,total_B
,total_C
并且在这三者中找到最大的数字。但我面临着获得正确最大数量的一些问题。数据训练1号和4号的输出是正确的,它返回最大的数字,但是2号和3号是错误的。以下是我的输出:
更新了输出
以下是我的代码:
public class Multiclass2 {
public static void main(String args []){
double x [][] = {{3.24,-0.96},
{-1.56,-0.61},
{-1.1,2.5},
{1.36,-4.8};
double [] wA = {0,1.94,3.82};
double [] wB = {0,-4.9,-4.03};
double [] wC = {0,4.48,3.25};
double threshold = 1;
int n = x.length;
double total_A = 0;
double total_B = 0;
double total_C = 0;
List<Double> li = new ArrayList<Double>();
double max = 0;
for(int i=0;i<n;i++){
System.out.println("For training data point no." + (i+1));
total_A = (threshold * wA[0]) + (x[i][0] * wA[1]) + (x[i][1] * wA[2]);
total_B = (threshold * wB[0]) + (x[i][0] * wB[1]) + (x[i][1] * wB[2]);
total_C = (threshold * wC[0]) + (x[i][0] * wC[1]) + (x[i][1] * wC[2]);
li.add(total_A);
li.add(total_B);
li.add(total_C);
max = Collections.max(li);
System.out.println(total_A+", "+total_B+", "+total_C);
System.out.println("MAx is "+max);
}
}
}
答案 0 :(得分:2)
您在整个循环中使用相同的集合,因此您正在计算所有数据点的最大值。你犯了两个代码风格的错误,允许这个错误发生:
Math.max(a, Math.max(b, c))
更正后的代码为:
public static void main(String args[]) {
double x[][] = {
{ 3.24, -0.96 },
{ -1.56, -0.61 },
{ -1.1, 2.5 },
{ 1.36, -4.8 }
};
double[] wA = { 0, 1.94, 3.82 };
double[] wB = { 0, -4.9, -4.03 };
double[] wC = { 0, 4.48, 3.25 };
double threshold = 1;
int n = x.length;
for (int i = 0; i < n; i++) {
System.out.println("For training data point no." + (i + 1));
double total_A = (threshold * wA[0]) + (x[i][0] * wA[1]) + (x[i][1] * wA[2]);
double total_B = (threshold * wB[0]) + (x[i][0] * wB[1]) + (x[i][1] * wB[2]);
double total_C = (threshold * wC[0]) + (x[i][0] * wC[1]) + (x[i][1] * wC[2]);
double max = Math.max(total_A, Math.max(total_B, total_C));
System.out.println(total_A + ", " + total_B + ", " + total_C);
System.out.println("Max is " + max);
}
}