Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to do and-operation, my csv is
0,0,0
0,1,0
1,0,0
1,1,1

I want to get 2 arrays: input and output array
data (input)
0,0
0,1
1,0
1,1
classes (output)
0
0
0
1
I try this code:

What I have tried:

C#
double[][] input = new double[samples][];
double[][] output = new double[samples][];
public static void convertCSV()
        {
            StreamReader reader = new StreamReader("D:\\and.csv");
            samples = 0;
            
            try
            {
                string str = null;
                while((samples<50)&&((str=reader.ReadLine())!=null))
                {
                    string[] strs = str.Split(';');
                    if (strs.Length == 1)
                        strs = str.Split(',');
                    
                    //// parse tokens
                    tempData[samples, 0] = double.Parse(strs[0]);
                    tempData[samples, 1] = double.Parse(strs[1]);
                    tempClasses[samples] = int.Parse(strs[2]);

                    
                    samples++;
                  
                }
                data = new double[samples, 2];
                Array.Copy(tempData, 0, data, 0, samples * 2);
                classes = new int[samples];
                Array.Copy(tempClasses, 0, classes, 0, samples);

                
            }
            catch(Exception)
            {
                return;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }
        public static void SearchSolution()
        {
            // prepare learning data
            
            for (int i = 0; i < samples; i++)
            {
                input[i] = new double[2];
                output[i] = new double[1];

                //// set input
                input[i][0] = data[i, 0];
                input[i][1] = data[i, 1];

                //// set output
                output[i][0] = classes[i];

               
                }
            //}
        }


my code cant give my 2 arrays to use it in training, I want some help if possible, please
Posted
Updated 17-Feb-20 2:07am
v2
Comments
CHill60 17-Feb-20 4:12am    
What happens when you run and debug your code?
This is a bad construct
catch(Exception)
            {
                return;
            }
because you will never know what error occurred.
You haven't declared tempData or tempClasses - could that be your problem?
Richard MacCutchan 17-Feb-20 6:56am    
Your code does not appear to do anything apart from trying to read the numbers and moving them around from place to place. You initially read them into tempData, only to then copy them into data, so you can finally copy them from there to input and output. What exactly are you trying to achieve?
Member 14129828 17-Feb-20 7:43am    
I want to separate the input from the output to use them in training by using
trainingSet = new BasicNeuralDataSet(input, output);
Richard MacCutchan 17-Feb-20 8:08am    
See my Solution below.

1 solution

The following code will split your data into the two arrays you have declared:
C#
static int samples = 4;    // adjust this to the actual number to be processed

static double[,] input = new double[samples,2];
static double[] output = new double[samples];

public static void convertCSV()
{
    StreamReader reader = new StreamReader("D:\\and.csv");
    int index = 0;
    
    try
    {
        string str;
        while(index < samples && (str = reader.ReadLine()) != null)
        {
            string[] strs = str.Split(';');
            if (strs.Length == 1)
                strs = str.Split(',');
            
            // parse tokens
            input[index,0] = double.Parse(strs[0]);
            input[index,1] = double.Parse(strs[1]);
            output[index] = double.Parse(strs[2]);
            index++;
        }
    }
    catch(Exception e)
    {
        // do something here, do not ignore exceptions
        Console.WriteLine(e.Message);
    }
    finally
    {
        if (reader != null)
        reader.Close();
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900