欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python實(shí)現(xiàn)kmean算法

 更新時(shí)間:2023年04月24日 10:49:39   作者:little shark  
這篇文章主要介紹了使用python實(shí)現(xiàn)kmean算法,kmean 是無(wú)監(jiān)督學(xué)習(xí)的一種算法,主要是用來(lái)進(jìn)行聚類分析的,他會(huì)在數(shù)據(jù)集中算出幾個(gè)點(diǎn)作為簇中心,求這些數(shù)據(jù)集與這些簇中心的距離,并將距離同一個(gè)簇中心距離最近的數(shù)據(jù)歸為一類,需要的朋友可以參考下

1. 簡(jiǎn)介

kmean 是無(wú)監(jiān)督學(xué)習(xí)的一種算法,主要是用來(lái)進(jìn)行聚類分析的,他會(huì)在數(shù)據(jù)集中算出幾個(gè)點(diǎn)作為簇中心,求這些數(shù)據(jù)集與這些簇中心的距離,并將距離同一個(gè)簇中心距離最近的數(shù)據(jù)歸為一類。因此,kmean最重要的地方便是關(guān)于簇中心的選擇。

他的算法流程簡(jiǎn)單總結(jié)如下

  • 簇個(gè)數(shù)的選擇;
  • 計(jì)算樣本到選取的簇中心距離,劃分樣本,將距離同一個(gè)簇中心最近的樣本歸為一類;
  • 設(shè)置一個(gè)迭代次數(shù),不斷更新簇中心;

2. kmean算法過(guò)程

kmean算法過(guò)程

這里初始簇中心的選擇并不是隨機(jī)選擇因?yàn)殡S機(jī)選擇的話,有的簇中心可能會(huì)重疊,這里改為了先選擇距離最遠(yuǎn)的兩個(gè)樣本作為兩個(gè)初始簇中心,下一個(gè)簇中心就從距離之前的簇中心距離最近的樣本中將距離最遠(yuǎn)的作為下一個(gè),如此反復(fù)直到滿足定義的k大小。同時(shí)圖中的新的簇中心的更新應(yīng)該是新老簇中心不相等時(shí)才更新才對(duì)。其實(shí)kmean的效果好壞很大的程度上取決于簇中心數(shù)量和初始簇中心的選擇上,并且他適合一些聚集成簇的樣本進(jìn)行劃分。

2.1 簇個(gè)數(shù)的選擇

  • 畫圖法: 最直接的辦法還是畫圖,從圖中直接看整個(gè)樣本大概分為幾大類,這種辦法適合低維(1-3維)的數(shù)據(jù),對(duì)于高維數(shù)據(jù)很難直接成圖,不過(guò)可以通過(guò)特征提取選擇能體現(xiàn)樣本差異的特征維(這里的差異很難定義,畢竟是無(wú)監(jiān)督學(xué)習(xí),可以簡(jiǎn)單認(rèn)為成圖后樣本聚集為幾類,這些類間間隔較遠(yuǎn)),或者直接降維為低維度數(shù)據(jù)成圖觀察。
  • 窮舉k法: 通過(guò)定義一系列不同的簇個(gè)數(shù),并通過(guò)聚類指標(biāo)評(píng)價(jià)這些不同簇個(gè)數(shù)的效果,選擇效果最好的那個(gè);

2.2 聚類評(píng)價(jià)指標(biāo)

還有更多的評(píng)價(jià)指標(biāo),這里我選擇了一些簡(jiǎn)單的,易于實(shí)現(xiàn)的。

2.2.1 輪廓系數(shù)

參考:https://baike.baidu.com/item/%E8%BD%AE%E5%BB%93%E7%B3%BB%E6%95%B0/17361607?fr=aladdin

輪廓系數(shù)

補(bǔ)充:如果Si接近0代表該樣本越可能在類的邊界上;越接近-1代表越可能屬于其他類;

2.2.2 緊密性指標(biāo)

緊密性指標(biāo)

這里指的是先計(jì)算同簇的樣本距離簇中心的平均距離CPi,再計(jì)算CPi的平均值;

2.2.3 間隔性指標(biāo)

間隔性指標(biāo)

3. 代碼

數(shù)據(jù)集:經(jīng)典的鳶尾花數(shù)據(jù)集,里面收錄了三個(gè)不同品種共四個(gè)不同性狀的數(shù)據(jù)。

#數(shù)據(jù)集
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
5.0,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa
4.7,3.2,1.6,0.2,Iris-setosa
4.8,3.1,1.6,0.2,Iris-setosa
5.4,3.4,1.5,0.4,Iris-setosa
5.2,4.1,1.5,0.1,Iris-setosa
5.5,4.2,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.0,3.2,1.2,0.2,Iris-setosa
5.5,3.5,1.3,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
4.4,3.0,1.3,0.2,Iris-setosa
5.1,3.4,1.5,0.2,Iris-setosa
5.0,3.5,1.3,0.3,Iris-setosa
4.5,2.3,1.3,0.3,Iris-setosa
4.4,3.2,1.3,0.2,Iris-setosa
5.0,3.5,1.6,0.6,Iris-setosa
5.1,3.8,1.9,0.4,Iris-setosa
4.8,3.0,1.4,0.3,Iris-setosa
5.1,3.8,1.6,0.2,Iris-setosa
4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
5.0,3.3,1.4,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor
6.9,3.1,4.9,1.5,Iris-versicolor
5.5,2.3,4.0,1.3,Iris-versicolor
6.5,2.8,4.6,1.5,Iris-versicolor
5.7,2.8,4.5,1.3,Iris-versicolor
6.3,3.3,4.7,1.6,Iris-versicolor
4.9,2.4,3.3,1.0,Iris-versicolor
6.6,2.9,4.6,1.3,Iris-versicolor
5.2,2.7,3.9,1.4,Iris-versicolor
5.0,2.0,3.5,1.0,Iris-versicolor
5.9,3.0,4.2,1.5,Iris-versicolor
6.0,2.2,4.0,1.0,Iris-versicolor
6.1,2.9,4.7,1.4,Iris-versicolor
5.6,2.9,3.6,1.3,Iris-versicolor
6.7,3.1,4.4,1.4,Iris-versicolor
5.6,3.0,4.5,1.5,Iris-versicolor
5.8,2.7,4.1,1.0,Iris-versicolor
6.2,2.2,4.5,1.5,Iris-versicolor
5.6,2.5,3.9,1.1,Iris-versicolor
5.9,3.2,4.8,1.8,Iris-versicolor
6.1,2.8,4.0,1.3,Iris-versicolor
6.3,2.5,4.9,1.5,Iris-versicolor
6.1,2.8,4.7,1.2,Iris-versicolor
6.4,2.9,4.3,1.3,Iris-versicolor
6.6,3.0,4.4,1.4,Iris-versicolor
6.8,2.8,4.8,1.4,Iris-versicolor
6.7,3.0,5.0,1.7,Iris-versicolor
6.0,2.9,4.5,1.5,Iris-versicolor
5.7,2.6,3.5,1.0,Iris-versicolor
5.5,2.4,3.8,1.1,Iris-versicolor
5.5,2.4,3.7,1.0,Iris-versicolor
5.8,2.7,3.9,1.2,Iris-versicolor
6.0,2.7,5.1,1.6,Iris-versicolor
5.4,3.0,4.5,1.5,Iris-versicolor
6.0,3.4,4.5,1.6,Iris-versicolor
6.7,3.1,4.7,1.5,Iris-versicolor
6.3,2.3,4.4,1.3,Iris-versicolor
5.6,3.0,4.1,1.3,Iris-versicolor
5.5,2.5,4.0,1.3,Iris-versicolor
5.5,2.6,4.4,1.2,Iris-versicolor
6.1,3.0,4.6,1.4,Iris-versicolor
5.8,2.6,4.0,1.2,Iris-versicolor
5.0,2.3,3.3,1.0,Iris-versicolor
5.6,2.7,4.2,1.3,Iris-versicolor
5.7,3.0,4.2,1.2,Iris-versicolor
5.7,2.9,4.2,1.3,Iris-versicolor
6.2,2.9,4.3,1.3,Iris-versicolor
5.1,2.5,3.0,1.1,Iris-versicolor
5.7,2.8,4.1,1.3,Iris-versicolor
6.3,3.3,6.0,2.5,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
7.1,3.0,5.9,2.1,Iris-virginica
6.3,2.9,5.6,1.8,Iris-virginica
6.5,3.0,5.8,2.2,Iris-virginica
7.6,3.0,6.6,2.1,Iris-virginica
4.9,2.5,4.5,1.7,Iris-virginica
7.3,2.9,6.3,1.8,Iris-virginica
6.7,2.5,5.8,1.8,Iris-virginica
7.2,3.6,6.1,2.5,Iris-virginica
6.5,3.2,5.1,2.0,Iris-virginica
6.4,2.7,5.3,1.9,Iris-virginica
6.8,3.0,5.5,2.1,Iris-virginica
5.7,2.5,5.0,2.0,Iris-virginica
5.8,2.8,5.1,2.4,Iris-virginica
6.4,3.2,5.3,2.3,Iris-virginica
6.5,3.0,5.5,1.8,Iris-virginica
7.7,3.8,6.7,2.2,Iris-virginica
7.7,2.6,6.9,2.3,Iris-virginica
6.0,2.2,5.0,1.5,Iris-virginica
6.9,3.2,5.7,2.3,Iris-virginica
5.6,2.8,4.9,2.0,Iris-virginica
7.7,2.8,6.7,2.0,Iris-virginica
6.3,2.7,4.9,1.8,Iris-virginica
6.7,3.3,5.7,2.1,Iris-virginica
7.2,3.2,6.0,1.8,Iris-virginica
6.2,2.8,4.8,1.8,Iris-virginica
6.1,3.0,4.9,1.8,Iris-virginica
6.4,2.8,5.6,2.1,Iris-virginica
7.2,3.0,5.8,1.6,Iris-virginica
7.4,2.8,6.1,1.9,Iris-virginica
7.9,3.8,6.4,2.0,Iris-virginica
6.4,2.8,5.6,2.2,Iris-virginica
6.3,2.8,5.1,1.5,Iris-virginica
6.1,2.6,5.6,1.4,Iris-virginica
7.7,3.0,6.1,2.3,Iris-virginica
6.3,3.4,5.6,2.4,Iris-virginica
6.4,3.1,5.5,1.8,Iris-virginica
6.0,3.0,4.8,1.8,Iris-virginica
6.9,3.1,5.4,2.1,Iris-virginica
6.7,3.1,5.6,2.4,Iris-virginica
6.9,3.1,5.1,2.3,Iris-virginica
5.8,2.7,5.1,1.9,Iris-virginica
6.8,3.2,5.9,2.3,Iris-virginica
6.7,3.3,5.7,2.5,Iris-virginica
6.7,3.0,5.2,2.3,Iris-virginica
6.3,2.5,5.0,1.9,Iris-virginica
6.5,3.0,5.2,2.0,Iris-virginica
6.2,3.4,5.4,2.3,Iris-virginica
5.9,3.0,5.1,1.8,Iris-virginica
#python3

#----------------------------
# author: little shark
# date: 2022/3/12
"""
Kmean 實(shí)現(xiàn)
"""

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import random

class Algo:
	def __init__(self):
		self.x = None
		self.y = None
		self.rows,self.cols = None, None
		self.dis = None
		self.dis_i = None
		self.rbf_i = None
		self.G_i = None
		self.GDis_i = True
		
	def loadData(self,file):
		"""
		Read file
		"""
		df=pd.read_csv(file,delimiter=",",header=None)
		self.x=df.iloc[:,0:-1].values
		self.y=df.iloc[:,-1].values
		
		self.rows,self.cols=self.x.shape
		#print(self.x.shape)
		
	def dataNorm(self):
		"""
		歸一化數(shù)據(jù)
		"""
		assert self.rows != None or self.cols !=None, "First you must loadData!!!"
		
		#get min matrix
		min_=self.x.min(axis=0).reshape(1,self.cols)
		
		#get max matrix
		max_=self.x.max(axis=0).reshape(1,self.cols)
		
		#get range matrix
		range_=max_-min_

		#update self.x
		self.x=(self.x-min_)/range_
	
	def euDistances(self,x):
		"""
		Get EuclideanDistances matrix
		"""
		assert self.rows != None or self.cols !=None, "First you must loadData!!!"
		
		#輸入矩陣大小
		rows,cols = x.shape
		
		#define a empty matrix
		dis=np.zeros(rows**2).reshape(rows,rows)
		
		#fill dis matrix by calculating distance of each point
		for i in range(rows):
			for j in range(rows):
				if i == j:
					dis[i][j] = 0
					continue
				d=sum(pow(x[i]-x[j],2))**0.5
				dis[i][j]=d
				dis[j][i]=d
		
		#get EuclideanDistances matrix
		self.dis=dis.copy()
		self.dis_i=True
		
		return dis
		
		
	def kmean(self,k=3,maxIteration=100,x=None,print_=False):
		"""
		kmean實(shí)體
		"""
		assert self.rows != None or self.cols !=None, "First you must loadData!!!"
		assert x.any() != None, "you must import a data to kmean"
		
		#輸入矩陣大小
		rows,cols = x.shape
		
		#定義一個(gè)標(biāo)簽列表,代表所有樣本的類別
		label=np.zeros(rows)
		labelCode=[i for i in range(k)]
		
		#計(jì)算輸入矩陣的距離矩陣
		dis=self.euDistances(x)
		
		#------------------------------------------
		#選擇初始簇中心,數(shù)量=k
		#最遠(yuǎn)的兩個(gè)點(diǎn)當(dāng)作簇中心
		max_=0
		maxI=0
		maxJ=0
		for i in range(rows):
			for j in range(rows):
				if dis[i][j] > max_:
					max_ = dis[i][j]
					maxI = i
					maxJ = j
		k1I = maxI
		k2I = maxJ
		k1=x[k1I].reshape(1,cols)
		k2=x[k2I].reshape(1,cols)
		
		Ci=[k1I,k2I] #保存簇中心索引
		C=[k1,k2] #保存簇中心值
		
		#step 3,檢索其他簇中心
		for i in range(2,k):
			tempI=[]
			temp=[]
			for j in range(len(C)):
				
				#step 3.1 距離簇中心最近的點(diǎn)
				minI=0
				min_=99999
				index=0
				for d in dis[Ci[j]]:
					#略過(guò)本身和已經(jīng)檢索過(guò)的點(diǎn)
					if d==0 or index in Ci:
						index+=1
						continue
					if d<min_:
						minI=index
						min_=d
					index+=1
				
				
				#step 3.2 添加距離大小及其索引
				temp.append(dis[Ci[j]][minI])
				tempI.append(minI)
				
			#step 3.3 選擇距離已選出的簇中心距離最近的點(diǎn)距離最大的作為下一個(gè)簇中心
			nextKI=tempI[temp.index(max(temp))]
			nextK=x[nextKI].reshape(1,cols)
			Ci.append(nextKI)
			C.append(nextK)
		
		#------------------------------------
		#更新label
		for i in range(rows):
			sample=x[i].reshape(1,cols)
			
			#判斷距離最近的簇中心
			minI=0
			min_=99999
			for j in range(k):
				disV=self.getDistance(sample,C[j])
				#print(dis)
				if disV<min_:
					minI=j
					min_=disV
			label[i]=labelCode[minI]
		
		#------------------------------------
		#更新簇中心
		for iter in range(maxIteration):
			for j in range(k):
				#獲取同類別下的數(shù)據(jù)
				dataSet=x[label == labelCode[j]]
				if len(dataSet) == 0:
					continue
				
				#計(jì)算平均值
				mean_=dataSet.mean(axis=0).reshape(1,cols)
				
				#更新簇中心
				C[j]=mean_
				
			#------------------------------------
			#更新label
			for i in range(rows):
				sample=x[i].reshape(1,cols)
				
				#判斷距離最近的簇中心
				minI=0
				min_=99999
				for j in range(k):
					disV=self.getDistance(sample,C[j])
					#print(dis)
					if disV<min_:
						minI=j
						min_=disV
				label[i]=labelCode[minI]
			
			cp = self.CP(label,C,x)
			sp = self.SP(C)
			conCoe = self.contourCoefficient(label,C,x)
			
			if print_:
				print("Iteration %-4d CP = %-7.4f CP = %-7.4f coef = %-7.4f"%(iter,cp,sp,conCoe))
			
		return label,C
	
	def getDistance(self,x1,x2):
		"""
		計(jì)算兩個(gè)向量的幾何距離
		"""
		return pow(((x1-x2)**2).sum(),0.5)
				
	def CP(self,label,c,x):
		"""
		緊密性評(píng)價(jià)指標(biāo)
		"""
		k = len(c)
		labelCode = np.unique(label)
		
		cp = 0
		for i in range(k):
			#同簇矩陣
			x1 = x[label == i]
			
			if len(x1) == 0:continue
			
			#大小
			rows, cols = x1.shape
			
			#距離簇中心的歐式距離和
			sum_ = pow(((x1 - c[i])**2).sum(),0.5)
			
			cp += sum_/rows
		
		return cp/k
		
	def SP(self,c):
		"""
		間隔性評(píng)價(jià)指標(biāo)
		"""
		k = len(c)
		
		sp = 0
		for i in range(k):
			for j in range(1,k):
				sp += self.getDistance(c[i],c[j])
		
		return 2*sp/(k**2-k)
		
	
	def calAi(self,v,vs):
		"""
		輪廓系數(shù)中計(jì)算ai
		v: 某向量
		vs: 該向量的同簇向量
		"""
		rows,cols = vs.shape
		ai = pow((vs - v)**2,0.5).sum()/rows
		
		return ai
		
	def calBi(self,v,vs,vsLabel):
		"""
		輪廓系數(shù)中計(jì)算bi
		v: 某向量
		vs: 該向量的非同簇向量
		vsCode: 該向量的非同簇向量的label
		"""
		ks = np.unique(vsLabel)
		k = len(ks)
		min_ = 99999
		
		for i in range(k):
			#非同簇中同簇向量
			x1 = vs[vsLabel == i]
			
			if len(x1) == 0:continue
			
			#非同簇中同簇平均距離
			disAver = self.calAi(v,x1)
			
			if disAver < min_:
				min_ = disAver
		
		return min_
		
	def contourCoefficient(self,label,c,x):
		"""
		輪廓系數(shù)
		"""
		k = len(c)
		rows,cols = x.shape
		S = np.zeros(rows)
		
		for i in range(rows):
			#同簇樣本
			x1 = x[label == label[i]]
			
			#非同簇樣本
			x2 = x[label != label[i]]
			label2 = label[label != label[i]]
			
			#當(dāng)前樣本
			sample = x[i]
			
			#ai ,bi 計(jì)算
			ai = self.calAi(sample,x1)
			bi = self.calBi(sample,x2,label2)
			
			s = (bi-ai)/max([ai,bi])
			
			S[i] = s
		
		return S.mean()
		
if __name__ == "__main__":
	#-------------------------
	#直接定義簇個(gè)數(shù)
	a=Algo()
	
	#加載數(shù)據(jù)
	a.loadData("數(shù)據(jù)集/iris (2).data")
	
	#運(yùn)行算法
	label,c = a.kmean(x=a.x,k=3)
	kCode = np.unique(label)
	
	print(label)
	
	#畫出數(shù)據(jù)劃分類
	for k in kCode:
		plt.scatter(a.x[label == k][:,0],a.x[label == k][:,2],alpha=0.6,label="sample %d"%k)
	
	#畫出簇中心
	for i in range(len(c)):
		plt.scatter(x=c[i][:,0],y=c[i][:,2],color="red",marker="*",label="center %d"%i)
	plt.legend()
	plt.show()
	
	
	#----------------------------
	#窮舉k法
	#exit()
	coef = []
	CP = []
	SP = []
	for k in range(2,10):
		label,c = a.kmean(x=a.x,k=k)
		coef.append(a.contourCoefficient(label,c,a.x))
		CP.append(a.SP(c))
		SP.append(a.CP(label,c,a.x))
		print("* When k = %d done *"%k)
	
	plt.subplot(131)
	plt.plot(range(2,10),SP,alpha=0.8,linestyle="--",color="red",label="SP")
	plt.xlabel("K")
	plt.legend()
	
	plt.subplot(132)
	plt.plot(range(2,10),coef,alpha=0.8,linestyle=":",color="green",label="coef")
	plt.xlabel("K")
	plt.legend()
	
	plt.subplot(133)
	plt.plot(range(2,10),CP,alpha=0.8,linestyle="-",color="blue",label="CP")
	plt.xlabel("K")
	plt.legend()
	plt.show()

4. 輸出結(jié)果

4.1 命令行輸出

命令行輸出

解釋:首先輸出的是所有樣本的聚類劃分,這里并沒有打亂樣本的順序進(jìn)行分析,所以同品種的樣本是連續(xù)的一大塊,大致來(lái)看直接定義k=3的劃分效果還不錯(cuò)。后面的輸出是窮舉k法,并計(jì)算對(duì)應(yīng)的評(píng)價(jià)指標(biāo)進(jìn)程。

4.2 可視化

在這里插入圖片描述

解釋:不同顏色代表不同類別的數(shù)據(jù),紅色星星代表簇中心,這里數(shù)據(jù)的維度展示的是第1列和第3列維度。

4.3 窮舉k法指標(biāo)

在這里插入圖片描述

解釋:第一個(gè)圖是間隔性指標(biāo)SP用來(lái)衡量聚類中心兩兩之間的平均距離,隨著k值增加,有比較明顯的波動(dòng),可以看見當(dāng)k=3時(shí)有一個(gè)最小值,表明k=3時(shí)類間的聚類最近,理想情況下我們希望這個(gè)這個(gè)指標(biāo)比較大,表明類別區(qū)分明顯;第二個(gè)是輪廓系數(shù),隨著k值增加逐漸下降;第三個(gè)是緊密性指標(biāo),用來(lái)衡量?jī)?nèi)中數(shù)據(jù)距離簇中心距離,隨著k值增加呈現(xiàn)一點(diǎn)的波動(dòng);從圖中來(lái)看任意一個(gè)指標(biāo)都不能準(zhǔn)確的表明k=3(真實(shí)的簇?cái)?shù))是一個(gè)好的簇?cái)?shù)量,也許這些指標(biāo)并不適合用來(lái)尋找定義的簇?cái)?shù)量。

單從輪廓系數(shù)來(lái)看,在知道k=3的情況下,越接近1代表聚類越合理,顯然是不現(xiàn)實(shí)的,且從公式上來(lái)說(shuō)當(dāng)k=1時(shí),輪廓系數(shù)會(huì)非常接近1,難道說(shuō)“大家都是一家人,四海皆兄弟”嗎?不太可能吧,且k=2的時(shí)候系數(shù)為0.75,難道劃分為2更好嗎?顯然在知道真實(shí)分類情況下也不適合,那么我們統(tǒng)計(jì)分析的結(jié)果真的就和真實(shí)一樣嗎?

首先從kmean上來(lái)說(shuō),他是無(wú)監(jiān)督的算法,這些數(shù)據(jù)是沒有先驗(yàn)的標(biāo)簽的,他是用來(lái)衡量數(shù)據(jù)相似情況,并把相似度較高的數(shù)據(jù)歸為一類,體現(xiàn)的是”同種中屬性更接近的思想“,可是他受限于初始k的選擇上;再?gòu)姆诸愔笜?biāo)上來(lái)看,這些指標(biāo)主要是用來(lái)衡量簇內(nèi)、簇間劃分程度,如果k越大,越多的樣本可能”各自為政“,那么他們間的SP、CP和輪廓系數(shù)可知是越來(lái)越小,從這些指標(biāo)代表的意義來(lái)說(shuō)并不能準(zhǔn)確的反映簇個(gè)數(shù)。

再者,人類認(rèn)知上存在缺陷,我們認(rèn)知我們所認(rèn)知的,我們并不能直觀的認(rèn)知到客觀規(guī)律,只能通過(guò)我們能感知到的一切來(lái)觀測(cè)進(jìn)而總結(jié)推理出客觀規(guī)律,我們目前的知識(shí)都是”前人拿著測(cè)量工具和放大鏡“一點(diǎn)點(diǎn)積累下來(lái)的,我們所思所想的都是實(shí)踐的產(chǎn)物,工具、思想都是人類活動(dòng)下的結(jié)晶,也就是說(shuō)存在不足,可是卻是我們能拿出手的”家當(dāng)“。

在這里我主要想說(shuō)的是在數(shù)據(jù)分析的時(shí)候不要太過(guò)迷信指標(biāo),特別是對(duì)一些不知真實(shí)分布的數(shù)據(jù)來(lái)做分析,如果這些指標(biāo)能準(zhǔn)確并一致的反映真實(shí)數(shù)據(jù),那我們?cè)缇头Q霸宇宙,預(yù)測(cè)未來(lái)了。

回到之前的話題:我們統(tǒng)計(jì)分析的結(jié)果真的就和真實(shí)一樣嗎? 就目前來(lái)說(shuō),這些測(cè)量和分析方法是我們能拿出來(lái)的,隨著人類的進(jìn)步這些東西要么進(jìn)化,要么淘汰。這些工具只是在一定程度上反映了真實(shí)而已,也許離得很近,也許大相徑庭…至于最終的結(jié)果在沒有真正認(rèn)知到客觀規(guī)律前,我們永遠(yuǎn)不知道,只是拿著我們?nèi)祟惢顒?dòng)的一系列產(chǎn)物來(lái)標(biāo)榜而已,也許未來(lái)人類回首現(xiàn)在,也會(huì)覺得有趣吧。

到此這篇關(guān)于使用python實(shí)現(xiàn)kmean算法的文章就介紹到這了,更多相關(guān)python實(shí)現(xiàn)kmean算法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)刪除重復(fù)文件的示例代碼

    Python實(shí)現(xiàn)刪除重復(fù)文件的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)刪除重復(fù)文件功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的小伙伴的可以了解一下
    2023-02-02
  • python序列類型種類詳解

    python序列類型種類詳解

    這篇文章主要介紹了python序列類型種類詳解,需要的朋友們可以學(xué)習(xí)參考下。
    2020-02-02
  • Python實(shí)現(xiàn)強(qiáng)制復(fù)制粘貼的示例詳解

    Python實(shí)現(xiàn)強(qiáng)制復(fù)制粘貼的示例詳解

    下個(gè)文檔還要馬內(nèi)?還好我會(huì)Python,本文就來(lái)教大家來(lái)一手如何利用Python實(shí)現(xiàn)強(qiáng)制復(fù)制粘貼。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-12-12
  • Keras 中Leaky ReLU等高級(jí)激活函數(shù)的用法

    Keras 中Leaky ReLU等高級(jí)激活函數(shù)的用法

    這篇文章主要介紹了Keras 中Leaky ReLU等高級(jí)激活函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Python優(yōu)雅實(shí)現(xiàn)二分查找的示例詳解

    Python優(yōu)雅實(shí)現(xiàn)二分查找的示例詳解

    二分查找是一種高效的搜索算法,用于在有序數(shù)組中查找特定元素,本文將介紹二分查找的基本原理,并通過(guò)Python代碼進(jìn)行詳細(xì)講解,需要的可以參考一下
    2023-07-07
  • redis之django-redis的簡(jiǎn)單緩存使用

    redis之django-redis的簡(jiǎn)單緩存使用

    本篇文章介紹了redis之django-redis的簡(jiǎn)單緩存使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Python基礎(chǔ)教程之內(nèi)置函數(shù)locals()和globals()用法分析

    Python基礎(chǔ)教程之內(nèi)置函數(shù)locals()和globals()用法分析

    這篇文章主要介紹了Python基礎(chǔ)教程之內(nèi)置函數(shù)locals()和globals()用法,結(jié)合實(shí)例形式分析了locals()和globals()函數(shù)的功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2018-03-03
  • python實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布游戲

    python實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布游戲

    這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布游戲,相信大家在童年或者生活中都玩過(guò)石頭剪刀布這個(gè)游戲,這個(gè)游戲需要兩個(gè)及以上的人。而今天,網(wǎng)上也實(shí)現(xiàn)了石頭剪刀布的游戲。通過(guò)初步學(xué)習(xí)python,也學(xué)會(huì)了如何編寫這個(gè)游戲。下面一起來(lái)看看詳細(xì)內(nèi)容吧
    2021-10-10
  • Python3訪問并下載網(wǎng)頁(yè)內(nèi)容的方法

    Python3訪問并下載網(wǎng)頁(yè)內(nèi)容的方法

    這篇文章主要介紹了Python3訪問并下載網(wǎng)頁(yè)內(nèi)容的方法,實(shí)例分析了Python頁(yè)面抓取及寫入文件的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • tensorboard 可視化之localhost:6006不顯示的解決方案

    tensorboard 可視化之localhost:6006不顯示的解決方案

    這篇文章主要介紹了tensorboard 可視化之localhost:6006不顯示的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評(píng)論