Skip to main content

[Machine Learning]: #2 Model and Cost Function

[Machine Learning]: Model and Cost Function


Outline:
1) Linear Regression Model
2) Cost Function
3) Math Model
  • Linear Regression Model
First, we give an example of the linear regression model, the example as follows, we are given the house size and house price, we want to know the "relation" between the house price and house size. What should we do?
          House Size(\(x\)):  \([2104, 2000, 1216, 1234, 852, 737, 469, 220]\);
          House Price(\(y\)):  \([460, 300, 232, 315, 178, 177, 140, 100]\);
To describe the supervised learning problem slightly more formally, our goal is, given a training set, to learn a function H (H is the Hypothesis Fucntion): X → Y so that h(x) is a “good” predictor for the corresponding value of y. For historical reasons, this function h is called a hypothesis. Seen pictorially, the process is therefore like this:

         -> input:  \(x_{i}\)            
         -> output:  \(y_{i}\)
           

When \(y\) is continuous, we call the learning problem as a regression problem, when \(y\) is discrete values, we call the learning problem as a classification problem.
--------------------------------------------------------------------------------------------------------------------------
  • Cost Function : \({\theta _i}\)
Now, let's consider \(H\) is a Linear Function, and suppose \({H_\theta }\left( x \right) = {\theta _0} + {\theta _1}x\), the problem become how to choose \({\theta _i}\)'s.

How to do this? Here is an idea: choose \({\theta _i}\)'s so that the hypothesis function as close as to real value \(y\). 

Now, we define a Cost Function: \(J({\theta _0},{\theta _1})\), where

$$J({\theta _0},{\theta _1}) = {1 \over {2m}}\sum\nolimits_{i = 1}^m {{{(\hat y_i^{} - {y_i})}^2}}  = {1 \over {2m}}\sum\nolimits_{i = 1}^m {{{({H_\theta }({x_i}) - {y_i})}^2}} $$

And our Goal is to minimize the Cost Function !!!

--------------------------------------------------------------------------------------------------------------------------
Now we get the mathematic model for this question

  • Math Model

         Hypothesis:                              \({H_\theta }(x) = {\theta _0} + {\theta _1}x\)

         Parameters:                              \({\theta _0}, {\theta _1}\)

         Cost Function:                         \(J({\theta _0},{\theta _1}) = {1 \over {2m}}\sum\nolimits_{i = 1}^m {{{(\hat y_i^{} - {y_i})}^2}}  = {1 \over {2m}}\sum\nolimits_{i = 1}^m {{{({H_\theta }({x_i}) - {y_i})}^2}} \)

         Goal: minimize:                        \(J({\theta _0},{\theta _1})\)

How can we solve this problem?
Please continue read another Blog [Machine Learning]: Parameter Learning

Comments

Popular posts from this blog

[LeetCode Solution 230]: Kth Smallest Element in a BST

Question: Given a binary search tree, write a function  kthSmallest  to find the  k th smallest element in it. ************************************************************************************************************************************ Write Infront To read to a tutorial, please to read the tutorial of in-order traversal of BST, please check: LeetCode Solution 94: Binary Tree Inorder Traversal We are going to solve this question using the following 4 methods: ->Binary Search ->Recursive ->Iterative ->Morris  Approach #1 Binary Search [Accepted] Detail Explanation The first method to solve this problem is using Binary Search. The idea is very easy and extremely to think. We use BST's property that the left child of the root is smaller than the root while the right child of the root is always bigger. We consider that the root is the pivot, and find the number of the nodes in the left subtree and the number of the nodes in th

[LeetCode Solution 145] Binary Tree Postorder Traversal

[LeetCode Solution 145]: Binary Tree Postorder Traversal Question: Given a binary tree, return the  postorder  traversal of its nodes' values. For example: Given binary tree  {1,#,2,3} , 1 \ 2 / 3 return  [3,2,1] . Approach #1 Recursive [Accepted] Detail Explanation The first method to solve this problem is using recursive. This is the classical method and straightforward. we can define a helper function to implement recursion. The java code is as follows: Java public class Solution { public List<Integer> postorderTraversal (TreeNode root) { List<Integer> res = new ArrayList<>(); helper(root, res); return res; } public void helper (TreeNode root, List<Integer> res) { if (root != null ) { if (root.left != null ) { helper(root.left, res); } if (root.right != null ) { helper(root.right, res); } res.add(root.val); } } } Complexity Analysis Time complexity :

[Interview]: URLify

[Interview]  URLify: -------------------------------------------------------------------------------------------------------------------------- Question: URLify: Write a method to replace all spaces in a string with ‘%20’, you may assume that the string has sufficient space at the end to hold the additional characters. Example  input: ' mr john smith '  output: ' mr %20john%20smith' --------------------------------------------------------------------------------------------------------------------------   Idea 1:  Start from the back and start replacing until the character is not ' ', and replace the characters in reverse order. Solution 1: public class Solution{ public String replace(char[] str) { boolean flag = false; StringBuffer sb = new StringBuffer(); for (int i = str.length - 1; i >= 0; i--) { if (str[i] != ' ') flag = true; if (flag == true) { if (str[i] == ' ') { s