# Load CSV data and apply transformation

**URL:** <https://community.konduit.ai/t/load-csv-data-and-apply-transformation/3294>\
**Category:** DataVec\
**Created:** [December 2, 2024, 6:43am UTC](https://community.konduit.ai/t/load-csv-data-and-apply-transformation/3294 "2024-12-02T06:43:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nilay](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@nilay](https://community.konduit.ai/u/nilay)\
**Post date:** [December 2, 2024, 6:43am UTC](https://community.konduit.ai/t/load-csv-data-and-apply-transformation/3294/1 "2024-12-02T06:43:50Z")

</div>

I want to load data from csv and below is my code

```auto
public DataSet loadData(int batchSize, int numClasses) throws IOException, InterruptedException {
		// Load CSV file as a resource from classpath
		ClassPathResource resource = new ClassPathResource("xyz.csv");

		// Define the schema for the dataset (including input features and target labels)
		Schema schema = new Schema.Builder()
				.addColumnString("Book") // String column for categorical encoding
				.addColumnDouble("Amount") // Numerical column
				.addColumnDouble("Rating") // Numerical column
				.build();

		// Define transformation process (converting string column to categorical and one-hot encoding)
		TransformProcess transformProcess = new TransformProcess.Builder(schema)
				.transform(new StringToCategoricalTransform("Book")) // Convert string to categorical (integer)
				.transform(new CategoricalToOneHotTransform("Book")) // One-hot encoding on the categorical column
				.build();

		// Initialize the CSVRecordReader to read data from the CSV file
		try (CSVRecordReader csvReader = new CSVRecordReader(1, ',')) {
			FileSplit fileSplit = new FileSplit(resource.getFile());
			csvReader.initialize(fileSplit);

			// Read the data in batches
			List<List<Writable>> originalData = csvReader.next(batchSize);

			// Apply transformations to the data
			List<List<Writable>> transformedData = LocalTransformExecutor.execute(originalData, transformProcess);

			// Create a CollectionRecordReader to wrap the transformed data
			CollectionRecordReader recordReader = new CollectionRecordReader(transformedData);

			// Define the RecordReaderDataSetIterator to load the dataset into batches
			int inputSize = 2;
			int outputSize = 1; 
			RecordReaderDataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize, inputSize, inputSize + outputSize);

			// Get the DataSet
			DataSet dataSet = iterator.next();

			// Normalize dataset (if needed)
			DataNormalization normalizer = new NormalizerStandardize();
			normalizer.fit(dataSet);
			normalizer.transform(dataSet);

			return dataSet;
}

```

on transformation i am getting below error  
java.lang.IllegalArgumentException: State names must not be null or empty

basically i have 2 inputs book and amount and one output rating

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [December 2, 2024, 11:34pm UTC](https://community.konduit.ai/t/load-csv-data-and-apply-transformation/3294/2 "2024-12-02T23:34:52Z")

</div>

@nilay do you have more of the stack trace and some of the sample data?
