十大经典算法分别为:
- K-means K均值(无监督算法,聚类算法,随机算法)
- KNN(K Nearest Neighbor) K近邻(有监督算法,分类算法)
- 逻辑回归(分类算法)
- 决策树(有监督算法,概率算法)
- 随机森林(集成算法中最简单的,模型融合算法)
- 朴素贝叶斯
- EM算法
- Adaboost(集成算法之一)
- SVM
- 马尔可夫
一、算法过程
(1)随机选择k个初始聚类中心;
(2)计算其他样本点与聚类中心的距离;
(3)将最小距离的标签赋给当前样本;
(4)更新聚类中心。
二、基于K-means的图像分割
基于边用边学的思想,这里做了一个图像分割项目。
(1)距离公式:
其中i表示第i个样本,c表示第c个聚类中心;j表示rgb的第j个通道值。
(2)终止条件是达到迭代次数。
(3)跌倒100次后效果图:
三、Python实现
import numpy as np
import matplotlib.pyplot as plt
img = plt.imread('G:/Experiment/Machine Learning/boat.jpg')
img_row = img.shape[0]
img_col = img.shape[1]
def knn(img, iter, k):
img = img.reshape(-1,3) # 使二维空间,变成一维空间,避免后面计算距离时使用双层循环, 这样每一行代表不同空间的像素
img_new = np.column_stack((img, np.ones(img_row*img_col))) # 加一列
# (1) 随机选择k个像素作为初始聚类中心
cluster_orientation = np.random.choice(img_row*img_col, k, replace=False) # 产生k索引坐标,即k个中心的位置
cluster_center = img_new[cluster_orientation, :] # shape =(5,4)根据索引坐标,找到对应的聚类中心的rgb像素值
# 迭代
distance = [ [] for i in range(k)] # [ [], [], [], [], []]生成list,每个元素是一个列向量,该列向量保存的是所有像素距离中心j的距离
for i in range(iter):
# (2) 计算所有像素与聚类中心j的颜色距离
print("迭代次数:%d" % i)
for j in range(k):
distance[j] = np.sqrt(np.sum(np.square(img_new - cluster_center[j]), axis=1)) # data_new.shape = (269180,4),一行的和
# (3) 在当前像素与k个中心的颜色距离中,找到最小那个中心,更新图像所有像素label
# np.array(distance).shape = (5, 269180) ,返回一列中最小值对应的索引,范围是 [0, 4], 代表不同的label
orientation_min_dist = np.argmin(np.array(distance), axis=0) # np.array(distance).shape = (5, 269180) 一列中最小值
img_new[:, 3] = orientation_min_dist # shape = (269180, ), 将返回的索引列向量赋值给第4维,即保存label的第3列
# (4) 更新第j个聚类中心
for j in range(k):
# np.mean(r,g,b,label),属性和label都求个平均值
one_cluster = img_new[img_new[:, 3] == j] # 找到所有label为j的像素,其中img_new.shape = (269180,4)
cluster_center[j] = np.mean(one_cluster, axis=0) # 通过img_new[:, 3] == j找到所有label为j的行索引(?, 4),
# 求一列均值,这样mean_r ,mean_g_, mean_b, mean_label,一次循环得到(1,4)
return img_new
if __name__ == '__main__':
labels_vector = knn(img, 100, 5)
labels_img = labels_vector[:,3].reshape(img_row, img_col)
plt.imshow(labels_img)
plt.show()