基于weka实现的神经网络算法实现

原创
2016/06/23 18:03
阅读数 4.7K

1、算法实现 

package demo7;

import java.io.File;
import java.io.IOException;

import weka.classifiers.functions.MultilayerPerceptron;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;

/**
 * 基于weka实现的神经网络算法实现
 * @author mengfeiyang
 *
 */
public class BPDeep2 {  
  
    public static void main(String[] args) throws IOException {  
        //便于测试,用数组保存一些数据,从数据库中取数据是同理的  
        //二维数组第一列表示当月的实际数据,第二列是上个月的数据,用于辅助对当月数据的预测的  
        //二维数组的数据用于测试集数据,为了展示两种weka载入数据的方法,将训练集数据从arff文件中读取  
        double[][] a = 
        	{
        		{-0.93,-0.995},
        		{-0.93,-0.93},
        		{-0.93,-0.93},
        		{-0.95,-0.93},
        		{-0.93,-0.95},  
                {-0.95,-0.93},
                {-0.93,-0.95},
                {-0.93,-0.93},
                {-0.95,-0.93},
                {-0.9,-0.95},  
                {-0.92,-0.9},
                {-0.575,-0.92},
                {-0.23,-0.575}
            };  
          
        //读入训练集数据  
        String path = "E:/imgfolder/train.arff";
        System.out.println(path);
        File inputFile = new File(path);//该文件见源代码最后的分享链接,可以下载后将路径替换掉  
        ArffLoader atf = new ArffLoader();  
        try {  
            atf.setFile(inputFile);  
        } catch (IOException e1) {  
            e1.printStackTrace();  
        }  
        Instances instancesTrain = atf.getDataSet();   
        instancesTrain.setClassIndex(0);//设置训练数据集的类属性,即对哪个数据列进行预测(属性的下标从0开始)  
          
        //读入测试集数据  
        FastVector attrs = new FastVector();  
              
        Attribute ratio = new Attribute("CUR",1);//创建属性,参数为属性名称和属性号,但属性号并不影响FastVector中属性的顺序  
        Attribute preratio = new Attribute("PRE",2);  
              
        attrs.addElement(ratio);//向FastVector中添加属性,属性在FastVector中的顺序由添加的先后顺序确定。  
        attrs.addElement(preratio);  
              
        Instances instancesTest = new Instances("bp",attrs,attrs.size());//创建实例集,即数据集,参数为名称,FastVector类型的属性集,以及属性集的大小(即数据集的列数)  
              
        instancesTest.setClass(ratio);//设置数据集的类属性,即对哪个数据列进行预测  
          
        for(int k=0;k<13;k++){  
            Instance ins = new Instance(attrs.size());//创建实例,即一条数据  
            ins.setDataset(instancesTest);//设置该条数据对应的数据集,和数据集的属性进行对应  
            ins.setValue(ratio, a[k][0]);//设置数据每个属性的值  
            ins.setValue(preratio, a[k][1]);   
            instancesTest.add(ins);//将该条数据添加到数据集中  
        }  
          
        MultilayerPerceptron m_classifier = new MultilayerPerceptron();//创建算法实例,要使用其他的算法,只用把类换做相应的即可  
          
        try {  
            m_classifier.buildClassifier(instancesTrain); //进行训练  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
          
        for(int i = 0;i<13;i++){//测试分类结果  
            //instancesTest.instance(i)获得的是用模型预测的结果值,instancesTest.instance(i).classValue()获得的是测试集类属性的值  
            //此处是把预测值和实际值同时输出,进行对比  
            try {  
                System.out.println(m_classifier.classifyInstance(instancesTest.instance(i))+",,,"+instancesTest.instance(i).classValue());  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
        System.out.println("bp success!");  
    }  
  
}  

2、训练数据集:train.arff 

@relation 'test'
@attribute CUR numeric
@attribute PRE numeric
@data
-0.81,-0.85
-0.8,-0.81
-0.82,-0.8
-0.83,-0.82
-0.913,-0.83
-0.75,-0.913
-0.945,-0.75
-0.94,-0.945
-0.877,-0.94
-0.814,-0.877
-0.92,-0.814
-0.9025,-0.92
-0.886,-0.9025
-0.78,-0.886
-0.97,-0.78
-0.9145,-0.97
-0.85,-0.9145
-0.97,-0.85
-0.9,-0.97
-0.97,-0.9
-0.91,-0.97
-0.85,-0.91
-0.8,-0.85
-0.9249,-0.8
-0.93,-0.9249
-0.99,-0.93
-0.933,-0.99
-0.002,-0.933
-0.958,-0.002
-1,-0.958
-0.4115,-1
-0.951,-0.4115
-0.67,-0.951
-0.91091425,-0.67
-0.95,-0.91091425
-0.9454,-0.95
-0.9,-0.9454
-0.92,-0.9
-0.92075,-0.92
-0.914175,-0.92075
-0.95,-0.914175
-0.65,-0.95
-0.998,-0.65
-0.8795,-0.998
-0.9,-0.8795
-0.9995,-0.9

3、执行结果

E:/imgfolder/train.arff
-0.8387689167154422,,,-0.93
-0.8435666578599861,,,-0.93
-0.8435666578599861,,,-0.93
-0.8435666578599861,,,-0.95
-0.8420460436187248,,,-0.93
-0.8435666578599861,,,-0.95
-0.8420460436187248,,,-0.93
-0.8435666578599861,,,-0.93
-0.8435666578599861,,,-0.95
-0.8420460436187248,,,-0.9
-0.8459234640790234,,,-0.92
-0.8443420634975742,,,-0.575
-0.8777601385075624,,,-0.23
bp success!

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
1 收藏
分享
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部