一、算法實現
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!");
}
}
二、訓練數據集: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
三、執行結果
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!