Skip to content

Commit 54dde19

Browse files
committed
Added Kernel of RBF.
1 parent d376140 commit 54dde19

File tree

7 files changed

+53
-21
lines changed

7 files changed

+53
-21
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.5.1"
3+
s.version = "2.5.2"
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/ViewController.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ -(void)twoDemensional
6161
_krKmeans.doneThenSave = YES;
6262

6363
// Set to use 2 points of Euclidean Distance method that performance is better
64-
_krKmeans.distanceFormula = KRKmeansDistanceFormulaByEuclidean; // KRKmeansDistanceFormulaByCosine
64+
_krKmeans.distanceFormula = KRKmeansDistanceFormulaEuclidean; // KRKmeansDistanceFormulaByCosine
6565

6666
// It means A sets. ( and the centers will be calculated here. )
6767
[_krKmeans addSets:@[@[@1, @1], @[@1, @2], @[@2, @2], @[@3, @2], @[@3, @1]]];
@@ -126,7 +126,7 @@ -(void)multiDemensional
126126
_multiKmeans.doneThenSave = YES;
127127

128128
// Suggests to use Cosine Similarity doing multi-dimensional clustering
129-
_multiKmeans.distanceFormula = KRKmeansDistanceFormulaByCosine; // KRKmeansDistanceFormulaByEuclidean
129+
_multiKmeans.distanceFormula = KRKmeansDistanceFormulaRBF; // KRKmeansDistanceFormulaCosine; // KRKmeansDistanceFormulaByEuclidean
130130

131131
// A sets
132132
[_multiKmeans addSets:@[@[@20, @9, @1, @3, @6, @2], @[@52, @32, @18, @7, @0, @1], @[@30, @18, @2, @27, @18, @5]]];
@@ -159,7 +159,7 @@ -(void)multiDemensional
159159
-(void)directClustering
160160
{
161161
KRKmeans *_kmeans = [KRKmeans sharedKmeans];
162-
_kmeans.distanceFormula = KRKmeansDistanceFormulaByEuclidean;
162+
_kmeans.distanceFormula = KRKmeansDistanceFormulaEuclidean;
163163
[_kmeans addPatterns:@[@[@7, @11], @[@18, @6]]];
164164
[_kmeans directClusterWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centers, NSInteger totalTimes)
165165
{
@@ -171,7 +171,7 @@ -(void)autoClustering
171171
{
172172
KRKmeans *_krKmeans = [[KRKmeans alloc] init];
173173
_krKmeans.doneThenSave = YES;
174-
_krKmeans.distanceFormula = KRKmeansDistanceFormulaByEuclidean; // KRKmeansDistanceFormulaByCosine
174+
_krKmeans.distanceFormula = KRKmeansDistanceFormulaEuclidean; // KRKmeansDistanceFormulaByCosine
175175
_krKmeans.autoClusterNumber = 3;
176176
[_krKmeans addPatterns:@[@[@1, @1], @[@1, @2], @[@2, @2], @[@3, @2],
177177
@[@3, @1], @[@5, @4], @[@3, @4], @[@2, @5],

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

ML/KRKmeans.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
typedef enum KRKmeansDistanceFormula
3838
{
3939
// Distance formula by Cosine Similarity
40-
KRKmeansDistanceFormulaByCosine = 0,
40+
KRKmeansDistanceFormulaCosine = 0,
4141
// Distance formula by Euclidean Distance
42-
KRKmeansDistanceFormulaByEuclidean = 1
42+
KRKmeansDistanceFormulaEuclidean = 1,
43+
// Distance formula by RBF
44+
KRKmeansDistanceFormulaRBF = 2
4345
}KRKmeansDistanceFormula;
4446

4547
/*
@@ -79,6 +81,8 @@ typedef void(^KRKmeansPerIteration)(NSInteger times, NSArray *clusters, NSArray
7981
@property (nonatomic, assign) BOOL doneThenSave;
8082
//Auto clustering numbers (要自動分成幾群)
8183
@property (nonatomic, assign) NSInteger autoClusterNumber;
84+
// If we used RBF be the kernel that can setup this considition
85+
@property (nonatomic, assign) float sigma;
8286

8387
@property (nonatomic, copy) KRKmeansClusteringCompletion clusterCompletion;
8488
@property (nonatomic, copy) KRKmeansPerIteration perIteration;

ML/KRKmeans.m

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ -(NSArray *)_useAverageVectorCalculateCenters:(NSArray *)_clusteredSets
8080
@implementation KRKmeans (fixDistances)
8181

8282
// Euclidean distance which multi-dimensional formula, 距離越小越近
83-
-(float)_distanceEuclideanX1:(NSArray *)_x1 x2:(NSArray *)_x2
83+
-(float)_euclideanX1:(NSArray *)_x1 x2:(NSArray *)_x2
8484
{
8585
NSInteger _index = 0;
8686
float _sum = 0.0f;
@@ -94,7 +94,7 @@ -(float)_distanceEuclideanX1:(NSArray *)_x1 x2:(NSArray *)_x2
9494
}
9595

9696
// Cosine Similarity method that multi-dimensional, 同歸屬度越大越近
97-
-(float)_distanceCosineSimilarityX1:(NSArray *)_x1 x2:(NSArray *)_x2
97+
-(float)_cosineSimilarityX1:(NSArray *)_x1 x2:(NSArray *)_x2
9898
{
9999
float _sumA = 0.0f;
100100
float _sumB = 0.0f;
@@ -114,17 +114,35 @@ -(float)_distanceCosineSimilarityX1:(NSArray *)_x1 x2:(NSArray *)_x2
114114
return ( _ab > 0.0f ) ? ( _sumAB / sqrtf( _ab ) ) : 0.0f;
115115
}
116116

117+
-(double)_rbf:(NSArray *)_x1 x2:(NSArray *)_x2
118+
{
119+
double _sum = 0.0f;
120+
NSInteger _index = 0;
121+
for( NSNumber *_value in _x1 )
122+
{
123+
// Formula : s = s + ( v1[i] - v2[i] )^2
124+
double _v = [_value doubleValue] - [[_x2 objectAtIndex:_index] doubleValue];
125+
_sum += ( _v * _v );
126+
++_index;
127+
}
128+
// Formula : exp^( -s / ( 2.0f * sigma * sigma ) )
129+
return pow(M_E, ((-_sum) / ( 2.0f * self.sigma * self.sigma )));
130+
}
131+
117132
// 距離概念是越小越近,歸屬度概念是越大越近 ( 或取其差值,使歸屬度同距離越小越近 )
118133
-(float)_distanceX1:(NSArray *)_x1 x2:(NSArray *)_x2
119134
{
120135
float _distance = 0.0f;
121136
switch (self.distanceFormula)
122137
{
123-
case KRKmeansDistanceFormulaByCosine:
124-
_distance = 1.0f - [self _distanceCosineSimilarityX1:_x1 x2:_x2];
138+
case KRKmeansDistanceFormulaCosine:
139+
_distance = 1.0f - [self _cosineSimilarityX1:_x1 x2:_x2];
125140
break;
126-
case KRKmeansDistanceFormulaByEuclidean:
127-
_distance = [self _distanceEuclideanX1:_x1 x2:_x2];
141+
case KRKmeansDistanceFormulaEuclidean:
142+
_distance = [self _euclideanX1:_x1 x2:_x2];
143+
break;
144+
case KRKmeansDistanceFormulaRBF:
145+
_distance = [self _rbf:_x1 x2:_x2];
128146
break;
129147
default:
130148
break;
@@ -286,8 +304,10 @@ -(instancetype)init
286304
_lastDistance = -1.0f;
287305
_currentIteration = 0;
288306

289-
_distanceFormula = KRKmeansDistanceFormulaByEuclidean;
307+
_distanceFormula = KRKmeansDistanceFormulaEuclidean;
290308
_trainedSaves = [KRKmeansSaves sharedInstance];
309+
310+
_sigma = 2.0f;
291311
}
292312
return self;
293313
}

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ KRKmeans has implemented K-Means the clustering algorithm (クラスタリング
77

88
```ruby
99
platform :ios, '7.0'
10-
pod "KRKmeans", "~> 2.5.1"
10+
pod "KRKmeans", "~> 2.5.2"
1111
```
1212

1313
## How to use
@@ -18,6 +18,14 @@ pod "KRKmeans", "~> 2.5.1"
1818
#import "KRKmeans.h"
1919
```
2020

21+
##### Distance Methods
22+
23+
``` objective-c
24+
KRKmeansDistanceFormulaEuclidean
25+
KRKmeansDistanceFormulaCosine
26+
KRKmeansDistanceFormulaRBF
27+
```
28+
2129
#### One dimensonal clustering
2230

2331
``` objective-c
@@ -71,7 +79,7 @@ pod "KRKmeans", "~> 2.5.1"
7179
KRKmeans *_krKmeans = [KRKmeans sharedKmeans];
7280

7381
// Set to Euclidean Distance method that performance is better
74-
_krKmeans.dimensional = KRKmeansDistanceFormulaByEuclidean;
82+
_krKmeans.dimensional = KRKmeansDistanceFormulaEuclidean;
7583

7684
//It means A sets. ( and the centers will be calculated here. )
7785
[_krKmeans addSets:@[@[@1, @1], @[@1, @2], @[@2, @2], @[@3, @2], @[@3, @1]]];
@@ -128,7 +136,7 @@ pod "KRKmeans", "~> 2.5.1"
128136
KRKmeans *_multiKmeans = [[KRKmeans alloc] init];
129137

130138
// Suggests to use Cosine Similarity doing multi-dimensional clustering
131-
_multiKmeans.dimensional = KRKmeansDimensionalMultiByCosine;
139+
_multiKmeans.dimensional = KRKmeansDistanceFormulaCosine;
132140

133141
// A sets
134142
[_multiKmeans addSets:@[@[@20, @9, @1, @3, @6, @2], @[@52, @32, @18, @7, @0, @1], @[@30, @18, @2, @27, @18, @5]]];
@@ -167,7 +175,7 @@ If you have trained clusters that you could directly put new patterns into direc
167175
-(void)directClustering
168176
{
169177
KRKmeans *_kmeans = [KRKmeans sharedKmeans];
170-
_kmeans.dimensional = KRKmeansDistanceFormulaByEuclidean;
178+
_kmeans.dimensional = KRKmeansDistanceFormulaEuclidean;
171179
[_kmeans addPatterns:@[@[@7, @11], @[@18, @6]]];
172180
[_kmeans directClusterWithCompletion:^(BOOL success, NSArray *clusters, NSArray *centers, NSInteger totalTimes)
173181
{
@@ -185,7 +193,7 @@ Automatic picking the group-centers by your wishes number.
185193
{
186194
KRKmeans *_krKmeans = [[KRKmeans alloc] init];
187195
_krKmeans.doneThenSave = YES;
188-
_krKmeans.distanceFormula = KRKmeansDistanceFormulaByEuclidean; // KRKmeansDistanceFormulaByCosine
196+
_krKmeans.distanceFormula = KRKmeansDistanceFormulaEuclidean; // KRKmeansDistanceFormulaCosine
189197
_krKmeans.autoClusterNumber = 3;
190198
[_krKmeans addPatterns:@[@[@1, @1], @[@1, @2], @[@2, @2], @[@3, @2],
191199
@[@3, @1], @[@5, @4], @[@3, @4], @[@2, @5],
@@ -220,7 +228,7 @@ Automatic picking the group-centers by your wishes number.
220228

221229
## Version
222230

223-
V2.5.1
231+
V2.5.2
224232

225233
## License
226234

0 commit comments

Comments
 (0)