RPCL(Rival Penalized Competitive Learning)在matlab下的实现
RPCL是在线kmeans的改进版,相当于半自动的选取出k个簇心:一开始设定N个簇心,N>k,然后聚类。每次迭代中把第二近的簇心甩开一段距离。
所谓在线kmeans是就是,每次仅仅用一个样本来更新簇心位置,而不是用全部数据(“批量”)。
代码:
% RPCL, rival penalized competitive learning,改进版的Kmeans
% 根据http://www.cs.hmc.edu/~asampson/ap/ap.git移植而来,并根据RPCL原文进行了修改
clear;
close all;
iter_num = 10; % 最多迭代次数
%load 'stimuli.txt';
%data = stimuli; clear stimuli;
%load 'dita.txt';
%data=dita; clear dita;
load 'data.txt';
sample_num = size(data, 1);
sample_dimension = size(data, 2);
learning_rate = 0.1;
K=20; %簇心数量
cluster_wins = zeros(K,1);
clusters = zeros(K, sample_dimension);
minVal = min(data); % 各维度计算最小值
maxVal = max(data); % 各维度计算最大值
% 初始化簇心。根据DLZhu的建议,要在数据区域外面生成簇心。
for i=1:K
%clusters(i,:) = [rand(), rand()]; % [0,1]x[0,1]平面上的散点
%clusters(i, :) = minVal + (maxVal - minVal) * rand();
clusters(i,:) = mypkg.genOutRand(minVal, maxVal);
cluster_wins(i) = 0;
end
% clusters = [
% [0.58900736, 0.8727472];
% [0.42491971, 0.31954921];
% [0.90917136, 0.70537815];
% [0.1836478, 0.2356091];
% [0.53538985, 0.35172357];
% [0.01189786, 0.98610582]
% ];
figure;
scatter(data(:,1), data(:,2));
hold on;
scatter(clusters(:,1), clusters(:,2), 'red', 'filled');
title('原始数据:蓝圈;初始簇心:红点');
c = zeros(sample_num, 1); % 每个样本数据所属簇心编号
cluster_colors = [
[0 0 1]; % blue
[1 0 0]; % red
[0 1 0]; % green
[1 0 1]; % magenta
[0 1 1]; % cyan
[1 1 0]; % yellow
];
for iter=1:iter_num
progress = iter / iter_num;
indexes = randperm(sample_num); % 样本顺序的一个随机排序
for h=1:sample_num
i=indexes(h);
% 遍历clusters,找到和data(j,:)最近的那个(winner),以及第二近的(rival)
gg = zeros(K,1);
for j=1:K
tt = norm(clusters(j,:) - data(i,:));
gg(j) = tt * cluster_wins(j);
end
% 找出winner簇心,它的gg(`)值最小
winner_idx = 1;
min_val = gg(1);
for j=1:K
if (gg(j)<min_val)
min_val = gg(j);
winner_idx = j;
end
end
% winner = clusters(winner_idx,:);
% 找出winner簇心,它的gg(`)值第二小
rival_idx = 1;
min_val = gg(1)+gg(2); %取前两元素的和,是为了避免gg(1)就是winner
for j=1:K
if (j==winner_idx)
continue;
end
if (gg(j)<min_val)
min_val = gg(j);
rival_idx = j;
end
end
% rival = clusters(rival_idx,:);
cluster_wins(winner_idx) = cluster_wins(winner_idx) + 1;
c(i) = winner_idx;
% 对于winner,learn
winner_learning_rate = learning_rate;
clusters(winner_idx,:) = clusters(winner_idx,:) + winner_learning_rate*(data(i,:)-clusters(winner_idx,:));
% 对于rival, de-learn
% learn(rival, stimulus, -0.8 * self.learning_rate)
rival_learning_rate = -0.8 * learning_rate;
clusters(rival_idx,:) = clusters(rival_idx,:) + rival_learning_rate*(data(i,:)-clusters(rival_idx,:));
end
%if(rem(iter,10)~=0)
% continue;
%end
huck = figure;
%for j=1:sample_num
% hold on;
% % plot(data(j,1), data(j,2), 'b--x', 'Color', cluster_colors(c(j),:), 'LineWidth', 2);
% plot([data(j,1), clusters(c(j),1)], [data(j,2), clusters(c(j),2)], 'Color', 'blue', 'Marker', 'o', 'MarkerFaceColor', cluster_colors(c(j),:), 'MarkerSize', 3);
%end
scatter(data(:,1), data(:,2));
hold on;
scatter(clusters(:,1), clusters(:,2), 'filled'); % 实心圆点,表示簇心初始位置
title(['第', num2str(iter), '次迭代']);
waitfor(huck);
end
disp('==========================');
final_c = unique(c);
final_clusters = clusters(final_c,:);
disp(final_clusters);
disp('Done.');
RPCL(Rival Penalized Competitive Learning)在matlab下的实现的更多相关文章
- matlab下K-means Cluster 算法实现
一.概念介绍 K-means算法是硬聚类算法,是典型的局域原型的目标函数聚类方法的代表,它是数据点到原型的某种距离作为优化的目标函数,利用函数求极值的方法得到迭代运算的调整规则.K-means算法以欧 ...
- 【转】 MATLAB下如何指定GPU资源
[转] MATLAB下如何指定GPU资源 原文链接
- Matlab下的文件执行路径
Matlab下有时命令出错,源于Command窗口的路径不正确.快捷键的执行会受此影响.
- Matlab下imwrite,Uint16的深度图像
Matlab下imwrite,Uint16的深度图像 1. 在Matlab命令窗口输入命令: help imwrite 会有如下解释: If the input array is of class u ...
- 关于libsvm工具箱在64位matlab下的安装说明
LIBSVM工具箱的安装 基本方法: 1.在网上下载LIBSVM工具箱. http://www.csie.ntu.edu.tw/~cjlin/libsvm/ 2.将LIBSVM工具箱所在目录添加到MA ...
- MATLAB下跑Faster-RCNN+ZF实验时如何编译自己需要的external文件
本篇文章主讲这篇博客中的(http://blog.csdn.net/sinat_30071459/article/details/50546891)的这个部分,如图所示 注:截图来自 小咸鱼_ 的博客 ...
- libSVM在matlab下的使用安装
1) 从LIBSVM的官网http://www.csie.ntu.edu.tw/~cjlin/libsvm/上下载最新版本的LIBSVM,当前版本为libsvm-3.18.zip 2) 解压压缩包到电 ...
- matlab下使用svmlib工具包
虽然网络上已经有了很多的类似的东西了吧.但是呢,我自己还是要写一写的. 安装: 对于 libsvm工具包,我们可以去官方网站下载,网址为:http://www.csie.ntu.edu.tw/~cjl ...
- matlab下的caffe接口配置(Windows)
本文基于大部分网上方法 http://blog.csdn.net/d5224/article/details/51916178,外加一点自己的个人实际配置经历,环境变量在配置后尽管显示正确并且重启多次 ...
随机推荐
- 带参数的CLR存储过程
昨天有学习<简单创建与布署CLR存储过程>http://www.cnblogs.com/insus/p/4371762.html,知道怎样创建以及布署至SQL中去. 下面这个范例是实现CL ...
- [转]一篇很全面的freemarker教程
copy自http://demojava.iteye.com/blog/800204 以下内容全部是网上收集: FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主 ...
- Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- angular animate
angular animate 有3种:1,css transition. 2,css keyframe. 3,javascript 用jquery的animate方法: 1,2 两种是纯粹css的, ...
- spring mvc4:异常处理
前面学习过struts2的异常处理,今天来看下spring mvc4的异常处理: 一.Servlet配置文件修改 <bean id="exceptionResolver" c ...
- 使用mysqldump进行mysql数据库备份还原
mysqldump是mysql自带的备份还原工具,默认在安装目录的bin下 可通过cmd命令行启动,然后运行: 还原一个数据库: mysql -h 主机 -u 用户名 -p密码 数据库名 < 指 ...
- Webwork 学习之路【04】Configuration 详解
Webwork做为经典的Web MVC 框架,个人觉得源码中配置文件这部分代码的实现十分考究. 支持自定义自己的配置文件.自定义配置文件读取类.自定义国际化支持. 可以作为参考,单独引入到其他项目中, ...
- 基于DDD的.NET开发框架 - ABP缓存Caching实现
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- 阿里云Ubuntu 14.04 + Nginx + let's encrypt 搭建https访问
参考页面: https://certbot.eff.org/#ubuntutrusty-nginx http://bbs.qcloud.com/thread-12059-1-1.html http:/ ...
- .net程序员转行做手游开发经历(三)
这次就主要讲讲我们开发的过程. 策划是我们团队的一个人成员专门负责,我们几个算是出谋划策.我这边的理解是,策划首先需要对所做的事情一定要有一定的把握,意思是尽可能的想到这件事情的影响范围,类似项目管理 ...