Programming Assignment的心得(一)
Linear Regression and Logistic Regression
主要代码
class linear_regression:
def __init__(self):
self.mse_func = lambda x,y: 1/x.shape[0] * np.sum((x-y)**2)
pass
def train(self,x_train, y_train):
"""Receive the input training data, then learn the model.
Inputs:
x_train: np.array, shape (num_samples, num_features)
y_train: np.array, shape (num_samples, )
Outputs:
None
"""
self.learning_rate = 0.0001
self.w = np.random.rand(10)
iteration = 10000
for i in range(iteration):
pred = self.predict(x_train)
"""
Please Fill Your Code Here.
"""
gradient = 2 * 1/x_train.shape[0] * np.dot(x_train.T, (pred - y_train))
self.w = self.w - self.learning_rate * gradient
if i % 100 == 0:
print("Iteration {}/{}, MSE loss {:.4f}".format(i+1, iteration, self.mse_func(pred, y_train)))
return
def predict(self, x_test):
"""Do prediction via the learned model.
Inputs:
x_test: np.array, shape (num_samples, num_features)
Outputs:
pred: np.array, shape (num_samples, )
"""
"""
Please Fill Your Code Here.
"""
pred = np.dot(x_test, self.w)
return pred
缩进问题
问题:spyder打开作业的py文件后,自己写的代码缩进无法一致
解决:
- 进入tools-preference
- 查看editor的display,选择第二项show blank spaces,发现源代码的空格显示为 → \rightarrow →,而自己的代码空格处显示为 ⋯ \cdots ⋯
- 选择editor的advanced settings 将indentation characters从4 spaces改为tabulations,同时勾选下面的tab always indent
矩阵形式写梯度
Theory:
for the ordinary least square problem:
J ( θ ) = 1 2 ∑ i = 1 m ( h ( x ( i ) ) − y ( i ) ) 2 = 1 2 ∑ i = 1 m ( θ T x ( i ) − y ( i ) ) 2 \begin{aligned} J(\theta)=\frac{1}{2} \sum_{i=1}^{m}\left(h\left(x^{(i)}\right)-y^{(i)}\right)^{2}=& \frac{1}{2} \sum_{i=1}^{m}\left(\theta^{T} x^{(i)}-y^{(i)}\right)^{2} \end{aligned} J(θ)=21i=1∑m(h(x(i))−y(i))2=21i=1∑m(