📄 meanvaluesubstitution.java
字号:
/* * $RCSfile: MeanValueSubstitution.java,v $ * $Revision: 1.1 $ * $Date: 2005/04/17 00:38:41 $ * * NeuralNetworkToolkit * Copyright (C) 2004 Universidade de Bras锟絣ia * * This file is part of NeuralNetworkToolkit. * * NeuralNetworkToolkit is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * NeuralNetworkToolkit is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NeuralNetworkToolkit; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 - USA. */package neuralnetworktoolkit.preprocessor;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ResourceBundle;import neuralnetworktoolkit.datamanager.textfilemanager.LoadFromFileException;/** * @author <a href="mailto:hugoiver@yahoo.com.br">Hugo Iver V. Gon锟絘lves</a> * @author <a href="mailto:rodbra@pop.com.br">Rodrigo C. M. Coimbra</a> */public class MeanValueSubstitution { private ResourceBundle resources; /** Stores attributes names. */ private String[] dataHeader; /** * * @param file * @param separator * * @throws LoadFromFileException */ public void substitute(File file, String separator, String missingSymbol) throws MeanValueSubstitutionException { String readedDataHeader; String dataLine; String newLine = new String(); String[] dataValues; double[] means; long[] instancesNumber; File outputFile; FileReader inputReader; FileWriter fileWriter; BufferedReader inputBuffer; BufferedWriter writeBuffer; int size = 0; long fileSize = 0; try { //resources = ResourceBundle.getBundle("neuralnetworktoolkit.preprocessor.resources.Messages"); inputReader = new FileReader(file); outputFile = new File(file.getName() + "-meanS"); fileWriter = new FileWriter(outputFile); outputFile.createNewFile(); writeBuffer = new BufferedWriter(fileWriter); inputBuffer = new BufferedReader(inputReader); readedDataHeader = inputBuffer.readLine(); dataHeader = readedDataHeader.split(separator); size = dataHeader.length; means = new double[size]; instancesNumber = new long[size]; while(inputBuffer.ready()) { dataLine = inputBuffer.readLine(); dataValues = dataLine.split(separator); for (int i = 0; i < size; i++) { if (!missingSymbol.equals(dataValues[i])) { instancesNumber[i]++; means[i] += Double.parseDouble(dataValues[i]); } } fileSize++; } for (int i = 0; i < size; i++) { means[i] = means[i]/instancesNumber[i]; } System.out.println("Fiz a m閐ia"); inputBuffer.close(); inputReader = new FileReader(file); inputBuffer = new BufferedReader(inputReader); inputBuffer.readLine(); writeBuffer.write(readedDataHeader); writeBuffer.newLine(); while(inputBuffer.ready()) { newLine = ""; dataLine = inputBuffer.readLine(); dataValues = dataLine.split(separator); System.out.println(dataLine); if (missingSymbol.equals(dataValues[0])) { newLine += Double.toString(means[0]); } else { newLine += dataValues[0]; } for (int i = 1; i < size; i++) { if (missingSymbol.equals(dataValues[i])) { newLine += separator + Double.toString(means[i]); } else { newLine += separator + dataValues[i]; } } System.out.println(newLine + "--> " + newLine.length()); writeBuffer.write(newLine); writeBuffer.newLine(); } writeBuffer.close(); } catch(FileNotFoundException e) { throw new MeanValueSubstitutionException(resources.getString("fileNotFound") + " " + file.getName() + ".\n"); } catch(IOException e) { throw new MeanValueSubstitutionException(resources.getString("failToRead") +" " + file.getName() + ".\n"); } catch(NumberFormatException e) { throw new MeanValueSubstitutionException("Fail to read file " + file.getName() + ".\nThis is not a valid file!\n"); } } // substitute() public void substitute(String fileName, String separator, String missingSymbol) throws MeanValueSubstitutionException { substitute(new File(fileName), separator, missingSymbol); } //substitute()} //MeanValueSubstitution
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -