Skip to content

Commit 7852d27

Browse files
committed
Fixed a distance bug.
1 parent d5f70bb commit 7852d27

File tree

14 files changed

+30
-26
lines changed

14 files changed

+30
-26
lines changed

KRKmeans.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "KRKmeans"
3-
s.version = "2.4"
3+
s.version = "2.4.1"
44
s.summary = "KRKmeans is clustering algorithm (クラスタリング分類) that one of Machine Learning methods."
55
s.description = <<-DESC
66
KRKmeans has implemented K-Means the clustering algorithm (クラスタリング分類) and achieved multi-dimensional clustering in this project. KRKmeans could be used in data mining (データマイニング), image compression (画像圧縮) and classification.
Binary file not shown.

KRKmeans/AppDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// AppDelegate.h
3-
// KRKmeans V2.4
3+
// KRKmeans V2.4.1
44
//
55
// Created by Kalvar on 2014/6/29.
66
// Copyright (c) 2014 - 2015年 Kalvar Lin, [email protected]. All rights reserved.

KRKmeans/AppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// AppDelegate.m
3-
// KRKmeans V2.4
3+
// KRKmeans V2.4.1
44
//
55
// Created by Kalvar on 2014/6/29.
66
// Copyright (c) 2014 - 2015年 Kalvar Lin, [email protected]. All rights reserved.

KRKmeans/ViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// ViewController.h
3-
// KRKmeans V2.4
3+
// KRKmeans V2.4.1
44
//
55
// Created by Kalvar on 2014/6/29.
66
// Copyright (c) 2014 - 2015年 Kalvar Lin, [email protected]. All rights reserved.

KRKmeans/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// ViewController.m
3-
// KRKmeans V2.4
3+
// KRKmeans V2.4.1
44
//
55
// Created by Kalvar on 2014/6/29.
66
// Copyright (c) 2014 - 2015年 Kalvar Lin, [email protected]. All rights reserved.

KRKmeans/main.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// main.m
3-
// KRKmeans V2.4
3+
// KRKmeans V2.4.1
44
//
55
// Created by Kalvar on 2014/6/29.
66
// Copyright (c) 2014 - 2015年 Kalvar Lin, [email protected]. All rights reserved.

KRKmeansTests/KRKmeansTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
2-
// KRKmeans V2.4Tests.m
3-
// KRKmeans V2.4Tests
2+
// KRKmeans V2.4.1Tests.m
3+
// KRKmeans V2.4.1Tests
44
//
55
// Created by Kalvar on 2014/6/29.
66
// Copyright (c) 2014 - 2015年 Kalvar Lin, [email protected]. All rights reserved.

ML/KRKmeans.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
22
// KRKmeans.h
3-
// KRKmeans V2.4
3+
// KRKmeans V2.4.1
44
//
55
// Created by Kalvar on 2014/6/30.
66
// Copyright (c) 2014 - 2015年 Kalvar Lin, [email protected]. All rights reserved.
77
//
88
/*
99
* @ K-Means
10-
* - 條件為 :
10+
* - 演譯原則 :
1111
* 1. 求出 A 集合與 B 集合之中心位置,或將 A, B 集合直接當成初始群聚中心點 (如果該集合裡只有 1 組資料時)
1212
* 2. 將 C 集合依 K-Means 理論放置於 A 與 B 集合裡。
1313
*
@@ -18,14 +18,17 @@
1818
* 4. 迭代運算,將其重新計算每群聚中心點,再重新分群,直至 MIN(新舊中心點誤差值) 小於設定值或與上次群聚中心點相等時,即停止 1 ~ 3 步驟的迭代運算。
1919
* 5. 使用 SSE 來評估每一次的分群結果,把每一次分群後的結果 SSE 值記錄起來,再逐步去調整起始中心點,直至比較出最小的 SSE 值,則該群聚結果即為最佳解。
2020
*
21-
* - K-Means 就是將相關聯( 例如距離最近、數字相關、倍數相關、特徵點相關等 )的數字分類在一起,
22-
* 而後再依照分群結果去做對應結果的方法。
21+
* - K-Means 就是將相關聯( 例如距離最近、數字相關、倍數相關、特徵點相關等 )的數字分類在一起,而後再依照分群結果去做對應結果的方法。
2322
*
2423
* - 已擴充 :
2524
* 1. 將一群隨機產生的 100 組 (x, y) 2 維資料集合,先做分群分類成 N 組集合群聚( N 可自訂 )。
2625
* 2. 再做現在將 C 集合分群的動作。
2726
* 3. 進行分群迭代運算,將每一個分類好的群組重新分群
2827
*
28+
* - 距離公式說明 :
29+
* 1. Cosine Similarity 是種歸屬度的概念,數字越大代表越相近 ( 或是用 1 - 歸屬度,取其差值,就能跟距離概念一樣越小越近了 )
30+
* 2. Euclidean Distance 是距離的概念,數字越小代表越相近
31+
*
2932
*/
3033

3134
#import <Foundation/Foundation.h>

ML/KRKmeans.m

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// KRKmeans.m
3-
// KRKmeans V2.4
3+
// KRKmeans V2.4.1
44
//
55
// Created by Kalvar on 2014/6/30.
66
// Copyright (c) 2014 - 2015年 Kalvar Lin, [email protected]. All rights reserved.
@@ -84,7 +84,7 @@ -(float)_distanceEuclidean2PointsX1:(NSArray *)_x1 x2:(NSArray *)_x2
8484
powf([[_x1 lastObject] floatValue] - [[_x2 lastObject] floatValue], 2));
8585
}
8686

87-
// Euclidean distance which multi-dimensional formula
87+
// Euclidean distance which multi-dimensional formula, 距離越小越近
8888
-(float)_distanceEuclideanX1:(NSArray *)_x1 x2:(NSArray *)_x2
8989
{
9090
NSInteger _index = 0;
@@ -98,7 +98,7 @@ -(float)_distanceEuclideanX1:(NSArray *)_x1 x2:(NSArray *)_x2
9898
return (_index > 0) ? sqrtf(_sum) : _sum;
9999
}
100100

101-
// Cosine Similarity method that multi-dimensional
101+
// Cosine Similarity method that multi-dimensional, 同歸屬度越大越近
102102
-(float)_distanceCosineSimilarityX1:(NSArray *)_x1 x2:(NSArray *)_x2
103103
{
104104
float _sumA = 0.0f;
@@ -115,9 +115,11 @@ -(float)_distanceCosineSimilarityX1:(NSArray *)_x1 x2:(NSArray *)_x2
115115
_sumAB += ( _aValue * _bValue );
116116
++_index;
117117
}
118-
return ( _sumAB / sqrtf( _sumA * _sumB ) );
118+
float _ab = _sumA * _sumB;
119+
return ( _ab > 0.0f ) ? ( _sumAB / sqrtf( _ab ) ) : 0.0f;
119120
}
120121

122+
// 距離概念是越小越近,歸屬度概念是越大越近 ( 或取其差值,使歸屬度同距離越小越近 )
121123
-(float)_distanceX1:(NSArray *)_x1 x2:(NSArray *)_x2
122124
{
123125
float _distance = 0.0f;
@@ -127,7 +129,7 @@ -(float)_distanceX1:(NSArray *)_x1 x2:(NSArray *)_x2
127129
_distance = [self _distanceEuclidean2PointsX1:_x1 x2:_x2];
128130
break;
129131
case KRKmeansDimensionalMultiByCosine:
130-
_distance = [self _distanceCosineSimilarityX1:_x1 x2:_x2];
132+
_distance = 1.0f - [self _distanceCosineSimilarityX1:_x1 x2:_x2];
131133
break;
132134
case KRKmeansDimensionalMultiByEuclidean:
133135
_distance = [self _distanceEuclideanX1:_x1 x2:_x2];
@@ -170,10 +172,9 @@ -(NSMutableArray *)_clusterSources:(NSArray *)_sources compareCenters:(NSArray *
170172
//個別求出要分群的集合跟其它集合體的距離
171173
float _distance = [self _distanceX1:_xySets x2:_eachSets];
172174
//比較出最小的距離,即為歸納分群的對象
173-
//是第 1 筆 || 當前距離 < 上一次的距離
175+
//是第 1 筆 || 當前距離 < 上一次的距離 ( 如距離為歸屬度,則因已在 _distanceX1:X2 裡使用了差值運算,故這裡一樣使用 < 即可 )
174176
if( _index == 0 || _distance < _lastDistance )
175177
{
176-
//記錄起來
177178
_lastDistance = _distance;
178179
_closeIndex = _index;
179180
}

0 commit comments

Comments
 (0)