当前位置: 首页 > news >正文

论坛网站用的虚拟主机网易企业邮箱注册流程

论坛网站用的虚拟主机,网易企业邮箱注册流程,app代码开发流程图,泾川网站建设03、K-means聚类实现步骤与基于K-means聚类的图像压缩#xff08;1#xff09; 03、K-means聚类实现步骤与基于K-means聚类的图像压缩#xff08;1#xff09; 03、K-means聚类实现步骤与基于K-means聚类的图像压缩#xff08;2#xff09; 开始学习机器学习啦#xf…03、K-means聚类实现步骤与基于K-means聚类的图像压缩1 03、K-means聚类实现步骤与基于K-means聚类的图像压缩1 03、K-means聚类实现步骤与基于K-means聚类的图像压缩2 开始学习机器学习啦已经把吴恩达的课全部刷完了现在开始熟悉一下复现代码。对这个手写数字实部比较感兴趣作为入门的素材非常合适。 K-means聚类实现步骤 1、K-means基础 K-means算法是一种常用的聚类算法它的实现步骤如下 STEP1:从数据集中随机选择k个样本作为初始聚类中心。 STEP2:计算每个样本到各聚类中心的距离并将样本归入最近的聚类中心。 STEP3:重新计算每个聚类的中心该中心为该类所有样本的平均值。 STEP4:重复步骤2和3直到满足以下条件之一 聚类中心不再变化。 达到预设的最大迭代次数。 最小平方误差SSE误差的平方和达到预设的阈值。 2、K-means的底层代码实现 STEP0:调用numpy和绘图库 import numpy as np from matplotlib import pyplot as pltSTEP1:从数据集中随机选择k个样本作为初始聚类中心 # 随机初始化聚类初始优化点 def kMeans_init_centroids(X, K):# 随机重新排序样本的索引randidx np.random.permutation(X.shape[0])# 取前K个样本作为聚类中心centroids X[randidx[:K]]return centroidsSTEP2:计算每个样本到各聚类中心的距离并将样本归入最近的聚类中心 def find_closest_centroids(X, centroids):# 获取聚类中心的数量也即K值K centroids.shape[0]# 初始化一个数组用于存储每个样本所属的聚类中心的索引 idx np.zeros(X.shape[0], dtypeint)# 遍历数据集中的每个样本for i in range(X.shape[0]):# 初始化一个列表用于存储当前样本到每个聚类中心的距离distance []# 计算当前样本到每个聚类中心的距离for j in range(centroids.shape[0]):# 使用欧几里得距离公式计算样本i与聚类中心j之间的距离norm_ij np.linalg.norm(X[i] - centroids[j])distance.append(norm_ij)# 找出距离列表中的最小值该最小值对应的索引就是当前样本所属的聚类中心idx[i] np.argmin(distance)# 返回每个样本所属的聚类中心的索引数组return idxSTEP3:重新计算每个聚类的中心该中心为该类所有样本的平均值 def compute_centroids(X, idx, K):# 获取数据集X的行数m和列数n # m表示样本数量n表示每个样本的特征数量 m, n X.shape# 初始化一个K x n的零矩阵用于存储K个聚类中心 # K表示聚类数量n表示特征数量 centroids np.zeros((K, n))# 遍历每个聚类中心 for k in range(K):# 从数据集X中选择属于当前聚类k的所有样本 # idx是一个长度为m的数组存储了每个样本所属的聚类中心的索引 points X[idx k]# 计算属于当前聚类k的所有样本的平均值得到聚类中心 # axis0表示按列计算平均值 centroids[k] np.mean(points, axis0)# 返回计算得到的K个聚类中心 return centroidsSTEP4:重复步骤2和3直到满足以下条件之一 聚类中心不再变化。 达到预设的最大迭代次数。 最小平方误差SSE误差的平方和达到预设的阈值。 此处直接以达到预设的最大迭代次数作为停止条件 def run_kMeans(X, initial_centroids, max_iters10):# 获取数据集X的行数m和列数n# m表示样本数量n表示每个样本的特征数量m, n X.shape# 获取初始聚类中心的数量KK initial_centroids.shape[0]# 将初始聚类中心赋值给centroids变量centroids initial_centroids# 将初始聚类中心复制给previous_centroids变量用于后续比较聚类中心是否发生变化previous_centroids centroids# 初始化一个长度为m的零数组用于存储每个样本所属的聚类中心的索引idx np.zeros(m)# 开始运行K-means算法最多迭代max_iters次for i in range(max_iters):# 输出当前迭代进度print(K-Means iteration %d/%d % (i, max_iters - 1))# 调用find_closest_centroids函数为数据集X中的每个样本找到最近的聚类中心并返回索引数组idx find_closest_centroids(X, centroids)# 调用compute_centroids函数根据每个样本所属的聚类中心和索引数组计算新的聚类中心centroids compute_centroids(X, idx, K)# 返回最终的聚类中心和每个样本所属的聚类中心的索引return centroids, idx3、K-means的底层代码案例 此处直接使用吴恩达的案例非常简洁直观嘞 import numpy as np import matplotlib.pyplot as pltdef load_data():X np.load(K_means_data/ex7_X.npy)return Xdef draw_line(p1, p2, style-k, linewidth1):plt.plot([p1[0], p2[0]], [p1[1], p2[1]], style, linewidthlinewidth)def plot_data_points(X, idx):# plots data points in X, coloring them so that those with the same# index assignments in idx have the same colorplt.scatter(X[:, 0], X[:, 1], cidx)def plot_progress_kMeans(X, centroids, previous_centroids, idx, K, i):# Plot the examplesplot_data_points(X, idx)# Plot the centroids as black xsplt.scatter(centroids[:, 0], centroids[:, 1], markerx, ck, linewidths3)# Plot history of the centroids with linesfor j in range(centroids.shape[0]):draw_line(centroids[j, :], previous_centroids[j, :])plt.title(Iteration number %d % i)def find_closest_centroids(X, centroids):Computes the centroid memberships for every exampleArgs:X (ndarray): (m, n) Input valuescentroids (ndarray): k centroidsReturns:idx (array_like): (m,) closest centroids# Set KK centroids.shape[0]# You need to return the following variables correctlyidx np.zeros(X.shape[0], dtypeint)for i in range(X.shape[0]):# Array to hold distance between X[i] and each centroids[j]distance []for j in range(centroids.shape[0]):norm_ij np.linalg.norm(X[i] - centroids[j])distance.append(norm_ij)idx[i] np.argmin(distance)return idx# GRADED FUNCTION: compute_centpods def compute_centroids(X, idx, K):Returns the new centroids by computing the means of thedata points assigned to each centroid.Args:X (ndarray): (m, n) Data pointsidx (ndarray): (m,) Array containing index of closest centroid for eachexample in X. Concretely, idx[i] contains the index ofthe centroid closest to example iK (int): number of centroidsReturns:centroids (ndarray): (K, n) New centroids computed# Useful variablesm, n X.shape# You need to return the following variables correctlycentroids np.zeros((K, n))for k in range(K):points X[idx k]centroids[k] centroids[k] np.mean(points, axis0)return centroids# You do not need to implement anything for this part def run_kMeans(X, initial_centroids, max_iters10, plot_progressFalse):Runs the K-Means algorithm on data matrix X, where each row of Xis a single example# Initialize valuesm, n X.shapeK initial_centroids.shape[0]centroids initial_centroidsprevious_centroids centroidsidx np.zeros(m)# Run K-Meansfor i in range(max_iters):# Output progressprint(K-Means iteration %d/%d % (i, max_iters - 1))# For each example in X, assign it to the closest centroididx find_closest_centroids(X, centroids)# Optionally plot progressif plot_progress:plot_progress_kMeans(X, centroids, previous_centroids, idx, K, i)previous_centroids centroids# Given the memberships, compute new centroidscentroids compute_centroids(X, idx, K)plt.show()return centroids, idx# Load an example dataset X load_data() # Set initial centroids initial_centroids np.array([[3,3],[6,2],[8,5]]) K 3 # Number of iterations max_iters 10 centroids, idx run_kMeans(X, initial_centroids, max_iters, plot_progressTrue)运行结果
http://www.zqtcl.cn/news/902408/

相关文章:

  • 技术支持东莞网站建设机械seo岗位是什么意思
  • 做商城网站需要备案什么域名硬件开发工具有哪些
  • 网络网站制作技巧wordpress全文
  • 韩国原生ip站群服务器左右悬停代码网站
  • 专门做广东11选5的网站网站 备案 营业执照
  • 免费扑克网站wordpress弹出服务协议窗口
  • 网站的反爬一般怎样做网站右键屏蔽
  • 茂名做网站dyiee青岛宣传片制作公司
  • 凡科网可以自己做网站吗编程常用网站
  • 做网站练手项目公司营业执照可以做几个网站
  • 聚通达网站建设网站并发要求
  • 网站建设预算申请如何写服装店网页设计素材
  • 做网站设计的公司柳州芜湖又出现一例
  • 重庆网站网站建设东莞市网站建设公司哪家好
  • php做网站如何架构wordpress 排版
  • wordpress免费网站模板下载地址在北京注册公司需要多少钱
  • 做的网站打不开高端网站名字
  • 个人网站建设报告西安网站开发高端网站开发
  • “网站建设:上海珍岛”网站备案信息查询系统
  • 北京哪个公司做网站专业建站培训
  • 郑州知名网站推广网站管理设置
  • 建设工程网站资质人员查询常州模板网站建设价格
  • 自己建网站做app手机网站列表页源码
  • 企业网站模板seo网站建设关键词优化
  • 平面毕业设计作品网站推广普通话ppt
  • p2p网站开发思路方案免费建简单网站
  • 微信朋友圈的网站连接怎么做互联网工程有限公司
  • 高大上企业网站优秀的门户网站
  • 做seo对网站推广有什么作用自己做电商网站吗
  • 网站从哪些方面来做泉州网页搜索排名提升