source: src/main/java/agents/anac/y2015/Phoenix/GP/CSVtoMatrix.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 4.6 KB
Line 
1package agents.anac.y2015.Phoenix.GP;/* This file is part of the jgpml Project.
2 * http://github.com/renzodenardi/jgpml
3 *
4 * Copyright (c) 2011 Renzo De Nardi and Hugo Gravato-Marques
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28import java.util.ArrayList;
29import java.util.StringTokenizer;
30
31import agents.Jama.Matrix;
32
33import java.io.BufferedReader;
34import java.io.FileReader;
35import java.io.FileNotFoundException;
36import java.io.IOException;
37
38
39/**
40 * Simple Class to load the example data from files straight into Matrices.
41 */
42public class CSVtoMatrix {
43
44 /**
45 * Load data
46 * @param filename data file
47 * @param sizeofInputs
48 * @param sizeofOutputs
49 * @return [X, Y]
50 */
51 public static Matrix[] load(String filename,int sizeofInputs, int sizeofOutputs){
52
53 ArrayList<double[]> inputsList = new ArrayList<double[]>();
54 ArrayList<double[]> outputsList = new ArrayList<double[]>();
55 BufferedReader br = null;
56
57 try {
58 br = new BufferedReader(new FileReader(filename));
59 } catch (FileNotFoundException e) {
60 System.out.println("error: file " + filename + " not found.");
61 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
62 }
63
64 boolean eof;
65 int datasize = 0;
66
67 do {
68 eof = true;
69
70 String readLine = null;
71
72 try {
73 readLine = br.readLine();
74 } catch (IOException e) {
75 System.out.println("error: reading from " + filename + ".");
76 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
77 }
78
79 if (readLine != null && !readLine.equals("")) {
80 eof = false;
81
82 try {
83 double[] in = new double[sizeofInputs];
84 double[] out = new double[sizeofOutputs];
85 StringTokenizer st = new StringTokenizer(readLine, ", ");
86
87 // parse inputs
88 int index = 0;
89 int currentVariable = 0;
90 for (int i = 0; i < sizeofInputs; i++) {
91 in[index] = Double.parseDouble(st.nextToken());
92 index++;
93 currentVariable++;
94 }
95
96 // parse outputs
97 index = 0;
98 for (int i = 0; i < sizeofOutputs; i++) {
99 out[index] = Double.parseDouble(st.nextToken());
100 index++;
101 currentVariable++;
102 }
103
104 inputsList.add(in);
105 outputsList.add(out);
106 }
107 catch (Exception e) {
108 System.out.println(e + "\nerror: this line in the logfile does not agree with the configuration provided... it will be skipped");
109 datasize--;
110 }
111 }
112 datasize++;
113 } while (!eof);
114
115 double[][] inmat = new double[inputsList.size()][sizeofInputs];
116 double[][] outmat = new double[inputsList.size()][sizeofOutputs];
117 inputsList.toArray(inmat);
118 outputsList.toArray(outmat);
119
120 return new Matrix[]{new Matrix(inmat), new Matrix(outmat)};
121 }
122
123 /**
124 * Simple example of how to use this class.
125 * @param args
126 */
127 public static void main(String[] args) {
128
129
130 Matrix[] data = CSVtoMatrix.load("../machinelearning/src/machinelearning/gaussianprocess/armdata.csv",6,1);
131
132
133 }
134
135
136}
Note: See TracBrowser for help on using the repository browser.