sample.cs

来自「It s the kNN or else named k-Nearest Nea」· CS 代码 · 共 68 行

CS
68
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace kNN
{
    class Sample
    {

        private ArrayList items;
        private string class_;

        public Sample(ArrayList sample, int target)
        {
            this.class_ = sample[target].ToString();
            this.items = sample.Clone() as ArrayList;
            this.items.RemoveAt(target);
            TransferItems();

        }
        public void TransferItems()
        {
            ArrayList temp = new ArrayList();
            foreach (string item in this.items)
            {
                string[] q = (item.Split('.'));
                if (q.Length > 1)
                {
                    string a = q[0].ToString() + "," + q[1].ToString();
                    temp.Add(a);
                }
                else temp.Add(item);
                this.items = temp;
            }
        }

        public double CountDistance(Sample neighbour)
        {
            
            double d = 0;
            for (int i = 0; i < this.items.Count; i++)
            {
                double temp1=Convert.ToDouble(this.items[i]);
                    double temp2 = Convert.ToDouble(neighbour.GetItems()[i]);
                d += Math.Pow((temp1-temp2), 2);
            }
            return Math.Sqrt(d);
        }



        public ArrayList GetItems()
        {
            return this.items;
        }
        public string GetClass()
        {
            return this.class_;
        }
        public Sample GetSample()
        {
            return this;
        }

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?