📄 readsample.java
字号:
package structure;
import java.io.*;
/**
* 将样本文件中的输入数据读入到一个二维数组中
* 该二维数值中,每一个元组是一个训练数据,第一个值是‘结果’,其他是输入
* */
public class ReadSample {
private int row,col; //二维数组的行和列数
private double[][] in;
private double[] result; //每一组输入的相应的输出结果(监督值)
/**
* 从样本数据中读入数据
* */
public void read(){
File f=new File("sample");
String s;
int begin,i=0;
boolean first=true; //是在读入第一行参数
try{
BufferedReader fin=new BufferedReader(new FileReader(f));
while((s=fin.readLine())!=null){
s=s.trim();
begin=s.indexOf("//");
if (begin==0||s.equalsIgnoreCase("")) continue;
if (first) {
readPara(s);
first=false;
}
else readSample(s,i++);
}
}catch(IOException e){
System.out.println(e);
}
}
/**
* 读入样本的参数,初始化二维数组
* */
private void readPara(String s){
int begin=0,end;
String str;
end=s.indexOf(",");
if(end<=0){
System.out.println("样本数据格式不对");
System.exit(1);
}
str=s.substring(begin,end);
row=Integer.parseInt(str);
begin=end+1;
str=s.substring(begin);
col=Integer.parseInt(str.trim());
in=new double[row][col-1];
result=new double[row];
}
/**
* 读入一行样本数据
* */
private void readSample(String s,int row){
int begin=0,end,j=0;
String str;
end=s.indexOf(",");
while(end>0){
str=s.substring(begin,end);
if(j!=0)
in[row][j-1]=Double.parseDouble(str);
else
result[row]=Double.parseDouble(str);
begin=end+1;
end=s.indexOf(",",begin);
j++;
}
str=s.substring(begin);
in[row][j-1]=Double.parseDouble(str);
}
public double[][] getInput(){
return in;
}
/**
* 返回监督值
* */
public double[] getResult(){
return result;
}
/**
* 打印二维数组
* */
void print(){
for(int i=0;i<row;i++){
for(int j=0;j<col-1;j++){
System.out.print(in[i][j]+", ");
}
System.out.println(+result[i]);
}
}
/**
* 返回二维数组的行数
* */
public int getRow(){
return row;
}
/**
* 返回二维数组的列数<p>
* (包括输入值和输出值)
* */
public int getCol(){
return col;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -