Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Please add an example CsvWriter Class. #663

Closed
volley84 opened this issue Jun 25, 2017 · 5 comments
Closed

Please add an example CsvWriter Class. #663

volley84 opened this issue Jun 25, 2017 · 5 comments

Comments

@volley84
Copy link

Or is there the examples?

@cesarsouza
Copy link
Member

cesarsouza commented Jun 26, 2017

Hi there,

Thanks for opening the issue! May I ask how you would like to use the CsvWriter? For example, are you trying to write matrices, jagged arrays, DataTables, or other specific types to .csv files?

I am just asking so I can craft a relevant example for your intended usage.

Regards,
Cesar

@volley84
Copy link
Author

volley84 commented Jun 29, 2017

Thanks for your reply. I wrote the code below, but how do you "CsvWriter"?

void WriteFloatToCsv(float[] Data, int ClassText)
{
string path = @"Data.csv";
{
using (StreamWriter sw = File.AppendText(path))
{
for (int i = 0; i < jointDataF.Length;i++)
{
sw.Write(jointDataF[i]);
sw.Write(",");
}
sw.Write(ClassText);
sw.WriteLine();
}
}

Is there still a fix for the code "CsvReader" below?

void ReadCsvToDouble(string path, out double[][] inputs, out int[] outputs)
{
double[][] train;
CsvReader cr = new CsvReader(path,true);
train = cr.ToJagged();
inputs = new double[train.Length][];
for(int i = 0; i<train.Length;i++)
inputs[i] = new double[train[i].Length-1];
outputs = new int[train.Length];
for(int i=0;i<inputs.Length;i++)
for(int j=0;j<inputs[i].Length;j++)
inputs[i][j] = train[i][j];
for (int i = 0; i < outputs.Length; i++)
outputs[i] = Convert.ToInt32(train[i][train[i].Length-1]);
}

@cesarsouza
Copy link
Member

cesarsouza commented Jun 29, 2017

Hi there,

Here is an example on how to write a matrix to a .csv file:

string filename = "matrix.csv";

double[,] values =
{
    { 1,  2,  3,  4 },
    { 5,  6,  7,  8 },
    { 9, 10, 11, 12 },
};

using (CsvWriter writer = new CsvWriter(filename))
{
    writer.WriteHeaders("a", "b", "c", "d"); // this is optional, but helps readability afterwards
    writer.Write(values);
}

and here is how to read it back using CsvReader:

CsvReader reader = new CsvReader(filename, hasHeaders: true);
double[,] actual = reader.ToMatrix();

It also work with jagged arrays (double[][]) if you replace ToMatrix with ToJagged:

CsvReader reader = new CsvReader(filename, hasHeaders: true);
double[][] actual = reader.ToJagged();

@cesarsouza
Copy link
Member

If you are storing the output labels as the last column of your csv file, you can use the following code to retrieve it:

double[][] inputs = actual.Get(null, 0, -1); // null means 'all rows', and 0 to -1 means from first column to the one before the last
int[] outputs = actual.GetColumn(-1).ToInt32(); // will get the last column and convert to int

@cesarsouza
Copy link
Member

Added in release 3.6.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants