日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ML之Kmeans:利用自定义Kmeans函数实现对多个坐标点(自定义四个点)进行自动(最多迭代10次)分类

發(fā)布時(shí)間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ML之Kmeans:利用自定义Kmeans函数实现对多个坐标点(自定义四个点)进行自动(最多迭代10次)分类 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ML之Kmeans:利用自定義Kmeans函數(shù)實(shí)現(xiàn)對(duì)多個(gè)坐標(biāo)點(diǎn)(自定義四個(gè)點(diǎn))進(jìn)行自動(dòng)(最多迭代10次)分類(lèi)

?

?

目錄

輸出結(jié)果

核心代碼


?

?

輸出結(jié)果

?

?

?

核心代碼

#!/usr/bin/python # -*- coding:utf-8 -*-import numpy as np #ML之Kmeans:利用自定義Kmeans函數(shù)實(shí)現(xiàn)對(duì)多個(gè)坐標(biāo)點(diǎn)(自定義四個(gè)點(diǎn))進(jìn)行自動(dòng)(最多迭代10次)分類(lèi)def kmeans(X, k, maxIt): numPoints, numDim = X.shape dataSet = np.zeros((numPoints, numDim + 1)) dataSet[:, :-1] = X centroids = dataSet[np.random.randint(numPoints, size = k), :] #centroids = dataSet[0:2, :] #Randomly assign labels to initial centorid給初始中心隨機(jī)分配標(biāo)簽centroids[:, -1] = range(1, k +1) iterations = 0 oldCentroids = None # Run the main k-means algorithmwhile not shouldStop(oldCentroids, centroids, iterations, maxIt): print ("iteration: \n", iterations) print ("dataSet: \n", dataSet) print ("centroids: \n", centroids) # Save old centroids for convergence test. Book keeping.oldCentroids = np.copy(centroids) iterations += 1 # Assign labels to each datapoint based on centroidsupdateLabels(dataSet, centroids) # Assign centroids based on datapoint labelscentroids = getCentroids(dataSet, k) # We can get the labels too by calling getLabels(dataSet, centroids)return dataSet # Function: Should Stop # ------------- # Returns True or False if k-means is done. K-means terminates either # because it has run a maximum number of iterations OR the centroids # stop changing. def shouldStop(oldCentroids, centroids, iterations, maxIt): if iterations > maxIt: return Truereturn np.array_equal(oldCentroids, centroids) # Function: Get Labels # ------------- # Update a label for each piece of data in the dataset. def updateLabels(dataSet, centroids): # For each element in the dataset, chose the closest centroid. # Make that centroid the element's label.numPoints, numDim = dataSet.shape for i in range(0, numPoints): dataSet[i, -1] = getLabelFromClosestCentroid(dataSet[i, :-1], centroids) def getLabelFromClosestCentroid(dataSetRow, centroids): label = centroids[0, -1]; minDist = np.linalg.norm(dataSetRow - centroids[0, :-1]) for i in range(1 , centroids.shape[0]): dist = np.linalg.norm(dataSetRow - centroids[i, :-1])if dist < minDist: minDist = dist label = centroids[i, -1]print ("minDist:", minDist)return label# Function: Get Centroids # ------------- # Returns k random centroids, each of dimension n. def getCentroids(dataSet, k): # Each centroid is the geometric mean of the points that# have that centroid's label. Important: If a centroid is empty (no points have# that centroid's label) you should randomly re-initialize it.result = np.zeros((k, dataSet.shape[1])) for i in range(1, k + 1):oneCluster = dataSet[dataSet[:, -1] == i, :-1] result[i - 1, :-1] = np.mean(oneCluster, axis = 0) result[i - 1, -1] = i x1 = np.array([1, 1]) x2 = np.array([2, 1]) x3 = np.array([4, 3]) x4 = np.array([5, 4]) testX = np.vstack((x1, x2, x3, x4)) result = kmeans(testX, 2, 10) print ("final result:") print (result)

?

?


相關(guān)文章
ML之Kmeans:利用自定義Kmeans函數(shù)實(shí)現(xiàn)對(duì)多個(gè)坐標(biāo)點(diǎn)(自定義四個(gè)點(diǎn))進(jìn)行自動(dòng)(最多迭代10次)分類(lèi)

?

總結(jié)

以上是生活随笔為你收集整理的ML之Kmeans:利用自定义Kmeans函数实现对多个坐标点(自定义四个点)进行自动(最多迭代10次)分类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。