分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
从kmeans各个样本所属类别 的非此即彼(要么是0要么是1,如果建立一个归属矩阵N∗k的更新
t2 = (1./dist).^(1/(b-1));P = t2./(sum(t2, 2)*ones(1, k));
- 1
- 2
- 目标函数的计算
J_cur = sum(sum((P.^b).*dist))/N;
- 1
算法的实现(matlab)
客户端程序
clear all; close all;I = imread('./lena.bmp');rng('default');[m, n, p] = size(I);X = reshape(double(I), m*n, p);k = 5; b = 2;[C, dist, J] = fcm(X, k, b);[~, label] = min(dist, [], 2);figureimshow(uint8(reshape(C(label, :), m, n, p)))figureplot(1:length(J), J, 'r-*'), xlabel('#iterations'), ylabel('objective function')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
FCM函数
function [C, dist, J] = fcm(X, k, b)iter = 0;[N, p] = size(X);P = randn(N, k);P = P./(sum(P, 2)*ones(1, k));J_prev = inf; J = [];while true, iter = iter + 1; t = P.^b; C = (X'*t)'./(sum(t)'*ones(1, p)); dist = sum(X.*X, 2)*ones(1, k) + (sum(C.*C, 2)*ones(1, N))'-2*X*C'; t2 = (1./dist).^(1/(b-1)); P = t2./(sum(t2, 2)*ones(1, k)); J_cur = sum(sum((P.^b).*dist))/N; J = [J J_cur]; if norm(J_cur-J_prev, 'fro') < 1e-3, break; end display(sprintf('#iteration: %03d, objective function: %f', iter, J_cur)); J_prev = J_cur; end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
效果演示
敛散性分析
聚类分割效果图
避免出现局部极小值的方法
一个比较简单也易实现的方法(比较low),就是多做几次试验。
times = 15;k = 5; b = 2;[C_prev, dist_prev, J_prev] = fcm(X, k, b);for i = 2:times, [C_cur, dist_cur, J_cur] = fcm(X, k, b); if (J_cur(end) < J_prev(end)), J_prev = J_cur; C_prev = C_cur; dist_prev = dist_cur; endend[~, label] = min(dist_prev, [], 2);imshow(uint8(reshape(C_prev(label, :), m, n, p)), [])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13