k表示人工智能的算法实现

k表示人工智能的算法实现

本文介绍了k表示人工智能的算法实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我正在将二维数组值观察传递给k表示算法,但是我的问题是要使其具有动态性,而不是已经初始化的值.

如何制作从键盘获取输入并将其存储在多维数组中的代码,然后将其传递给k表示函数.

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.

 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();


        }
    }
}

推荐答案

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


(这是最简单的方法,但是我宁愿使用 int.TryParse [ ^ ]代替,因为它允许用户进行错误操作而不会使您的应用程序崩溃).

然后,您将需要循环,收集值并将其插入到数组中.由于您使用的是二维数组,因此将需要两个嵌套循环(一个在另一个循环内,每个循环都有一个索引变量来说明数据应该在数组中的哪个位置).


(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).


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.


这篇关于k表示人工智能的算法实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:41