1. 程式人生 > >java實現K-means演算法,k-means聚類演算法原理

java實現K-means演算法,k-means聚類演算法原理

	/**
	 * 需要所有point 以及族中心list
	 * 
	 * @author:Yien
	 * @when:2018年5月20日下午3:14:09
	 * @Description:TODO
	 * @param:@param pointList
	 * @param:@param centerPointList
	 * @param:@return
	 * @return:Map<UserScorePoint,UserScorePoint>
	 * @throws:
	 */
	public static Map<UserScorePoint, UserScorePoint> 迭代函式(List<UserScorePoint> pointList,
			List<UserScorePoint> centerPointList) {

		/**
		 * 
		 * centerPointList可能為空;
		 */
		// System.out.println("三個新的族中心是:" + Arrays.toString(centerPointList.toArray()));

		Map<UserScorePoint, UserScorePoint> pointCenterMap = new HashMap<UserScorePoint, UserScorePoint>();
		/**
		 * 已經初始化了族心 就往Map自動新增進去
		 */
		if (!centerPointList.isEmpty()) {

			for (UserScorePoint point : centerPointList) {

				pointCenterMap.put(point, point);

			}
			/**
			 * 在列表中去掉族中心集合
			 */
			pointList.removeAll(centerPointList);
		}
		for (int i = centerPointList.size(); i < pointList.size(); ++i) {
			/**
			 * 新增族中心列表
			 */
			if (centerPointCount > i) {

				centerPointList.add(pointList.get(i));
				/**
				 * 族心自動新增到族
				 */
				pointCenterMap.put(pointList.get(i), pointList.get(i));
			}

			/**
			 * 繼續遍歷 找族中心;
			 */
			else {

				UserScorePoint centerPoint = findWhichPoint(pointList.get(i), centerPointList);
				// System.out.println("這裡是找到的族心"+centerPoint);

				/**
				 * 向Map新增族心資訊;
				 */
				pointCenterMap.put(pointList.get(i), centerPoint);
			}

		}

		return pointCenterMap;

	}