java Perceptron

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class PerceptronApproach {  
    private static final int T = 100; // 最大迭代次数  
      
    /** 
     *  
     * @param dataSet:数据集 
     * @param weight:每条数据的权重 
     * @return 
     */  
    public ArrayList<Double> getWeightVector(ArrayList<ArrayList<Double>> dataSet, ArrayList<Double> dataWeight,ArrayList<Double> dataOutput) {  
        int dataLength = 0; 
        double a = 0.1;
        if(null == dataSet) {  
            return null;  
        } else {  
            dataLength = dataSet.get(0).size();  
        }  
          
        // 初始化感知器的权重向量  
        ArrayList<Double> sensorWeightVector = new ArrayList<Double>();  
        
        for(int i = 0; i < dataLength; i++) {  
            sensorWeightVector.add(1d);//添加1,d是类型双精度浮点值  
        }  

          
        // 初始化感知器的增量  
//      int increment = 1;  
          
        int sign = 0; // 迭代终止的条件: 权值向量的的值连续dataSet.size()次大于0  
        for(int i = 0; i < T && sign < dataSet.size(); i++) { // 最大迭代次数  
            for(int z = 0; z < dataSet.size(); z++) {  
                double result = 0;  
                for(int j = 0 ; j < dataLength; j++) {  
                    result += dataSet.get(z).get(j) * sensorWeightVector.get(j);//得到加权值  
                }  
                
                // b为阈值
                double b = 4;
                result =result - b;
                int d_value;
                if(result > 0){
                    d_value = 1;
                }
                else{
                    d_value = 0;
                }
                
                //权值更新一次
                for(int k=0;k<sensorWeightVector.size();k++){
                    sensorWeightVector.set(k, sensorWeightVector.get(k)-a*(d_value-dataOutput.get(z))*dataSet.get(z).get(k));
                    System.out.println(sensorWeightVector);
                }
       
            }  
        }  
        System.out.println(sensorWeightVector);
        return sensorWeightVector;  
    }  
      
    public static void main(String[] args) {  
        File f = new File("D:/Workspaces/MyEclipse 8.5/cellOutlierDetection/data/traindatainput.txt");  
        BufferedReader reader = null;  
          
        try {  
            reader = new BufferedReader(new FileReader(f));  
            String str = null;  
            try {  
                ArrayList<ArrayList<Double>> dataSet = new ArrayList<ArrayList<Double>>();  
                while((str = reader.readLine()) != null) {  
                    ArrayList<Double> tmpList = new ArrayList<Double>();  
                    String[] s = str.split("\t");  
                    for(int i = 0; i < s.length; i++) {  
                        tmpList.add(Double.parseDouble(s[i]));  
                    }  
                    dataSet.add(tmpList); 
                    System.out.println(str);
                }  
                ArrayList<Double> dataOutput = new ArrayList<Double>();
                File OutputF = new File("D:/Workspaces/MyEclipse 8.5/cellOutlierDetection/data/traindataout.txt");
                BufferedReader OutputBr = new BufferedReader(new FileReader(OutputF));
                String Outputstr = null;
                while((Outputstr = OutputBr.readLine())!= null){
                    dataOutput.add(Double.parseDouble(Outputstr));
                    System.out.println(Outputstr);
                }
                
                  
                ArrayList<Double> dataWeight = new ArrayList<Double>();  
                for(int i = 0; i < dataSet.size(); i++) {  
                    dataWeight.add(1d);  
                }  

                
                PerceptronApproach d = new PerceptronApproach();  
                d.getWeightVector(dataSet, dataWeight,dataOutput);  
                  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
              
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
    }  
      
}  

因为需要自己根据训练数据设定权值的更新方式,加上对MATLAB不熟悉,所以用java重写了单层感知器。源代码百度来的,以后找到网址再贴上来,然后根据自己的需要修改了权重跟新方式。保存在此,供自己以后学习和查看。

转载于:https://www.cnblogs.com/asdfgh/p/4277157.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值