• 大小: 0.05M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-02-24
  • 语言: C#
  • 标签: 算法  数据挖掘  数据  

资源简介

private void Compute()
        {
            for (int i = 0; i < 100; i )
            {
                double distance = -1;
                for (int j = 0; j < 4; j )
                {
                    int tempDistance = (this.Center[j].X - this.Points[i].X) * (this.Center[j].X - this.Points[i].X) (this.Center[j].Y - this.Points[i].Y) * (this.Center[j].Y - this.Points[i].Y);

                    if ((Math.Sqrt(tempDistance) < distance) || j == 0)
                    {
                        distance = Math.Sqrt(tempDistance);
                        Points[i].Focus = j;
                    }
                }
            }
        }

        /// <summary>
        /// 更新中心
        /// </summary>
        private void UpdateCenter()
        {
            for (int i = 0; i < this.K; i )
            {
                int x = 0, y = 0, count = 0;
                for (int j = 0; j < 100; j )
                {
                    if (Points[j].Focus == i)
                    {
                        x = Points[j].X;
                        y = Points[j].Y;
                        count ;
                    }
                }
                //利用三目运算符,防止出现除以0的不合法运算
                x = x / (count == 0 ? 1 : count);
                y = y / (count == 0 ? 1 : count);
                Center[i].X = x;
                Center[i].Y = y;
            }
        }

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Kmeans
{
    public struct Point1
    {
        public int X;
        public int Y;
        public int Focus;
    }

    public partial class Form1 : Form
    {
        public Point1[] Points = new Point1[100];

        public Point1[] Center = new Point1[4];
        int K = 4;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            
            Random randXY = new Random();
            
            //初始化数据
            for (int i = 0; i < 100; i++)
            {

                Random randFocus = new Random();
                //数据对象
                Points[i] = new Point1();
                Points[i].X = randXY.Next(500);
                Points[i].Y = randXY.Next(500)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      11264  2017-03-29 17:31  Kmeans\bin\Debug\Kmeans.exe

     文件      26112  2017-03-29 17:31  Kmeans\bin\Debug\Kmeans.pdb

     文件      11600  2017-03-30 14:34  Kmeans\bin\Debug\Kmeans.vshost.exe

     文件        490  2012-06-06 02:06  Kmeans\bin\Debug\Kmeans.vshost.exe.manifest

     文件       6854  2017-03-29 17:31  Kmeans\Form1.cs

     文件       2098  2012-07-27 15:46  Kmeans\Form1.Designer.cs

     文件       5814  2012-07-27 15:46  Kmeans\Form1.resx

     文件       4746  2017-03-29 14:37  Kmeans\Kmeans.csproj

     文件        452  2017-03-29 14:37  Kmeans\Kmeans.csproj.user

     文件       1533  2017-03-29 14:36  Kmeans\Kmeans.sln

    ..A..H.     22016  2017-03-30 17:28  Kmeans\Kmeans.suo

     文件       6234  2017-03-29 17:31  Kmeans\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1343  2017-03-30 14:34  Kmeans\obj\x86\Debug\Kmeans.csproj.FileListAbsolute.txt

     文件        902  2017-03-29 14:37  Kmeans\obj\x86\Debug\Kmeans.csproj.GenerateResource.Cache

     文件      11264  2017-03-29 17:31  Kmeans\obj\x86\Debug\Kmeans.exe

     文件        180  2017-03-29 14:37  Kmeans\obj\x86\Debug\Kmeans.Form1.resources

     文件      26112  2017-03-29 17:31  Kmeans\obj\x86\Debug\Kmeans.pdb

     文件     101482  2012-07-27 14:51  Kmeans\obj\x86\Debug\ResolveAssemblyReference.cache

     文件       5496  2012-07-27 14:51  Kmeans\Program.cs

     文件       1344  2012-02-15 16:37  Kmeans\Properties\AssemblyInfo.cs

     文件        849  2017-03-29 14:36  Kmeans\UpgradeLog.xml

     文件       3348  2017-03-29 14:36  Kmeans\_UpgradeReport_Files\UpgradeReport.css

     文件      12505  2010-05-04 01:19  Kmeans\_UpgradeReport_Files\UpgradeReport.xslt

     文件         69  2017-03-29 14:36  Kmeans\_UpgradeReport_Files\UpgradeReport_Minus.gif

     文件         71  2017-03-29 14:36  Kmeans\_UpgradeReport_Files\UpgradeReport_Plus.gif

     目录          0  2012-02-15 16:37  Kmeans\obj\x86\Debug\TempPE

     目录          0  2017-03-29 17:31  Kmeans\obj\x86\Debug

     目录          0  2017-03-30 14:33  Kmeans\bin\Debug

     目录          0  2017-03-29 14:35  Kmeans\bin\Release

     目录          0  2012-02-15 16:37  Kmeans\obj\x86

............此处省略8个文件信息

评论

共有 条评论