Python 梯度下降实现案例(求空间内一点到其它所有点距离之和最小 )

本文通过Python实现梯度下降法和随机梯度下降法找到二维单位平方内2000个随机点的质心,即到所有点距离之和最小的点。梯度下降法从(0,1)出发,经过迭代找到质心(0.506, 0.495),而随机梯度下降法因每次迭代选取随机点导致路径曲折,最终得到质心(0.516, 0.489)。" 10822901,1400872,MFC图形菜单栏图标设置方法,"['MFC图形界面', '菜单栏', '按钮', '图标', 'CBCGPToolBar']
摘要由CSDN通过智能技术生成

题目描述:

1.Generate n = 2,000 points uniformly at random in the two-dimensional unit square.Which point do you expect the centroid to be?

2.What objective does the centroid of the points optimize?

3.Apply gradient descent to find the centroid.

4.Apply stochastic gradient descent to find the centroid. Can you say in simple words,what the algorithm is doing?


解析:

1.Generate n = 2,000 points uniformly at random in the two-dimensional unit square. Which point do you expect the centroid to be? 

首先生成一个横纵坐标都在01范围内的2000个随机散点图。要求的质心就是找到到所有点距离之和最小的点。由于点的生成都是随机的,所以期望的点是 : (0.50.5) 。生成散点图代码和图如下:


from scipy import *
import pylab as pl #pylab 模块是一款由python提供的可以绘制二维,三维数据的工具模块,其中包括了绘图软件包 matplotlib

all_points = rand(2000, 2)
pl.plot(all_points[:, 0], all_points[:, 1], 'g.')
pl.show()


2.What objective does the centroid of the points optimize? </

  • 9
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值