ResultSet rs = st.executeQuery("SELECT * FROM history whereTime='2015-10-12,1'");
数据处理
这部分使用weka API中的j48算法和svm算法来对数据进行处理
Weka连接数据库
查询涨幅不小于0.1的数据
InstanceQuery query = new InstanceQuery(); query.setDatabaseURL("jdbc:mysql://localhost:3306/test"); query.setUsername("root"); query.setPassword(""); query.setQuery("SELECT `Open`,Highest,Lowest,`Close`,`Change`,Increase,Amplitude,HandsAll,Money from history where Increase>=0.1;");
//Get context with jQuery - using jQuery's .get() method. var ctx = $("#myChart").get(0).getContext("2d"); //This will get the first returned node in the jQuery collection. var myNewChart = new Chart(ctx);
public classDataUtil{ public DataUtil() throws Exception{ Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = ""; Connection conn = DriverManager.getConnection(url, user, password); Statement st = conn.createStatement(); Statement st1 = conn.createStatement(); ResultSet rs = st .executeQuery("SELECT Increase FROM history where Time='2015-10-12,1'"); ResultSet rss = st1 .executeQuery("SELECT Increase FROM history where Time='2015-10-13,2'"); while (rs.next()&&rss.next()) { String str = rs.getString("Increase"); String str2=rss.getString("Increase"); newFile("./train.txt", str+","+str);//保存2015年10月12日的涨幅情况用于训练 newFile("./test.txt", str2+","+str2);//保存2015年10月13日的涨幅情况用于测试 }
}
public static void newFile(String filePathAndName, String fileContent) { try {
File myFilePath = new File(filePathAndName.toString()); if (!myFilePath.exists()) { // 如果该文件不存在,则创建 myFilePath.createNewFile(); } // FileWriter(myFilePath, true); 实现不覆盖追加到文件里
// FileWriter(myFilePath); 覆盖掉原来的内容 FileWriter resultFile = new FileWriter(myFilePath, true); PrintWriter myFile = new PrintWriter(resultFile);
// 给文件里面写内容,原来的会覆盖掉 myFile.println(fileContent);
resultFile.close();
} catch (Exception e) { e.printStackTrace();
}
}
}
SVR预测类
public class Predict { publicstaticvoid main(String[] args) throws Exception { new DataUtil();//获取测试数据和训练数据 List<Double> label = new ArrayList<Double>(); List<svm_node[]> nodeSet = new ArrayList<svm_node[]>(); getData(nodeSet, label, "./train.txt");
int dataRange = nodeSet.get(0).length; svm_node[][] datas = new svm_node[nodeSet.size()][dataRange]; // 训练集的向量表 for (int i = 0; i < datas.length; i++) { for (int j = 0; j < dataRange; j++) { datas[i][j] = nodeSet.get(i)[j]; } } double[] lables = newdouble[label.size()]; // a,b 对应的lable for (int i = 0; i < lables.length; i++) { lables[i] = label.get(i); } // 定义svm_problem对象 svm_problem problem = new svm_problem(); problem.l = nodeSet.size(); // 向量个数 problem.x = datas; // 训练集向量表 problem.y = lables; // 对应的lable数组 // 定义svm_parameter对象 svm_parameter param = new svm_parameter(); param.svm_type = svm_parameter.EPSILON_SVR; param.kernel_type = svm_parameter.LINEAR; param.cache_size = 100; param.eps = 0.00001; param.C = 1.9; // 训练SVM分类模型 System.out.println(svm.svm_check_parameter(problem, param)); // 如果参数没有问题,则svm.svm_check_parameter()函数返回null,否则返回error描述。 svm_model model = svm.svm_train(problem, param); // svm.svm_train()训练出SVM分类模型 // 获取测试数据 List<Double> testlabel = new ArrayList<Double>(); List<svm_node[]> testnodeSet = new ArrayList<svm_node[]>(); getData(testnodeSet, testlabel, "./test.txt"); svm_node[][] testdatas = new svm_node[testnodeSet.size()][dataRange]; // 训练集的向量表 for (int i = 0; i < testdatas.length; i++) { for (int j = 0; j < dataRange; j++) { testdatas[i][j] = testnodeSet.get(i)[j]; } } double[] testlables = newdouble[testlabel.size()]; // a,b 对应的lable for (int i = 0; i < testlables.length; i++) { testlables[i] = testlabel.get(i); } // 预测测试数据的lable double err = 0.0; for (int i = 0; i < testdatas.length; i++) { double truevalue = testlables[i]; System.out.print("真实值:"+truevalue + " "); double predictValue = svm.svm_predict(model, testdatas[i]); System.out.println("预测值:"+predictValue); err += Math.abs(predictValue - truevalue); Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = ""; Connection conn = DriverManager.getConnection(url, user, password); Statement st = conn.createStatement(); st.executeUpdate("insert into predictresult(truevalue,predictvalue) values('"+truevalue+"'"+","+"'"+predictValue+"');"); conn.close(); DataUtil.newFile("./result.txt", "真实值:"+truevalue + " "+"预测值:"+predictValue+" "+"err=" + err / datas.length); } /*System.out.println("err=" + err / datas.length);*/ }
Bar.defaults = { //Boolean - If we show the scale above the chart data scaleOverlay :false, //Boolean - If we want to override with a hard coded scale scaleOverride :false, //** Required if scaleOverride is true ** //Number - The number of steps in a hard coded scale scaleSteps :null, //Number - The value jump in the hard coded scale scaleStepWidth :null, //Number - The scale starting value scaleStartValue :null,
//String - Colour of the scale line scaleLineColor :"rgba(0,0,0,.1)", //Number - Pixel width of the scale line scaleLineWidth :1,
//Boolean - Whether to show labels on the scale scaleShowLabels :false, //Interpolated JS string - can access value scaleLabel :"<%=value%>", //String - Scale label font declaration for the scale label scaleFontFamily :"'Arial'", //Number - Scale label font size in pixels scaleFontSize :12, //String - Scale label font weight style scaleFontStyle :"normal", //String - Scale label font colour scaleFontColor :"#666", ///Boolean - Whether grid lines are shown across the chart scaleShowGridLines :true, //String - Colour of the grid lines scaleGridLineColor :"rgba(0,0,0,.05)", //Number - Width of the grid lines scaleGridLineWidth :1,
//Boolean - If there is a stroke on each bar barShowStroke :true, //Number - Pixel width of the bar stroke barStrokeWidth :2, //Number - Spacing between each of the X value sets barValueSpacing :5, //Number - Spacing between data sets within X values barDatasetSpacing :1, //Boolean - Whether to animate the chart animation :true,
//Number - Number of animation steps animationSteps :60, //String - Animation easing effect animationEasing :"easeOutQuart",
//Function - Fires when the animation is complete onAnimationComplete :null }
蛛网图标参数
Radar.defaults = { //Boolean - If we show the scale above the chart data scaleOverlay :false, //Boolean - If we want to override with a hard coded scale scaleOverride :false, //** Required if scaleOverride is true ** //Number - The number of steps in a hard coded scale scaleSteps :null, //Number - The value jump in the hard coded scale scaleStepWidth :null, //Number - The centre starting value scaleStartValue :null, //Boolean - Whether to show lines for each scale point scaleShowLine :true,
//String - Colour of the scale line scaleLineColor :"rgba(0,0,0,.1)", //Number - Pixel width of the scale line scaleLineWidth :1,
//Boolean - Whether to show labels on the scale scaleShowLabels :false, //Interpolated JS string - can access value scaleLabel :"<%=value%>", //String - Scale label font declaration for the scale label scaleFontFamily :"'Arial'", //Number - Scale label font size in pixels scaleFontSize :12, //String - Scale label font weight style scaleFontStyle :"normal", //String - Scale label font colour scaleFontColor :"#666", //Boolean - Show a backdrop to the scale label scaleShowLabelBackdrop :true, //String - The colour of the label backdrop scaleBackdropColor :"rgba(255,255,255,0.75)", //Number - The backdrop padding above & below the label in pixels scaleBackdropPaddingY :2, //Number - The backdrop padding to the side of the label in pixels scaleBackdropPaddingX :2, //Boolean - Whether we show the angle lines out of the radar angleShowLineOut :true, //String - Colour of the angle line angleLineColor :"rgba(0,0,0,.1)", //Number - Pixel width of the angle line angleLineWidth :1, //String - Point label font declaration pointLabelFontFamily :"'Arial'", //String - Point label font weight pointLabelFontStyle :"normal", //Number - Point label font size in pixels pointLabelFontSize :12, //String - Point label font colour pointLabelFontColor :"#666", //Boolean - Whether to show a dot for each point pointDot :true, //Number - Radius of each point dot in pixels pointDotRadius :3, //Number - Pixel width of point dot stroke pointDotStrokeWidth :1, //Boolean - Whether to show a stroke for datasets datasetStroke :true, //Number - Pixel width of dataset stroke datasetStrokeWidth :2, //Boolean - Whether to fill the dataset with a colour datasetFill :true, //Boolean - Whether to animate the chart animation :true,
//Number - Number of animation steps animationSteps :60, //String - Animation easing effect animationEasing :"easeOutQuart",
//Function - Fires when the animation is complete onAnimationComplete :null }
运行
右键单击Run as-> MyEclipse Server Application,启动后在浏览器里输入:localhost:8080/dataviewt/index.jsp可查看不同可视化效果 主要比较股票在2015-10-12,1和2015-10-13,2这两个不同时间段的行情指数