> 2021年03月27日信息消化 ### 每天学点算法 #### Facebook题目| 简单正则匹配 实现正则表达式与以下特殊字符的匹配。 - `.`(句号),可匹配任何单个字符。 - `*`(星号),与前一要素中的零个或多个要素相匹配。 也就是说,实现一个函数,接收一个字符串和一个有效的正则表达式,并返回该字符串是否与正则表达式匹配。 例如,给定正则表达式 "ra. "和字符串 "Ray",你的函数应该返回true。同样的正则表达式在字符串 "raymond "上,应该返回false。 给定正则表达式".*at "和字符串 "chat",你的函数应该返回true。同样的正则表达式 This problem was asked by Facebook. Implement regular expression matching with the following special characters: - `.` (period) which matches any single character - `*` (asterisk) which matches zero or more of the preceding element That is, implement a function that takes in a string and a valid regular expression and returns whether or not the string matches the regular expression. For example, given the regular expression "ra." and the string "ray", your function should return true. The same regular expression on the string "raymond" should return false. Given the regular expression ".*at" and the string "chat", your function should return true. The same regular expressi ```python # https://github.com/vineetjohn/daily-coding-problem/blob/master/solutions/problem_025.py def is_match(regex, string): # if no pattern and no text, return True if not regex: return not string # first character will not be a Kleene star first_match = bool(string) and regex[0] in {string[0], '.'} if len(regex) >= 2 and regex[1] == '*': # regex[0] consumes no characters or # regex[0] consumes one character return is_match(regex[2:], string) or \ (first_match and is_match(regex, string[1:])) else: # regex[0] consumes one character return first_match and is_match(regex[1:], string[1:]) assert is_match("ra.", "ray") assert not is_match("ra.", "raymond") assert is_match(".*at", "chat") assert not is_match(".*at", "chats") ``` - [JS实现](https://github.com/iamvictorli/Daily-Coding-Problem/blob/master/solutions/21-30/Problem25.js) ### 每天学点机器学习 #### 斯坦福机器学习课程 Week1 ##### 模型表示 Model Representation 以房屋面积(x)与房价(y)为训练数据进行预测。 ```mermaid graph TD; A["Training Set 训练数据集"]--> B["Learning Algorithm 学习算法"] B --> h["h (Hypothesis 预测/假设)"] C["x | Size of house 房屋面积"] --> h h --> D["y | Estimated price 价格预测"] ``` ##### 代价函数 Cost Function 假设房价y=h(x),因为是一元线性回归问题,所以设有如下方程: $$ h_\theta(x) = \theta_0 + \theta_1x $$ 当面积为x时,通过h的预测值减去实际的y值,就是误差。这里的目标就是让误差和趋近零。 $\sum$ 求误差平方和,除m求均差,再除2是为了方便降阶计算,因为平方函数的导数项会抵消 $\frac{1}{2}$ $$ J(\theta_0,\theta_1)=\frac{1}{2m}\sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})^2 $$ 这个函数也叫 "平方误差函数(Squared error function)",或 "平均平方误差(Mean squared error)"。 ※复习下导数的概念:(1) [Introduction to Derivatives](https://www.mathsisfun.com/calculus/derivatives-introduction.html) (2) [Derivative Rules](https://www.mathsisfun.com/calculus/derivatives-rules.html) 总结,这里的假设函数h(x),参数θ,数据量m,代价函数J(θ)。 h是关于房屋面积的函数,而J是关于参数的函数。目标是获得最小的J(θ)。 下面是随堂小测。 ![9cXHus](https://gitee.com/morphicx/image-host/raw/master/uPic/9cXHus.png) 答案:想要求J(0)的值,也就是θ1=0的时候,带入代价函数,斜率为0所以误差就是x,得到1/2m*sum(1^2 +2^2 +3^2 )= 14/6 这里画出J(θ1)的图像,当θ=1时就是理想中的最小误差。 ![img](https://gitee.com/morphicx/image-host/raw/master/uPic/fph0S5tTEeajtg5TyD0vYA_9b28bdfeb34b2d4914d0b64903735cf1_Screenshot-2016-10-26-01.09.05.png) 上图是在只有J(θ1)的假设下进行的,当考虑到J(θ0, θ1)时,代价函数会变成一个2维的碗状图形。 这里用等高线图下进行显示,等高线中心的地方就是误差最小的时候。 ![img](https://gitee.com/morphicx/image-host/raw/master/uPic/hsGgT536Eeai9RKvXdDYag_2a61803b5f4f86d4290b6e878befc44f_Screenshot-2016-10-29-09.59.41.png) 学完这个module之后,去看了下讲师吴恩达的简介,respect。 搜的时候还看到一个有趣的知乎:[为何中国人迷恋吴恩达的机器学习课?](https://www.zhihu.com/question/65780182/answer/234848513) ### 其他值得阅读 #### 财务自由的真正意义 原文:[The True meaning of financial freedom](https://convertkit.com/creator-stories-nick-true?) ##### 意义就是“未来的自由” > “It was about not getting stuck.” 对尼克来说,这始于他上中学时在教堂上的 "儿童预算 "课。钱对他来说很有趣,就像一种游戏,赢家有更多选择,更多自由。当他的父母开始根据他做的家务给他钱时,他开始玩这个游戏。 > I saved a ton of money because I desired future flexibility. 即使还是一个孩子的时候,我也存了很多钱,因为我渴望未来的灵活性。 ### 一点收获 - 就是因为是父母,才容易不从她们角度出发,只考虑自己。 - html表单不同button提交不同地址: ``` ```