资源简介
利用KMeans算法对点云数据进行分类,例如可以提取出点云数据里的房屋、道路等。
代码片段和文件信息
#Author ZTY
import numpy as np
def kmean(xkmaxtimes):
mn = np.shape(x)
# 0矩阵的格式np.zeros([mn])
dataset = np.zeros([mn+1])
dataset[::-1] = x
#middle = dataset[np.random.randint(msize=k):]
middle = dataset[0:3:]
middle[:-1] = range(1k+1)
times = 0
oldmiddle = None
while not shouldstop(oldmiddlemiddletimesmaxtimes):
print(‘times:‘times)
print(‘dataset:‘dataset)
print(‘middle:‘middle)
oldmiddle = np.copy(middle)
times = times + 1
update(datasetmiddle)
middle = getmiddles(datasetk)
return dataset
def shouldstop(oldmiddlemiddletimesmaxtimes):
if times > maxtimes:
return True
return np.array_equal(oldmiddlemiddle)
def update(datasetmiddle):
mn =dataset.shape
for i in range(0m):
dataset[i-1] = getLabelFromCloestCentroid(dataset[i:-1]middle)
def getLabelFromCloestCentroid(datasetRowmiddle):
label = middle[0-1]
minDist = np.linalg.norm(datasetRow - middle[0:-1])
#np.linalg.norm(a-b)用来计算ab两点之间的距离,a.b如果是list,必须要np.array(a)进行格式转换
for i in range(1middle.shape[0]):
dist = np.linalg.norm(datasetRow - middle[i:-1])
if dist < minDist:
minDist = dist
label = middle[i-1]
print(‘minDist‘minDist)
print(‘label‘label)
return label
def getmiddles(datatsetk):
result = np.zeros((kdatatset.shape[1]))
for i in range(1k+1):
- 上一篇:超限学习机—逻辑回归Python代码
- 下一篇:Python简易滚动抽奖界面程序
评论
共有 条评论