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,外加一点自己的个人实际配置经历,环境变量在配置后尽管显示正确并且重启多次 ...
随机推荐
- PAT 1006. 换个格式输出整数 (15)
让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例 ...
- python处理经过gzip压缩的网页内容
Python在进行网页抓取时,有时会获取到经过gzip压缩后的数据(体积小,传输快),导致无法阅读和使用. 如图所示,为http原始报文.可以看到,header区域的“Content-Encoding ...
- VFS分析(二)基本数据结构(持续更新)
nameidata /mnt/dir1/dir2/ nameidata结构体是一个临时的结构体, 目标是为了找到最后的dentry.
- Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- QT 数据库编程二
//logindlg.cpp #include "logindlg.h" #include <QGridLayout> #include <QHBoxLayout ...
- leetcode 315. Count of Smaller Numbers After Self 两种思路(欢迎探讨更优解法)
说来惭愧,已经四个月没有切 leetcode 上的题目了. 虽然工作中很少(几乎)没有用到什么高级算法,数据结构,但是我一直坚信 "任何语言都会过时,只有数据结构和算法才能永恒". ...
- spring boot 自动部署方案
现在主流的自动部署方案大都是基于Docker的了,但传统的自动部署方案比较适合中小型公司,下面的方案就是比较传统的自动部署方案. 1.为什么需要自动部署 基于微服务的架构,自动部署显得非常重要.因为每 ...
- 自己存档:table 的css
border:0;margin:0;border-collapse:collapse;border-spacing:0;
- fstab 中 通过UUID挂载 参数解释
UUID=cf474122-1d51-4953-846d-9ce1c8d23ae6 / ext4 defaults 1 1UUID=ef21d494-0dc7-41ec-95b2-a691bfd4e5 ...
- .Net相关
Lucene 全文搜索 http://lucenenet.apache.org/ Memcached 分布式缓存 http://memcached.org/ selenium UI自动化测试 http ...