KRKmeans 2.6.1

KRKmeans 2.6.1

测试已测试
语言语言 Objective-CObjective C
许可证 MIT
发布最新版本2016年6月

Kalvar Lin 维护。



KRKmeans 2.6.1

ios-KRKmeans-Algorithm

KRKmeans 实现了 K-Means 聚类算法(クラスタリング分類),并在该项目中实现了多维聚类。KRKmeans 可以用于数据挖掘、图像压缩和分类。

Podfile

platform :ios, '7.0'
pod "KRKmeans", "~> 2.6.1"

如何使用

导入

#import "KRKmeans.h"

距离方法

"KRKmeansKernelCosine" is Cosine Similarity.
"KRKmeansKernelEuclidean" is Euclidean.
"KRKmeansKernelRBF" is Radial Basis Function.

选择组的中心

// 1. Random choosing the centers of groups from patterns.
[kmeans randomChooseCenters:3];

// 2. Quickly customizing the groups and centers
[kmeans addGroupForCenterFeatures:@[@2, @2] centerId:@"Center_1" groupId:@"Group_1"];
[kmeans addGroupForCenterFeatures:@[@6, @5] centerId:@"Center_2" groupId:@"Group_2"];
[kmeans addGroupForCenterFeatures:@[@3, @17] centerId:@"Center_3" groupId:@"Group_3"];

一维聚类

一维聚类。

-(void)oneDemensionalClustering
{
    //One dimensional K-Means, the data set is any number means.
    KRKmeansOne *kmeansOne = [KRKmeansOne sharedKmeans];
    kmeansOne.sources      = @[@0.33, @0.88, @1, @0.52, @146, @120, @45, @43, @0.4];

    //If you wanna customize the median value
    //kmeansOne.customMedian = 45.0f;

    [kmeansOne clusteringWithCompletion:^(BOOL success, float knowledgeLine, NSArray *maxClusters, NSArray *minClusters, NSArray *midClusters, NSDictionary *overlappings)
    {
        NSLog(@"knowledgeLine : %f", knowledgeLine);
        NSLog(@"maxClusters : %@", maxClusters);
        NSLog(@"minClusters : %@", minClusters);
        NSLog(@"midClusters : %@", midClusters);
        NSLog(@"overlappings : %@", overlappings);
        //[_krKmeansOne printResults];
    }];
}

多维聚类

-(void)multiClustering
{
    KRKmeans *kmeans     = [KRKmeans sharedKmeans];
    kmeans.modelKey      = @"MyKmeans1";
    kmeans.saveAfterDone = YES;
    kmeans.maxIteration  = 10;

    // Adding patterns
    NSArray *patterns = @[@[@5, @4], @[@3, @4], @[@2, @5], @[@9, @8], @[@3, @20],
                          @[@1, @1], @[@1, @2], @[@2, @2], @[@3, @2], @[@3, @1],
                          @[@6, @4], @[@7, @6], @[@5, @6], @[@6, @5], @[@7, @8],
                          @[@3, @12], @[@5, @20]];
    NSInteger index   = -1;
    for( NSArray *features in patterns )
    {
        index += 1;
        NSString *patternId      = [NSString stringWithFormat:@"Training_%li", index];
        KRKmeansPattern *pattern = [kmeans createPatternWithFeatures:features patternId:patternId];
        [kmeans addPattern:pattern];
    }

    [kmeans randomChooseCenters:3];
    [kmeans setupKernel:KRKmeansKernelEuclidean];
    [kmeans clusteringWithCompletion:^(BOOL success, KRKmeans *kmeansObject, NSInteger totalTimes) {
        NSLog(@"totalTimes : %li", totalTimes);
        NSLog(@"featuresOfCenters : %@", kmeansObject.featuresOfCenters);
        NSLog(@"centers objects: %@", kmeansObject.centers);
        NSLog(@"SSE : %lf", kmeansObject.sse);
    } perIteration:^(NSInteger times, KRKmeans *kmeansObject, BOOL *pause) {
        NSLog(@"times : %li", times);
        // If you want to direct pause that next iteration running, then you could set :
        //*pause = YES;
    }];

}

预测 & 恢复

要将训练好的模型恢复为可预测模式。

// Recovering the tranined groups to predicate patterns.
-(void)predicatingByTrainedModel
{
    KRKmeans *kmeans = [KRKmeans sharedKmeans];
    [kmeans recoverGroupsForKey:@"MyKmeans1"];

    NSMutableArray *samples = [NSMutableArray new];
    NSArray *patterns       = @[@[@21, @12], @[@13, @21], @[@12, @5], @[@3, @8]];
    NSInteger index         = -1;
    for( NSArray *features in patterns )
    {
        index += 1;
        NSString *patternId      = [NSString stringWithFormat:@"Predication_%li", index];
        KRKmeansPattern *pattern = [kmeans createPatternWithFeatures:features patternId:patternId];
        [samples addObject:pattern];
    }

    [kmeans predicatePatterns:samples completion:^(BOOL success, KRKmeans *kmeansObject, NSInteger totalTimes) {
        NSLog(@"\n\n====================== Predication ===========================\n\n");
        [kmeansObject printResults];
    }];

}

版本

V2.6.1

许可证

MIT.