close;clear;clc
%聚类已没问题
% x=[0 0;2 4;3 3;3 4;4 2;4 4;4 3;5 3;6 2;7 1;2 9;3 8;4 6;4 7;5 6;5 8;6 6;7 4;8 4;10 10]; %训练数据点
% group=[1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2]';
% load ex7data1.mat;
% load ex7data2.mat;
% x=X;
% x=imread('1.jpg');
% A=imread('dog.jpg');
A=imread('2.tif');
% imshow(a)
% for i=1:690
% for j=1:690
% x((i-1)*690+j,1:3)=A(i,j,:);
% end
% end
x=zeros(512*512,3);
x(:,1)=reshape(A(:,:,1),512*512,1);
x(:,2)=reshape(A(:,:,2),512*512,1);
x(:,3)=reshape(A(:,:,3),512*512,1);
% A(:,:,1)=200*ones(512,512);
% A2=A;
% imshow(A2)
K=600;%聚类的个数
sel = randperm(size(x, 1));
sel = sel(1:K);
c=x(sel, :); %随机初始化
% c=randn(2,2);
% c =[-0.8045 0.8351
% 0.6966 -0.2437];
% c=[4 4;7 7]
% c1=c(1,:);
% c2=c(2,:);
n=size(x,1);%个数
m=size(x,2);%特征维数
maxItea=3;
itea=1;
t=1;
J=zeros(maxItea-1,1);
while(itea<maxItea)
%%==计算距离
for i=1:K %2
for j=1:n %51
d(i,j)=norm(c(i,:)-x(j,:),2);
end
end
%%==排序 找出距离最小的索引值
[g index]=sort(d);
%%==按距离远近计算分类后的数据中心点
center=zeros(size(index,1),m);
for i=1:size(index,2)
center(index(1,i),:)=center(index(1,i),:)+x(i,:);
J(t,1)=J(t,1)+norm(x(i,:)-c(index(1,i),:));
J(t,1)=J(t,1)/n; %%代价函数
end
index=index(1,:);
%%==移动分类中心点
for i=1:K
if ~isempty(index(index==i))
c(i,:)=center(i,:)/length(index(index==i));
else
c(i,:)=randn(1,m);
end
end
itea=itea+1;t=t+1;
end
save('c.mat','c')
for i=1:K %2
for j=1:n %51
d(i,j)=norm(c(i,:)-x(j,:),2);
end
end
% %%====分类画图
[g index]=sort(d);
index=index(1,:);
for i=1:size(index,2)
xx(i,:)=c(index(1,i),:);
end
AA(:,:,1)=reshape(xx(:,1),512,512);
AA(:,:,2)=reshape(xx(:,2),512,512);
AA(:,:,3)=reshape(xx(:,3),512,512);
imshow(AA)
save('AA.mat','AA')
figure(2)
plot(1:size(J,1),J)
% figure(3)
% plot(X(:,1),X(:,2),'*')
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
matlab_ML.zip (20个子文件)
My_SVM
main1.m 1KB
svm_L.m 1KB
ex6data1.mat 981B
ex6data2.mat 7KB
sigmoid.m 137B
ex6data3.mat 6KB
sim.m 152B
My_Kmeans
1.jpg 5.23MB
2.tif 769KB
ex7data1.mat 995B
f2.m 2KB
f1.m 2KB
AA.mat 1.82MB
ex7data2.mat 5KB
c.mat 13KB
dog.jpg 40KB
My_PCA
displayData.m 2KB
f2.m 210B
ex7faces.mat 10.52MB
f1.m 604B
共 20 条
- 1
资源评论
工木勤勤
- 粉丝: 7
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功