迭代算法-梯度下降法Python理解
对theta1求偏导:
1 def partial_cost_theta1(theta0, theta1, x, y): 2 # Hypothesis 3 h = theta0 + theta1*x 4 # Hypothesis minus observed times x 5 diff = (h - y) * x 6 # Average to compute partial derivative 7 partial = diff.sum() / (x.shape[0]) 8 return partial 9 10 ============================= 11 partial = diff.sum() / (x.shape[0]) 12 这是梯度下降法中用python对theta0求偏导, 13 其中, 14 a)diff=(h-y)涉及到的参数x与y均是传入的数组类型,所以在python语法中可以用diff.sum()对这些值进行求和操作; 15 b)x.shape[0],表示求出这个列数组(矩阵)的维度数(n维1列).