PLA算法Java实现——机器学习基石

package Machine_learning.PLA;

/**
 * Created by unclewang on 2017/3/22.
 */
public class Weight {
    public double w0;
    public double w1;
    public double w2;
    public  Weight(){

    }

    public Weight(double w0,double w1,double w2){
        this.w0=w0;
        this.w1=w1;
        this.w2=w2;
    }
}
package Machine_learning.PLA;

/**
 * Created by unclewang on 2017/3/22.
 */
public class Item {
    public int x0;
    public double x1;
    public double x2;
    public int label;
    public Item(double x1,double x2,int label){

        this.x1=x1;
        this.x2=x2;
        this.label=label;
    }
}
package Machine_learning.PLA;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by unclewang on 2017/3/22.
 */
public class PLA_Demo {

    public static int sign(double x) {
        if (x > 0)
            return 1;
        else if (x < 0)
            return -1;
        else
            return 0;
    }

    public static double dotpro(Item item, Weight wight) {
        return item.x0 * wight.w0 + item.x1 * wight.w1 + item.x2 * wight.w2;
    }

    public static Weight updatewight(Item item, Weight weight) {
        Weight newWeight = new Weight();
        newWeight.w0 = weight.w0 + item.x0 * item.label;
        newWeight.w1 = weight.w1 + item.x1 * item.label;
        newWeight.w2 = weight.w2 + item.x2 * item.label;
        return newWeight;
    }

    public static void main(String[] args) {
        List<Item> vector = new ArrayList<>();
        Item temp1 = new Item(1, 2, 1);
        vector.add(temp1);
        Item temp2 = new Item(2, 2, 1);
        vector.add(temp2);
        Item temp3 = new Item(3, 2, -1);
        Item temp4 = new Item(4, 2, -1);
        vector.add(temp3);
        vector.add(temp4);
        Weight weight = new Weight(0, 0, 0);
        System.out.println(vector.size());
        for (int i = 0; i < vector.size(); i++) {
            Item item = vector.get(i);
            if (item.label != sign(dotpro(item, weight))) {
                System.out.println("那个点出问题了?"+(i+1));
                weight = updatewight(item, weight);
                System.out.println("  " + weight.w1 + "  " + weight.w2);
                i = -1;
            }
        }
        System.out.println("  " + weight.w1 + "  " + weight.w2);
    }
}

其实w0没什么意义
强化了我继续学习下去的信心

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值