Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my code i am passing double dimensional array values observation to k means algorithm, but my problem I want to make it dynamic, instead if already initialized value.

How could I make code that take input from keyboard and store it in multidimensional array then pass it to k means function.
C#
 static class Program 
      {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
          
          
           
     double[][] observations =
            {
                 new double[] {  1,  2,  3 },
                 new double[] {  1,  3,  2 },
                 new double[] {  2,  1,  3 },
                 new double[] {  2,  3,  1 },
                 new double[] {  3,  1,  2 },
                 new double[] {  3,  2,  1 },
                 new double[] {  2,  1,  3 },
                 new double[] {  2,  1,  3 },
                 new double[] {  2,  1,  3 },
                 new double[] {  2,  1,  3 },
                 new double[] {  3,  2,  1 },
                 new double[] {  3,  2,  1 },
               //new double[] {  100,  100,  100 },
              // new double[] {  120,  120,  120 },
              // new double[] {  200,  200,  200 },
               //new double[] {  201,  202,  203 },
               //new double[] {   500,  500,  500 },
               //new double[] {  12,  13,  11 },
               //new double[] {  15,  12,  11 },
            };
         
            // Create a new K-Means algorithm with 3 clusters 
            KMeans kmeans = new KMeans(3);

            // Compute the algorithm, retrieving an integer array
            //  containing the labels for each of the observations
            int[] medoid = kmeans.Compute(observations);
            
            
           
            // As a result, the first two observations should belong to the
            //  same cluster (thus having the same label). The same should
            //  happen to the next four observations and to the last three.

            Console.Write("The most efficient prediction via K-Means is : ");
            for (int counter = 0; counter < medoid.Length; counter++)
            {
               Console.Write(medoid[counter] + " ");
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            
        }
    }
}
Posted
Updated 22-Dec-11 19:34pm
v2

You will have to use Console.ReadLine[^], combined with int.Parse[^] to convert the input (which will be string) to integers:

C#
string s = Console.ReadLine();
int i = int.Parse(s);

(That is the simplest way, but I would prefer to use int.TryParse[^] instead as it allows the user to make errors without crashing you application).

You will then need to loop round, collecting values and inserting them in your array. Since you are using a two-dimensional array, you will need two nested loops (one inside the other, each with a index variable to say where in the array data is supposed to go).
 
Share this answer
 
This is one way to do it, but I do not like:

C#
string line = string.Empty;

List<double[]> observations = new List<double[]>();

while (line != ".")
{
    line = Console.ReadLine();

    string[] matches = Regex.Split(line, @"\s+");

    double[] array = (from match in matches
        select double.Parse(match)).ToArray<double>();

    observations.Add(array);
}


This is bad, because every time you need to add a valuable from beginning (also parsing errors). This is not a problem if you use a little input, but from my experience the essence of artificial intelligence is to work with a lot of information. My recommendation is to use files to read values.
 
Share this answer
 
v3
Comments
Drazen Pupovac 23-Dec-11 4:29am    
Instruction:

Enter number separately with space, like this: 2 3 4

To break loop, enter '.' and press 'enter'

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