kmeans很好的解释了机器学习的定义,一种非监督学习算法。经过几千条数据的锻炼,更趋于真是模型,虽然改造成半监督性后更精确,但也失去了它智能化、自动化的初衷。
kmeans算法实现:
1)从样本D中随机选取K个元素,作为K个簇的中心
2)分别计算剩下的元素到K个簇的距离,将这些元素归化到距离最短的簇
3)根据聚类结果,重新计算K个簇各自的中心,计算方法是取簇中所有元素各自维度的算术平均
4)将D中的元素按照新的中心重新聚类
5)重复第四步,直到中心不发生变化
6)将结果输出
这次所用的数据集如下,即模拟用户轨迹数据-各个时刻点的经纬度坐标(大概有900多条明细数据,下图为经过转json处理后)
java实现过程:
/**
* K-means 聚类算法
* @author ardo
*
*/
public class KmeansClustering {
public static String readFilePath = "D:\\ardo\\data\\latlng.txt";
private List<Point> dataset = null;
public KmeansClustering() throws IOException {
initDataSet(readFilePath);
}
/**
* 初始化数据集
*
* @throws IOException
*/
private void initDataSet(String filePath) throws IOException {
dataset = new ArrayList<Point>();
// BufferedReader bufferedReader = new BufferedReader(
// new InputStreamReader(KmeansClustering.class.getClassLoader()
// .getResourceAsStream(path)));
// String line = null;
// while ((line = bufferedReader.readLine()) != null) {
// String[] s = line.split("\t");
// Point point = new Point();
// point.setX(Double.parseDouble(s[0]));
// point.setY(Double.parseDouble(s[1]));
// point.setName(s[2]);
//
// }
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
String mrDetail[] = lineTxt.split("\\|");
Point point = new Point();
point.setName(mrDetail[0]);
point.setX(Double.parseDouble(mrDetail[16]));
point.setY(Double.parseDouble(mrDetail[17]));
dataset.add(point);
}
}
}
/**
* @param k
* 聚类的数目
*/
public Map<Point,List<Point>> kcluster(int k) {
// 随机从样本集合中选取k个样本点作为聚簇中心
// 每个聚簇中心有哪些点
Map<Point,List<Point>> nowClusterCenterMap = new HashMap<Point, List<Point>>();
for (int i = 0; i < k; i++) {
Random random = new Random();
int num = random.nextInt(dataset.size());
nowClusterCenterMap.put(dataset.get(num), new ArrayList<Point>());
}
//上一次的聚簇中心
Map<Point,List<Point>> lastClusterCenterMap = null;
// 找到离中心最近的点,然后加入以该中心为map键的list中
while (true) {
for (Point point : dataset) {
double shortest = Double.MAX_VALUE;
Point key = null;
for (Entry<Point, List<Point>> entry : nowClusterCenterMap.entrySet()) {
double distance = distance(point, entry.getKey());
if (distance < shortest) {
shortest = distance;
key = entry.getKey();
}
}
nowClusterCenterMap.get(key).add(point);
}
//如果结果与上一次相同,则整个过程结束
if (isEqualCenter(lastClusterCenterMap,nowClusterCenterMap)) {
break;
}
lastClusterCenterMap = nowClusterCenterMap;
nowClusterCenterMap = new HashMap<Point, List<Point>>();
//把中心点移到其所有成员的平均位置处,并构建新的聚簇中心
for (Entry<Point,List<Point>> entry : lastClusterCenterMap.entrySet()) {
nowClusterCenterMap.put(getNewCenterPoint(entry.getValue()), new ArrayList<Point>());
}
}
return nowClusterCenterMap;
}
/**
* 判断前后两次是否是相同的聚簇中心,若是则程序结束,否则继续,直到相同
* @param lastClusterCenterMap
* @param nowClusterCenterMap
* @return bool
*/
private boolean isEqualCenter(Map<Point, List<Point>> lastClusterCenterMap,
Map<Point, List<Point>> nowClusterCenterMap) {
if (lastClusterCenterMap == null) {
return false;
}else {
for (Entry<Point, List<Point>> entry : lastClusterCenterMap.entrySet()) {
if (!nowClusterCenterMap.containsKey(entry.getKey())) {
return false;
}
}
}
return true;
}
/**
* 计算新的中心
*
* @param value
* @return Point
*/
private Point getNewCenterPoint(List<Point> value) {
double sumX = 0.0, sumY = 0.0;
for (Point point : value) {
sumX += point.getX();
sumY += point.getY();
}
// System.out.println((int)sumX / value.size() + "===" + (int)sumY / value.size());
Point point = new Point();
point.setX(sumX / value.size());
point.setY(sumY / value.size());
return point;
}
/**
* 使用欧几里得算法计算两点之间距离
*
* @param point1
* @param point2
* @return 两点之间距离
*/
private double distance(Point point1, Point point2) {
double distance = Math.pow((point1.getX() - point2.getX()), 2)
+ Math.pow((point1.getY() - point2.getY()), 2);
distance = Math.sqrt(distance);
return distance;
}
public static void main(String[] args) throws IOException {
KmeansClustering kmeansClustering = new KmeansClustering();
Map<Point, List<Point>> result = kmeansClustering.kcluster(12);
for (Entry<Point, List<Point>> entry : result.entrySet()) {
System.out.println("===============聚簇中心为:" + entry.getKey() + "================");
// for (Point point : entry.getValue()) {
// System.out.println(point.getName());
// }
}
}
}
样本点之间的距离计算这里仍然采用欧几里得距离算法。900多条数据聚簇为11个中心点,当然我的程序中设置的为12,则跑出来的肯定不会大于这个数;算法不足之处:最终的结果与初始化K个中心值的选择有很大的关系,容易受噪音干扰。由此也可以看出kmeans更适合局部、量化的。程序的输出如下
拿到聚簇中心统计数据(坐标)后,我们开始绘制用户轨迹,如下(图中除起点和终点外中间蓝色圆圈的9个点为途经点):
如果大家对以上绘制地图线路API感兴趣,可以下载示例:
http://download.csdn.net/download/ardo_pass/10110459