91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 2KB
    文件類型: .rar
    金幣: 2
    下載: 0 次
    發布日期: 2021-05-12
  • 語言: C#
  • 標簽: 算法??

資源簡介

文件中包含K-Means聚類算法C#版本,一個文件中包含7個函數,使用的時候直接將C#文件復制到項目中即可使用,調用的時候主函數會直接返回結果

資源截圖

代碼片段和文件信息




using?System;
using?System.Collections;
using?System.Collections.Generic;
/**
*?
*?@author?aturbo
*?1、隨機選擇k個點作為中心點(centroid)
*?2、計算點到各個類中心的距離;
*?3、將點放入最近的中心點所在的類
*?4、重新計算中心點
*?5、判斷目標函數是否收斂,收斂停止,否則循環2-4步
*?
*/
public?class?MyKmeans
{

????public?static??double[][]?points?=?{?new?double[]{?1?1?}new?double[]?{?2?1?}new?double[]?{?1?2?}?new?double[]{?2?2?}?new?double[]{?3?3?}?new?double[]{?8?8?}?new?double[]{?9?8?}
????????????new?double[]{?8?9?}?new?double[]{?9?9?}?};

????/**
?*?隨機選擇k個點作為中心點
?*?@param?k?
?*?@return?k個中心點
?*/
????private?static?double[][]?chooseinitK(int?k)
????{
????????double[][]?cluster?=?new?double[k][];
????????List?set?=?new?List();
????????for?(int?i?=?0;?i?????????{
????????????set.Add(i);
????????}
????????//在set中剩下的序列點就為隨機選擇的
????????for?(int?i?=?0;?i?????????{
????????????Random?random?=?new?Random();
????????????int?a?=?random.Next(points.Length);
????????????if?(!set.Contains(a))
????????????????continue;
????????????set.Remove(a);
????????????//System.out.println(“a“?+?a);
????????????i++;
????????}
????????//Iterator?iterator?=?set.iterator();
????????int?j?=?0;
????????foreach?(int?iterator??in?set)
????????{
????????????cluster[j]?=?points[iterator];
????????????j++;
????????}
????????for?(int?i?=?0;?i?????????{
????????????//System.out.println(“隨機選擇的k個節點:“?+?cluster[i][0]?+?“\t“?+?cluster[i][1]);
????????}
????????return?cluster;
????}
????/**
?*?歐式距離計算公式
?*?@param?center?(中心)點
?*?@param?otherpoint?
?*?@return?歐式距離
?*/
????private?static?double?eurDistance(double[]?center?double[]?otherpoint)
????{
????????double?distance?=?0.0;
????????for?(int?i?=?0;?i?????????{
????????????distance?+=?((center[i]?-?otherpoint[i])?*?(center[i]?-?otherpoint[i]));
????????}
????????distance?=?Math.Sqrt(distance);
????????return?distance;
????}
????/**
?*?目標函數——也就是每個聚類中的點到它中心點的距離和
?*?@param?center?中心點
?*?@param?cluster?劃分的(中間)聚類
?*?@return?cost
?*/
????private?static?double?cost(double[][]?center?List[]?cluster)
????{
????????double?cost?=?0.0;
????????for?(int?i?=?0;?i?????????{
????????????for?(int?j?=?0;?j?????????????{
????????????????double?tempCost?=?0.0;
????????????????for?(int?k?=?0;?k?????????????????{
????????????????????//System.out.println(cluster[i].get(j)[k]);
????????????????????tempCost?+=?(cluster[i][j][k]?-?center[i][k])?*?(cluster[i][j][k]?-?center[i][k]);
????????????????}

????????????????cost?+=?Math.Sqrt(tempCost);
????????????}

????????}
????????return?cost;
????}
????/**
?*?聚類算法——將所有點和各中心點計算距離,將點放入最近距離點的類中
?*?@param?points?所有點
?*?@param?centers?中心點
?*?@param?k
?*?@return?聚類
?*/
????private?static?List[]?returnCluster(double[][]?points?double[][]?centers?int?k)
???

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----

?????文件???????5647??2019-04-25?15:50??kmeans\MyKmeans.cs

?????目錄??????????0??2019-04-25?16:07??kmeans

-----------?---------??----------?-----??----

?????????????????5647????????????????????2


評論

共有 條評論