java实现机器学习计算系数(解决不规则矩形问题)

1 这里先创建个对象aaa

class AAA {
//名字和年龄为x,成绩为y

String name;
Sting age;
String performance;

}

2定义方法

class aaa{
   public static Instances getData(List<AAA> personList) {

        /**
         * 1.创建数据格式
         */
        //创建两个数值型
        Attribute num1 = new Attribute("num1");
        Attribute num2 = new Attribute("num2");
        Attribute result = new Attribute("result");

        //创建一个relation关系
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(num1);
        attributes.add(num2);
        attributes.add(result);

        //把创建的格式应用在数据集中
        //如果知道数据有多少行,就可以不用0
        Instances dataset = new Instances("Test-dataset", attributes, 0);
        for (AAA aaa : personList) {
            double[] values = new double[dataset.numAttributes()];
            //2为y,其他为x,(根据自己需要创建多个X)
            values[0] = aaa.name
            values[1] = aaa.age
            values[2] = aaa.performance
            dataset.add(new DenseInstance(1.0, values));
        }

        return dataset;
    }
}

3 打印

Instances data = getData(list);
        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }

        // 构建模型
        LinearRegression lr = new LinearRegression();
        lr.buildClassifier(data);

        List<Double> v = new ArrayList<>();
        // 输出回归系数
        double[] coefficients = lr.coefficients();
        for (int i = 0; i < coefficients.length; i++) {
            String name = "";
            if (i == coefficients.length - 1) {
                name = "常量";
            } else if (i == coefficients.length - 2) {
                name = "因变量";
            } else {
                name = data.attribute(i).name();
            }
            double b = coefficients[i];
            v.add(b);
            System.out.println(vin+"  " + name + ": " + b);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值