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没什么意义
强化了我继续学习下去的信心