本文整理汇总了Java中org.datavec.api.records.reader.impl.csv.CSVRecordReader类的典型用法代码示例。如果您正苦于以下问题:Java CSVRecordReader类的具体用法?Java CSVRecordReader怎么用?Java CSVRecordReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CSVRecordReader类属于org.datavec.api.records.reader.impl.csv包,在下文中一共展示了CSVRecordReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void test() throws Exception {
CSVRecordReader rr = new CSVRecordReader(0, ',');
rr.initialize(new FileSplit(new ClassPathResource("iris.dat").getFile()));
CSVRecordReader rr2 = new CSVRecordReader(0, ',');
rr2.initialize(new FileSplit(new ClassPathResource("iris.dat").getFile()));
RecordReader rrC = new ConcatenatingRecordReader(rr, rr2);
int count = 0;
while(rrC.hasNext()){
rrC.next();
count++;
}
assertEquals(300, count);
}
示例2: createDataSource
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
private void createDataSource() throws IOException, InterruptedException {
//First: get the dataset using the record reader. CSVRecordReader handles loading/parsing
int numLinesToSkip = 0;
String delimiter = ",";
RecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter);
recordReader.initialize(new InputStreamInputSplit(dataFile));
//Second: the RecordReaderDataSetIterator handles conversion to DataSet objects, ready for use in neural network
int labelIndex = 4; //5 values in each row of the iris.txt CSV: 4 input features followed by an integer label (class) index. Labels are the 5th value (index 4) in each row
int numClasses = 3; //3 classes (types of iris flowers) in the iris data set. Classes have integer values 0, 1 or 2
DataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize, labelIndex, numClasses);
DataSet allData = iterator.next();
allData.shuffle();
SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.80); //Use 80% of data for training
trainingData = testAndTrain.getTrain();
testData = testAndTrain.getTest();
//We need to normalize our data. We'll use NormalizeStandardize (which gives us mean 0, unit variance):
DataNormalization normalizer = new NormalizerStandardize();
normalizer.fit(trainingData); //Collect the statistics (mean/stdev) from the training data. This does not modify the input data
normalizer.transform(trainingData); //Apply normalization to the training data
normalizer.transform(testData); //Apply normalization to the test data. This is using statistics calculated from the *training* set
}
示例3: createDataSource
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
private void createDataSource() throws IOException, InterruptedException {
//First: get the dataset using the record reader. CSVRecordReader handles loading/parsing
int numLinesToSkip = 0;
String delimiter = ",";
RecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter);
recordReader.initialize(new InputStreamInputSplit(dataFile));
//Second: the RecordReaderDataSetIterator handles conversion to DataSet objects, ready for use in neural network
int labelIndex = 11;
DataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize, labelIndex, labelIndex, true);
DataSet allData = iterator.next();
SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.80); //Use 80% of data for training
trainingData = testAndTrain.getTrain();
testData = testAndTrain.getTest();
//We need to normalize our data. We'll use NormalizeStandardize (which gives us mean 0, unit variance):
DataNormalization normalizer = new NormalizerStandardize();
normalizer.fit(trainingData); //Collect the statistics (mean/stdev) from the training data. This does not modify the input data
normalizer.transform(trainingData); //Apply normalization to the training data
normalizer.transform(testData); //Apply normalization to the test data. This is using statistics calculated from the *training* set
}
示例4: simpleTransformTest
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void simpleTransformTest() throws Exception {
Schema schema = new Schema.Builder().addColumnDouble("0").addColumnDouble("1").addColumnDouble("2")
.addColumnDouble("3").addColumnDouble("4").build();
TransformProcess transformProcess = new TransformProcess.Builder(schema).removeColumns("0").build();
CSVRecordReader csvRecordReader = new CSVRecordReader();
csvRecordReader.initialize(new FileSplit(new ClassPathResource("iris.dat").getFile()));
TransformProcessRecordReader transformProcessRecordReader =
new TransformProcessRecordReader(csvRecordReader, transformProcess);
assertEquals(4, transformProcessRecordReader.next().size());
}
示例5: testReset
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testReset() throws Exception {
CSVRecordReader rr = new CSVRecordReader(0, ',');
rr.initialize(new FileSplit(new ClassPathResource("iris.dat").getFile()));
int nResets = 5;
for (int i = 0; i < nResets; i++) {
int lineCount = 0;
while (rr.hasNext()) {
List<Writable> line = rr.next();
assertEquals(5, line.size());
lineCount++;
}
assertFalse(rr.hasNext());
assertEquals(150, lineCount);
rr.reset();
}
}
示例6: testResetWithSkipLines
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testResetWithSkipLines() throws Exception {
CSVRecordReader rr = new CSVRecordReader(10, ',');
rr.initialize(new FileSplit(new ClassPathResource("iris.dat").getFile()));
int lineCount = 0;
while (rr.hasNext()) {
rr.next();
++lineCount;
}
assertEquals(140, lineCount);
rr.reset();
lineCount = 0;
while (rr.hasNext()) {
rr.next();
++lineCount;
}
assertEquals(140, lineCount);
}
示例7: testCsvSkipAllLines
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test(expected = NoSuchElementException.class)
public void testCsvSkipAllLines() throws IOException, InterruptedException {
final int numLines = 4;
final List<Writable> lineList = Arrays.asList((Writable) new IntWritable(numLines - 1),
(Writable) new Text("one"), (Writable) new Text("two"), (Writable) new Text("three"));
String header = ",one,two,three";
List<String> lines = new ArrayList<>();
for (int i = 0; i < numLines; i++)
lines.add(Integer.toString(i) + header);
File tempFile = File.createTempFile("csvSkipLines", ".csv");
FileUtils.writeLines(tempFile, lines);
CSVRecordReader rr = new CSVRecordReader(numLines, ',');
rr.initialize(new FileSplit(tempFile));
rr.reset();
assertTrue(!rr.hasNext());
rr.next();
}
示例8: testCsvSkipAllButOneLine
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testCsvSkipAllButOneLine() throws IOException, InterruptedException {
final int numLines = 4;
final List<Writable> lineList = Arrays.<Writable>asList(new Text(Integer.toString(numLines - 1)),
new Text("one"), new Text("two"), new Text("three"));
String header = ",one,two,three";
List<String> lines = new ArrayList<>();
for (int i = 0; i < numLines; i++)
lines.add(Integer.toString(i) + header);
File tempFile = File.createTempFile("csvSkipLines", ".csv");
FileUtils.writeLines(tempFile, lines);
CSVRecordReader rr = new CSVRecordReader(numLines - 1, ',');
rr.initialize(new FileSplit(tempFile));
rr.reset();
assertTrue(rr.hasNext());
assertEquals(rr.next(), lineList);
}
示例9: testStreamReset
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testStreamReset() throws Exception {
CSVRecordReader rr = new CSVRecordReader(0, ',');
rr.initialize(new InputStreamInputSplit(new ClassPathResource("iris.dat").getInputStream()));
int count = 0;
while(rr.hasNext()){
assertNotNull(rr.next());
count++;
}
assertEquals(150, count);
assertFalse(rr.resetSupported());
try{
rr.reset();
fail("Expected exception");
} catch (Exception e){
e.printStackTrace();
}
}
示例10: testCsvRecordReader
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testCsvRecordReader() throws Exception {
SerializerInstance si = sc.env().serializer().newInstance();
assertTrue(si instanceof KryoSerializerInstance);
RecordReader r1 = new CSVRecordReader(1,'\t');
RecordReader r2 = serDe(r1, si);
File f = new ClassPathResource("iris_tab_delim.txt").getFile();
r1.initialize(new FileSplit(f));
r2.initialize(new FileSplit(f));
while(r1.hasNext()){
assertEquals(r1.next(), r2.next());
}
assertFalse(r2.hasNext());
}
示例11: testRRDSIwithAsync
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testRRDSIwithAsync() throws Exception {
RecordReader csv = new CSVRecordReader();
csv.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
int batchSize = 10;
int labelIdx = 4;
int numClasses = 3;
RecordReaderDataSetIterator rrdsi = new RecordReaderDataSetIterator(csv, batchSize, labelIdx, numClasses);
AsyncDataSetIterator adsi = new AsyncDataSetIterator(rrdsi, 8, true);
while (adsi.hasNext()) {
DataSet ds = adsi.next();
}
}
示例12: testNormalizerPrefetchReset
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testNormalizerPrefetchReset() throws Exception {
//Check NPE fix for: https://github.com/deeplearning4j/deeplearning4j/issues/4214
RecordReader csv = new CSVRecordReader();
csv.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
int batchSize = 3;
DataSetIterator iter = new RecordReaderDataSetIterator(csv, batchSize, 4, 4, true);
DataNormalization normalizer = new NormalizerMinMaxScaler(0, 1);
normalizer.fit(iter);
iter.setPreProcessor(normalizer);
iter.inputColumns(); //Prefetch
iter.totalOutcomes();
iter.hasNext();
iter.reset();
iter.next();
}
示例13: testsBasicMeta
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testsBasicMeta() throws Exception {
//As per testBasic - but also loading metadata
RecordReader rr2 = new CSVRecordReader(0, ',');
rr2.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
RecordReaderMultiDataSetIterator rrmdsi = new RecordReaderMultiDataSetIterator.Builder(10)
.addReader("reader", rr2).addInput("reader", 0, 3).addOutputOneHot("reader", 4, 3).build();
rrmdsi.setCollectMetaData(true);
int count = 0;
while (rrmdsi.hasNext()) {
MultiDataSet mds = rrmdsi.next();
MultiDataSet fromMeta = rrmdsi.loadFromMetaData(mds.getExampleMetaData(RecordMetaData.class));
assertEquals(mds, fromMeta);
count++;
}
assertEquals(150 / 10, count);
}
示例14: testSplittingCSVMeta
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testSplittingCSVMeta() throws Exception {
//Here's the idea: take Iris, and split it up into 2 inputs and 2 output arrays
//Inputs: columns 0 and 1-2
//Outputs: columns 3, and 4->OneHot
RecordReader rr2 = new CSVRecordReader(0, ',');
rr2.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
RecordReaderMultiDataSetIterator rrmdsi = new RecordReaderMultiDataSetIterator.Builder(10)
.addReader("reader", rr2).addInput("reader", 0, 0).addInput("reader", 1, 2)
.addOutput("reader", 3, 3).addOutputOneHot("reader", 4, 3).build();
rrmdsi.setCollectMetaData(true);
int count = 0;
while (rrmdsi.hasNext()) {
MultiDataSet mds = rrmdsi.next();
MultiDataSet fromMeta = rrmdsi.loadFromMetaData(mds.getExampleMetaData(RecordMetaData.class));
assertEquals(mds, fromMeta);
count++;
}
assertEquals(150 / 10, count);
}
示例15: testNextAndReset
import org.datavec.api.records.reader.impl.csv.CSVRecordReader; //导入依赖的package包/类
@Test
public void testNextAndReset() throws Exception {
int epochs = 3;
RecordReader rr = new CSVRecordReader();
rr.initialize(new FileSplit(new ClassPathResource("iris.txt").getFile()));
DataSetIterator iter = new RecordReaderDataSetIterator(rr, 150);
MultipleEpochsIterator multiIter = new MultipleEpochsIterator(epochs, iter);
assertTrue(multiIter.hasNext());
while (multiIter.hasNext()) {
DataSet path = multiIter.next();
assertFalse(path == null);
}
assertEquals(epochs, multiIter.epochs);
}