吴恩达Coursera, 机器学习专项课程, Machine Learning:Unsupervised Learning, Recommenders, Reinforcement Learning第一周编程作业1
吴恩达Coursera, 机器学习专项课程, Machine Learning:Unsupervised Learning, Recommenders, Reinforcement Learning第一周所有jupyter notebook文件1:
吴恩达Coursera, 机器学习专项课程, Machine Learning:Unsupervised Learning, Recommenders, Reinforcement Learning第一周所有jupyter notebook文件(包括实验室练习文件)1
本次作业
Exercise 1
# UNQ_C1 # GRADED FUNCTION: find_closest_centroids def find_closest_centroids(X, centroids): """ Computes the centroid memberships for every example Args: X (ndarray): (m, n) Input values centroids (ndarray): k centroids Returns: idx (array_like): (m,) closest centroids """ # Set K K = centroids.shape[0] # You need to return the following variables correctly idx = np.zeros(X.shape[0], dtype=int) ### START CODE HERE ### dis = [] for i in range(len(X)): for j in range(len(centroids)): dis.append(((X[i][0] - centroids[j][0]) **2 + (X[i][1] - centroids[j][1]) **2 ) ** (1/2)) idx[i] = np.argmin(dis) dis = [] ### END CODE HERE ### return idx
Exercise 2
# UNQ_C2 # GRADED FUNCTION: compute_centpods def compute_centroids(X, idx, K): """ Returns the new centroids by computing the means of the data points assigned to each centroid. Args: X (ndarray): (m, n) Data points idx (ndarray): (m,) Array containing index of closest centroid for each example in X. Concretely, idx[i] contains the index of the centroid closest to example i K (int): number of centroids Returns: centroids (ndarray): (K, n) New centroids computed """ # Useful variables m, n = X.shape # You need to return the following variables correctly centroids = np.zeros((K, n)) ### START CODE HERE ### for i in range(K): indices = np.where(idx == i) centroids[i, :] = (np.sum(X[indices, :], axis=1) / len(indices[0])).ravel() ### END CODE HERE ## return centroids
作者:楚千羽
出处:https://www.cnblogs.com/chuqianyu/
本文来自博客园,本文作者:楚千羽,转载请注明原文链接:https://www.cnblogs.com/chuqianyu/p/16947300.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~