def fit_(x, y, theta, alpha, max_iter): while cost_(y, predict_(x, theta)) != 0 and max_iter != 0: theta[0] = float(theta[0] - alpha * (gradient(x, y, theta)[0])) theta[1] = float(theta[1] - alpha * (gradient(x, y, theta)[1])) max_iter -= 1 return (np.array(theta))
import numpy as np from cost import cost_ X = np.array([0, 15, -9, 7, 12, 3, -21]) Y = np.array([2, 14, -13, 5, 12, 4, -19]) # Example 1: print(cost_(X, Y)) # Output: # 4.285714285714286 # # Example 2: print(cost_(X, X)) # Output: # 0.0
def test5(): # Example 5: print(y3) print(y_hat3) assert cost_(y3, y_hat3) == 4.285714285714286 / 2
def test6(): # Example 6: assert cost_(y3, y3) == 0.0
def test2(): # Example 2: assert cost_(y1, y_hat1) == 3.0
def test4(): # Example 4: assert cost_(y2, y_hat2) == 4.238750000000004
x1 = np.array([[0.], [1.], [2.], [3.], [4.]]) theta1 = np.array([[2.], [4.]]) y_hat1 = predict_(x1, theta1) print(y_hat1) y1 = np.array([[2.], [7.], [12.], [17.], [22.]]) # Example 1: print(cost_elem_(y1, y_hat1)) # Output: # array([[0.], [0.1], [0.4], [0.9], [1.6]]) # Example 2: print(cost_(y1, y_hat1)) # Output: # 3.0 x2 = np.array([[0.2, 2., 20.], [0.4, 4., 40.], [0.6, 6., 60.], [0.8, 8., 80.]]) theta2 = np.array([[0.05], [1.], [1.], [1.]]) y_hat2 = predict_(x2, theta2) y2 = np.array([[19.], [42.], [67.], [93.]]) # Example 3: print(cost_elem_(y2, y_hat2)) # Output: # array([[1.3203125], [0.7503125], [0.0153125], [2.1528125]]) # Example 4: print(cost_(y2, y_hat2))
from cost import cost_elem_, cost_, mse from prediction import predict_ import numpy as np X = np.array([0, 15, -9, 7, 12, 3, -21]) Y = np.array([2, 14, -13, 5, 12, 4, -19]) print(cost_(X, Y))
#!/usr/bin/env python3 from cost import cost_, cost_elem_ from prediction import predict_ import numpy as np x1 = np.array([[0.], [1.], [2.], [3.], [4.]]) theta1 = np.array([[2.], [4.]]) y_hat1 = predict_(x1, theta1) y1 = np.array([[2.], [7.], [12.], [17.], [22.]]) output = cost_elem_(y1, y_hat1) print('Example 1:', output.tolist()) #assert np.array_equal(output, [[0.], [0.1], [0.4], [0.9], [1.6]]) output = cost_(y1, y_hat1) print('Example 2:', output) x2 = np.array([[0.2, 2., 20.], [0.4, 4., 40.], [0.6, 6., 60.], [0.8, 8., 80.]]) theta2 = np.array([[0.05], [1.], [1.], [1.]]) y_hat2 = predict_(x2, theta2) y2 = np.array([[19.], [42.], [67.], [93.]]) output = cost_elem_(y2, y_hat2) print('Example 3:', output.tolist()) output = cost_(y2, y_hat2) print('Example 4:', output) x3 = np.array([0, 15, -9, 7, 12, 3, -21]) theta3 = np.array([[0.], [1.]]) y_hat3 = predict_(x3, theta3)
from cost import cost_elem_, cost_ from prediction import predict_ import numpy as np x1 = np.arange(0, 5) theta1 = np.array([2, 4]) y_hat1 = predict_(x1, theta1) y1 = np.array([2, 7, 12, 17, 22]) print(repr(cost_elem_(y1, y_hat1))) print(repr(cost_(y1, y_hat1)))
#!/usr/bin/env python3 import numpy as np from cost import cost_ X = np.array([0, 15, -9, 7, 12, 3, -21]) Y = np.array([2, 14, -13, 5, 12, 4, -19]) # Example 1: output = cost_(X, Y) print(output) assert output == 4.285714285714286 # Example 2: output = cost_(X, X) print(output) assert output == 0.0