Skip to content

Commit 638a5e9

Browse files
committed
Added kernel of RBF.
1 parent 160ba2c commit 638a5e9

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

FCM/KRFuzzyCMeans.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
typedef enum KRFuzzyCMeansDistanceFormula
1212
{
1313
// Distance by Cosine Similarity
14-
KRFuzzyCMeansDistanceFormulaByCosine = 0,
14+
KRFuzzyCMeansDistanceFormulaCosine = 0,
1515
// Distance by Euclidean Distance
16-
KRFuzzyCMeansDistanceFormulaByEuclidean = 1
16+
KRFuzzyCMeansDistanceFormulaEuclidean,
17+
// Distance by RBF
18+
KRFuzzyCMeansDistanceFormulaRBF
1719
}KRFuzzyCMeansDistanceFormula;
1820

1921
/*
@@ -49,6 +51,8 @@ typedef void(^KRFuzzyCMeansPerIteration)(NSInteger times, NSArray *clusters, NSA
4951
@property (nonatomic, assign) NSInteger m;
5052
//訓練完是否自動儲存
5153
@property (nonatomic, assign) BOOL doneThenSave;
54+
//使用 RBF 時,能自訂 Sigma 標準差
55+
@property (nonatomic, assign) float sigma;
5256

5357
@property (nonatomic, assign) KRFuzzyCMeansDistanceFormula distanceFormula;
5458

FCM/KRFuzzyCMeans.m

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ @interface KRFuzzyCMeans()
2323
@implementation KRFuzzyCMeans(fixDistance)
2424

2525
// Euclidean distance which multi-dimensional formula, 距離越小越近
26-
-(float)_distanceEuclideanX1:(NSArray *)_x1 x2:(NSArray *)_x2
26+
-(float)_euclideanX1:(NSArray *)_x1 x2:(NSArray *)_x2
2727
{
2828
NSInteger _index = 0;
2929
float _sum = 0.0f;
@@ -32,13 +32,12 @@ -(float)_distanceEuclideanX1:(NSArray *)_x1 x2:(NSArray *)_x2
3232
_sum += powf([_x floatValue] - [[_x2 objectAtIndex:_index] floatValue], 2);
3333
++_index;
3434
}
35-
//return _sum;
3635
// 累加完距離後直接開根號
3736
return (_index > 0) ? sqrtf(_sum) : _sum;
3837
}
3938

4039
// Cosine Similarity method that multi-dimensional, 同歸屬度越大越近
41-
-(float)_distanceCosineSimilarityX1:(NSArray *)_x1 x2:(NSArray *)_x2
40+
-(float)_cosineSimilarityX1:(NSArray *)_x1 x2:(NSArray *)_x2
4241
{
4342
float _sumA = 0.0f;
4443
float _sumB = 0.0f;
@@ -58,17 +57,35 @@ -(float)_distanceCosineSimilarityX1:(NSArray *)_x1 x2:(NSArray *)_x2
5857
return ( _ab > 0.0f ) ? ( _sumAB / sqrtf( _ab ) ) : 0.0f;
5958
}
6059

60+
-(double)_rbf:(NSArray *)_x1 x2:(NSArray *)_x2
61+
{
62+
double _sum = 0.0f;
63+
NSInteger _index = 0;
64+
for( NSNumber *_value in _x1 )
65+
{
66+
// Formula : s = s + ( v1[i] - v2[i] )^2
67+
double _v = [_value doubleValue] - [[_x2 objectAtIndex:_index] doubleValue];
68+
_sum += ( _v * _v );
69+
++_index;
70+
}
71+
// Formula : exp^( -s / ( 2.0f * sigma * sigma ) )
72+
return pow(M_E, ((-_sum) / ( 2.0f * self.sigma * self.sigma )));
73+
}
74+
6175
// 距離概念是越小越近,歸屬度概念是越大越近 ( 或取其差值,使歸屬度同距離越小越近 )
6276
-(float)_distanceX1:(NSArray *)_x1 x2:(NSArray *)_x2
6377
{
6478
float _distance = 0.0f;
6579
switch (self.distanceFormula)
6680
{
67-
case KRFuzzyCMeansDistanceFormulaByCosine:
68-
_distance = 1.0f - [self _distanceCosineSimilarityX1:_x1 x2:_x2];
81+
case KRFuzzyCMeansDistanceFormulaCosine:
82+
_distance = 1.0f - [self _cosineSimilarityX1:_x1 x2:_x2];
6983
break;
70-
case KRFuzzyCMeansDistanceFormulaByEuclidean:
71-
_distance = [self _distanceEuclideanX1:_x1 x2:_x2];
84+
case KRFuzzyCMeansDistanceFormulaEuclidean:
85+
_distance = [self _euclideanX1:_x1 x2:_x2];
86+
break;
87+
case KRFuzzyCMeansDistanceFormulaRBF:
88+
_distance = [self _rbf:_x1 x2:_x2];
7289
break;
7390
default:
7491
break;
@@ -342,8 +359,10 @@ -(instancetype)init
342359
_lastCenters = nil;
343360
_currentIteration = 0;
344361

345-
_distanceFormula = KRFuzzyCMeansDistanceFormulaByEuclidean;
362+
_distanceFormula = KRFuzzyCMeansDistanceFormulaEuclidean;
346363
_trainedSaves = [KRFuzzyCMeansSaves sharedInstance];
364+
365+
_sigma = 2.0f;
347366
}
348367
return self;
349368
}

KRFuzzyCMeans.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 = "KRFuzzyCMeans"
3-
s.version = "1.3"
3+
s.version = "1.4"
44
s.summary = "Fuzzy C-Means is clustering algorithm combined fuzzy theory on Machine Learning."
55
s.description = <<-DESC
66
KRFuzzyCMeans has implemented Fuzzy C-Means (FCM) the fuzzy (ファジー理論) clustering / classification algorithm (クラスタリング分類) in Machine Learning (マシンラーニング). It could be used in data mining (データマイニング) and image compression (画像圧縮).

KRFuzzyCMeans/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (void)viewDidLoad
2222
_krFcm.doneThenSave = YES;
2323
_krFcm.m = 3;
2424
_krFcm.convergenceError = 0.001f;
25-
_krFcm.distanceFormula = KRFuzzyCMeansDistanceFormulaByEuclidean; //KRFuzzyCMeansDistanceFormulaByCosine
25+
_krFcm.distanceFormula = KRFuzzyCMeansDistanceFormulaRBF; //KRFuzzyCMeansDistanceFormulaEuclidean; //KRFuzzyCMeansDistanceFormulaByCosine
2626
[_krFcm addCenters:@[@5.0f, @5.0f]]; //The center 1, cluster 1 start in here
2727
[_krFcm addCenters:@[@10.0f, @10.0f]]; //The center 2, cluster 2 start in here
2828
[_krFcm addCenters:@[@12.0f, @14.0f]]; //The center 3, cluster 3 start in here

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2014 - 2015 Kuo-Ming Lin (Kalvar Lin) (http://kalvar-kaki.blogspot.tw/)
1+
Copyright (c) 2014 - 2016 Kuo-Ming Lin (Kalvar Lin) (http://kalvar-kaki.blogspot.tw/)
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ KRFuzzyCMeans has implemented Fuzzy C-Means (FCM) the fuzzy (ファジー理論)
77

88
```ruby
99
platform :ios, '7.0'
10-
pod "KRFuzzyCMeans", "~> 1.3"
10+
pod "KRFuzzyCMeans", "~> 1.4"
1111
```
1212

1313
## How to use
@@ -17,13 +17,21 @@ pod "KRFuzzyCMeans", "~> 1.3"
1717
#import "KRFuzzyCMeans.h"
1818
```
1919

20+
##### Distance Methods
21+
22+
``` objective-c
23+
KRFuzzyCMeansDistanceFormulaEuclidean
24+
KRFuzzyCMeansDistanceFormulaCosine
25+
KRFuzzyCMeansDistanceFormulaRBF
26+
```
27+
2028
#### Training
2129
``` objective-c
2230
KRFuzzyCMeans *_krFcm = [KRFuzzyCMeans sharedFCM];
2331
_krFcm.doneThenSave = YES;
2432
_krFcm.m = 3;
2533
_krFcm.convergenceError = 0.001f;
26-
_krFcm.distanceFormula = KRFuzzyCMeansDistanceFormulaByEuclidean; //KRFuzzyCMeansDistanceFormulaByCosine
34+
_krFcm.distanceFormula = KRFuzzyCMeansDistanceFormulaEuclidean; //KRFuzzyCMeansDistanceFormulaCosine
2735
[_krFcm addCenters:@[@5.0f, @5.0f]]; //The center 1, cluster 1 start in here
2836
[_krFcm addCenters:@[@10.0f, @10.0f]]; //The center 2, cluster 2 start in here
2937
[_krFcm addCenters:@[@12.0f, @14.0f]]; //The center 3, cluster 3 start in here
@@ -66,7 +74,7 @@ _krFcm.distanceFormula = KRFuzzyCMeansDistanceFormulaByEuclidean; //KRFuzzyCMea
6674

6775
## Version
6876

69-
V1.3
77+
V1.4
7078

7179
## License
7280

0 commit comments

Comments
 (0)