#include "k_means.h"
double get_distance(double* src, double* dest, int dimension)
{
double dist = 0;
for(int i=0;i<dimension;i++)
{
dist += pow((dest[i]-src[i]),2.0);
}
dist = pow(dist,0.5);
return dist;
}
int check_equal(double* src, double* dest, int cnt)
{
for(int i=0;i<cnt;i++)
{
if(abs(src[i]-dest[i])>0.000001)return 0;
}
return 1;
}
int k_means(double* data, int dimension, int count, int k, double* avg,int* cluster)
{
if(count<k)return -1;
double* center = new double[dimension*k];
int* center_cnt = new int[k];
double* old_center = new double[dimension*k];
int* idx = new int[count];
memset(center,0,sizeof(double)*dimension*k);
memset(center_cnt,0,sizeof(int)*k);
memset(old_center,0,sizeof(double)*dimension*k);
memset(idx,0,sizeof(int)*k);
int step = count/k;
for(int i=0;i<k;i++)
{
double* dest = center+(i*dimension);
double* src = data+(i*step*dimension);
memcpy(dest, src, sizeof(double)*dimension);
}
do
{
memcpy(old_center, center, sizeof(double)*dimension*k);
memset(center,0,sizeof(double)*dimension*k);
memset(center_cnt,0,sizeof(int)*k);
for(int i=0;i<count;i++)
{
double min_dist = get_distance(data+(i*dimension), old_center, dimension);
int min_dist_idx = 0;
for(int j=1;j<k;j++)
{
double _dist = get_distance(data+(i*dimension),old_center+(j*dimension),dimension);
if(min_dist>_dist)
{
min_dist = _dist;
min_dist_idx = j;
}
}
idx[i] = min_dist_idx;
for(int j=0;j<dimension;j++)
{
center[idx[i]*dimension+j]+=data[i*dimension+j];
}
center_cnt[idx[i]]+=1;
}
for(int i=0;i<k;i++)
{
for(int j=0;j<dimension;j++)
{
center[i*dimension+j] = center[i*dimension+j]/center_cnt[i];
}
}
}while(!check_equal(old_center,center,k*dimension));
if(cluster!=NULL)memcpy(cluster, idx, sizeof(int)*count);
memcpy(avg, center, sizeof(double)*dimension*k);
delete idx;
delete old_center;
delete center_cnt;
delete center;
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
test_k_means.zip_K._k means 界面
共79个文件
h:43个
tlog:9个
dll:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 161 浏览量
2022-09-20
19:32:02
上传
评论
收藏 1.59MB ZIP 举报
温馨提示
K-means算法的c语言实现。 使用SDL库做了个界面,可以看到测试效果。
资源推荐
资源详情
资源评论
收起资源包目录
test_k_means.zip (79个子文件)
test_k_means.suo 30KB
Debug
SDL.dll 317KB
test_k_means.pdb 403KB
lazysnapping.ilk 476KB
lazysnapping.exe 100KB
lazysnapping.pdb 539KB
test_k_means.ilk 389KB
test_k_means.exe 34KB
test_k_means
test_k_means.cpp 2KB
test_k_means.vcxproj.filters 1KB
include
SDL_cdrom.h 6KB
SDL_config_nds.h 3KB
SDL_config_amiga.h 2KB
SDL_getenv.h 979B
SDL_copying.h 869B
SDL_active.h 2KB
SDL_quit.h 2KB
SDL_mouse.h 5KB
SDL_loadso.h 3KB
SDL_version.h 3KB
SDL_audio.h 11KB
begin_code.h 5KB
SDL_rwops.h 5KB
SDL_video.h 37KB
close_code.h 1KB
SDL_error.h 2KB
SDL_config_macos.h 3KB
SDL_config_macosx.h 4KB
SDL_cpuinfo.h 2KB
SDL_keysym.h 7KB
SDL_events.h 13KB
SDL_endian.h 6KB
SDL_config.h 1KB
SDL_config_dreamcast.h 3KB
SDL_mutex.h 6KB
SDL_stdinc.h 16KB
SDL_platform.h 3KB
SDL_config_os2.h 4KB
SDL.h 3KB
SDL_main.h 3KB
SDL_opengl.h 328KB
SDL_byteorder.h 986B
SDL_keyboard.h 4KB
SDL_thread.h 4KB
SDL_syswm.h 6KB
SDL_name.h 183B
SDL_joystick.h 5KB
SDL_config_minimal.h 2KB
SDL_types.h 979B
SDL_timer.h 4KB
SDL_config_symbian.h 3KB
SDL_config_win32.h 4KB
k_means.h 147B
lib
SDL.dll 317KB
SDLmain.lib 8KB
SDL.lib 42KB
k_means.cpp 2KB
Debug
vc100.idb 147KB
SDL.dll 317KB
CL.write.1.tlog 2KB
CL.read.1.tlog 10KB
mt.read.1.tlog 822B
test_k_means.log 3KB
test_k_means.Build.CppClean.log 2KB
mt.command.1.tlog 636B
cl.command.1.tlog 2KB
k_means.obj 11KB
link.write.1.tlog 1KB
test_k_means.obj 13KB
link.command.1.tlog 2KB
test_k_means.lastbuildstate 101B
link.read.1.tlog 4KB
mt.write.1.tlog 450B
test_k_means.exe.intermediate.manifest 618B
vc100.pdb 76KB
test_k_means.vcxproj.user 143B
test_k_means.vcxproj 4KB
test_k_means.sdf 2.77MB
test_k_means.sln 908B
共 79 条
- 1
资源评论
御道御小黑
- 粉丝: 58
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功