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=1m(h(x(i))y(i))2=21i=1m(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值