day23--KMeans聚类算法(day56)

1.KMeans

KMeans聚类算法是将数据点划分为K个类簇,找到每个簇的中心并使其高度最小化
KMeans聚类算法的分析步骤

  1. 确定K值,即将数据集聚集成K个类簇
  2. 从数据集中随机选择K个数据点作为质心或数据中心
  3. 分别计算每个点到每个质心之间的距离,并将每个点划分到离最近质心的小组
  4. 当每个质心都聚集了一些点后,重新定义算法选出新的质心(对于每个簇,计算其平均值,即得到新的K个质心点)
  5. 循环执行到3,4步,直到分类结果不会改变

2.核心代码

	public void clustering() {
		//分簇的步骤,dataset.numInstances=150,和knn算法中值相同,使用的是同一个数据文件
		int[] tempOldClusterArray = new int[dataset.numInstances()];
		tempOldClusterArray[0] = -1;
		int[] tempClusterArray = new int[dataset.numInstances()];
		Arrays.fill(tempClusterArray, 0);
		double[][] tempCenters = new double[numClusters][dataset.numAttributes() - 1];

		// Step 1. Initialize centers.打乱150个数据
		int[] tempRandomOrders = getRandomIndices(dataset.numInstances());
		//然后选择K个元素
		for (int i = 0; i < numClusters; i++) {
			for (int j = 0; j < tempCenters[0].length; j++) {
				//拷贝数据
				tempCenters[i][j] = dataset.instance(tempRandomOrders[i]).value(j);
			} // Of for j
		} // Of for i

		int[] tempClusterLengths = null;
		while (!Arrays.equals(tempOldClusterArray, tempClusterArray)) {
			System.out.println("New loop ...");
			tempOldClusterArray = tempClusterArray;
			tempClusterArray = new int[dataset.numInstances()];

			// Step 2.1 Minimization. Assign cluster to each instance.
			int tempNearestCenter;
			double tempNearestDistance;
			double tempDistance;

			for (int i = 0; i < dataset.numInstances(); i++) {
				tempNearestCenter = -1;
				tempNearestDistance = Double.MAX_VALUE;

				for (int j = 0; j < numClusters; j++) {
					tempDistance = distance(i, tempCenters[j]);
					if (tempNearestDistance > tempDistance) {
						tempNearestDistance = tempDistance;
						tempNearestCenter = j;
					} // Of if
				} // Of for j
				//将每一簇分好下标
				tempClusterArray[i] = tempNearestCenter;
			} // Of for i

			// Step 2.2 Mean. Find new centers.
			tempClusterLengths = new int[numClusters];
			Arrays.fill(tempClusterLengths, 0);
			double[][] tempNewCenters = new double[numClusters][dataset.numAttributes() - 1];
			// Arrays.fill(tempNewCenters, 0);
			for (int i = 0; i < dataset.numInstances(); i++) {
				for (int j = 0; j < tempNewCenters[0].length; j++) {
					tempNewCenters[tempClusterArray[i]][j] += dataset.instance(i).value(j);
				} // Of for j
				tempClusterLengths[tempClusterArray[i]]++;
			} // Of for i

			// Step 2.3 Now average
			for (int i = 0; i < tempNewCenters.length; i++) {
				for (int j = 0; j < tempNewCenters[0].length; j++) {
					tempNewCenters[i][j] /= tempClusterLengths[i];
				} // Of for j
			} // Of for i

			System.out.println("Now the new centers are: " + Arrays.deepToString(tempNewCenters));
			tempCenters = tempNewCenters;
		} // Of while

		// Step 3. Form clusters.
		clusters = new int[numClusters][];
		int[] tempCounters = new int[numClusters];
		for (int i = 0; i < numClusters; i++) {
			clusters[i] = new int[tempClusterLengths[i]];
		} // Of for i
		

		for (int i = 0; i < tempClusterArray.length; i++) {
			clusters[tempClusterArray[i]][tempCounters[tempClusterArray[i]]] = i;
			tempCounters[tempClusterArray[i]]++;
		} // Of for i

		System.out.println("The clusters are: " + Arrays.deepToString(clusters));
	}// Of clustering
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
TF-IDF是一种常用的文本加权技术,用于评估一个词对于一个文件集或语料库中某一份文件的重要程度。它通过计算一个词在文件中出现的次数与在整个语料库中出现的频率的比例来确定词的重要性。具体而言,一个词在文章中出现次数越多,同时在所有文档中出现次数越少,就越能够代表该文章。 在进行文本聚类时,可以使用TF-IDF提取文本特征。首先,使用分词工具(例如jieba)对文本进行分词,然后使用停用词表删除常见词汇。接下来,计算每个词的TF-IDF值,并将其作为文本的特征。最后,可以使用KMeans算法进行聚类,将文本划分为不同的群组。 总结来说,tf-idf kmeans文本聚类的过程包括使用TF-IDF提取文本特征和使用KMeans算法进行聚类。通过TF-IDF可以计算每个词的重要性,然后将文本表示为特征向量,最后使用KMeans算法将文本聚类成不同的群组。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [基于TF-IDF+KMeans聚类算法构建中文文本分类模型(附案例实战)](https://blog.csdn.net/m0_64336780/article/details/129887890)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [tf-idf kmeans文本聚类](https://blog.csdn.net/be_humble/article/details/121234927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值