测试目标:
1) 对数据挖掘的步骤进行实践:生成、分析、验证
2) 熟悉对WEKA的使用
测试步骤:
1) 通达信导出股票复权数据
2) 通过程序生成: *.arff,包括:训练集和测试集合
3) 对结果进行评估
生成ARFF的代码如下:
File arffTrainingFile=new File("D:/ARFF/${name}Training.arff")
arffTrainingFile.delete()
arffTrainingFile.createNewFile()
arffTrainingFile.withWriter {Writer writer->
writer.println("% @author bruce.lin.chn")
writer.println("% @date ${new Date().toLocaleString()}")
writer.println("@relation \"${name}决策树分析训练\"")
List keyList=metrics[0].keySet().toList().sort()
keyList.each{String key->
if (key !="date")
{
writer.println("@attribute ${key} numeric")
}
}
writer.println("@attribute class { \"Yes\", \"No\"}")
writer.println("@data")
for(int i=-360; i<-90;i++)
{
Map metric=metrics[i]
keyList.each{ String key->
if (key !="date")
{
print "${metric[key]}, "
writer.print "${metric[key]}, "
}
}
println "${IsSuccessfulInvestment(i)}"
if (IsSuccessfulInvestment(i)>0)
writer.println '"Yes"'
else
writer.println '"No"'
}
}
File arffTestingFile=new File("D:/ARFF/${name}Testing.arff")
arffTestingFile.delete()
arffTestingFile.createNewFile()
arffTestingFile.withWriter {Writer writer->
writer.println("% @author bruce.lin.chn")
writer.println("% @date ${new Date().toLocaleString()}")
writer.println("@relation \"${name}决策树分析测试\"")
List keyList=metrics[0].keySet().toList().sort()
keyList.each{String key->
if (key !="date")
{
writer.println("@attribute ${key} numeric")
}
}
writer.println("@attribute class { \"Yes\", \"No\"}")
writer.println("@data")
for(int i=-90; i<-1;i++)
{
Map metric=metrics[i]
keyList.each{ String key->
if (key !="date")
{
print "${metric[key]}, "
writer.print "${metric[key]}, "
}
}
println "${IsSuccessfulInvestment(i)}"
if (IsSuccessfulInvestment(i)>0)
writer.println '"Yes"'
else
writer.println '"No"'
}
}
生成的文件如下:
% @author bruce.lin.chn
% @date 2013-2-4 11:00:55
@relation "SZ002250决策树分析训练"
@attribute amplitude05 numeric
@attribute amplitude10 numeric
@attribute amplitude30 numeric
@attribute amplitude90 numeric
@attribute averageAmplitude05 numeric
@attribute averageAmplitude10 numeric
@attribute averageAmplitude30 numeric
@attribute averageAmplitude90 numeric
@attribute layoutBullAlignment numeric
@attribute layoutUpUpDown numeric
@attribute ma05 numeric
@attribute ma10 numeric
@attribute ma30 numeric
@attribute ma90 numeric
@attribute class { "Yes", "No"}
@data
0.78, 3.06, 9.47, 3.02, 1.4, 2.39, 4.47, 5.07, -1, -1, 97.88, 95.72, 90.11, 95.76, "No"
0.78, 3.13, 10.1, 4.03, 0.66, 2.34, 4.76, 5.01, -1, -1, 98.75, 96.49, 90.39, 95.66, "No"
-0.5, 1.23, 8.1, 2.6, 0.74, 2.17, 4.94, 4.94, -1, -1, 98.53, 96.86, 90.7, 95.56, "No"
-3.07, -1.97, 4.67, -0.36, 1.26, 1.98, 5.09, 4.85, -1, -1, 98.09, 96.98, 90.83, 95.42, "No"
-2.04, -1.95, 4.79, -0.06, 1.73, 1.72, 5.21, 4.79, -1, -1, 97.25, 97.15, 90.91, 95.31, "No"
-0.69, -1.2, 5.49, 0.85, 1.61, 1.6, 5.38, 4.77, -1, -1, 96.74, 97.25, 91.07, 95.27, "No"
1.66, 0.53, 7.34, 2.96, 1.28, 1.39, 5.54, 4.76, -1, -1, 96.46, 97.54, 91.35, 95.24, "No"
-2.44, -3.92, 1.81, -2.1, 1.3, 1.74, 5.46, 4.75, -1, -1, 95.52, 96.98, 91.52, 95.18, "No"
-3.05, -4.62, 0.44, -3.24, 1.95, 2.24, 5.38, 4.75, -1, -1, 94.94, 96.49, 91.64, 95.12, "No"
-0.36, -1.65, 2.78, -0.66, 1.96, 2.1, 5.25, 4.71, -1, -1, 94.78, 96.02, 91.88, 95.07, "No"
0.08, -1.12, 2.47, -0.51, 1.57, 1.91, 4.92, 4.68, -1, -1, 94.49, 95.65, 92.29, 95.06, "No"
-0.54, -2.14, 0.25, -2.14, 0.9, 1.61, 4.34, 4.68, -1, -1, 93.49, 95.03, 92.76, 95.03, "No"
-0.29, -1.41, 0.09, -1.86, 0.91, 1.41, 3.83, 4.68, -1, -1, 93.53, 94.59, 93.18, 95.03, "No"
-0.28, -0.94, -0.01, -1.51, 0.67, 1.45, 3.38, 4.66, -1, -1, 93.86, 94.49, 93.61, 95.03, "No"
-0.45, -1.17, -0.82, -1.9, 0.49, 1.45, 3.02, 4.63, -1, -1, 93.65, 94.34, 94.0, 95.03, "No"
-0.98, -1.81, -2.18, -2.84, 0.39, 1.29, 2.72, 4.59, -1, -1, 93.21, 93.99, 94.35, 95.0, "No"
-0.68, -1.06, -2.27, -2.63, 0.59, 0.72, 2.51, 4.56, -1, -1, 93.09, 93.44, 94.6, 94.95, "No"
-2.63, -3.37, -4.99, -5.17, 1.07, 1.11, 2.44, 4.57, -1, -1, 92.44, 93.15, 94.74, 94.92, "Yes"
-0.59, -1.76, -3.53, -3.59, 1.08, 1.25, 2.43, 4.55, -1, -1, 92.02, 93.11, 94.81, 94.88, "No"
... ... ...
分析的代码如下:
Classifier classifier= new J48()
ArffLoader arffLoader=new ArffLoader()
arffLoader.setFile(new File("D:/ARFF/SZ002250Training.arff"))
Instances instancesTraining=arffLoader.getDataSet()
instancesTraining.setClassIndex(14)
println "共加载:"+instancesTraining.numInstances().toString()
classifier.buildClassifier(instancesTraining)
println "训练完毕!"
println "classifier.graph()********************************************"
println classifier.graph()
arffLoader.setFile(new File("D:/ARFF/SZ002250Testing.arff"))
Instances instancesTesting=arffLoader.getDataSet()
instancesTesting.setClassIndex(14)
int sum=instancesTesting.numInstances()
int successful=0
for(int i=0;i<sum; i++)
{
//分析结果与实际结果相同?
if (classifier.classifyInstance(instancesTesting.instance(i))==instancesTesting.instance(i).classValue())
{
successful++
}
}
println "正确率:"+(successful*100.0/sum).toString()+"%"
生成的决策树:
输出的结果:
正确率:86.5168539326%