def plot_with_cost(x, y, theta): for i in range(len(x)): plt.plot(x[i], y[i], 'bo') plt.plot(x, predict_(x, theta), 'r') plt.vlines(x, y, predict_(x, theta), 'r', "dashed") plt.title("Cost: {}".format(cost_(y, predict_(x, theta)))) plt.show()
def main(): 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.]]) print(cost_elem_(y1, y_hat1)) print(cost_(y1, y_hat1)) plot(x1, y1, theta1) 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.]]) print(cost_elem_(y2, y_hat2)) print(cost_(y2, y_hat2)) plot(x2, y2, theta2) x3 = np.array([0, 15, -9, 7, 12, 3, -21]) theta3 = np.array([[0.], [1.]]) y_hat3 = predict_(x3, theta3) y3 = np.array([2, 14, -13, 5, 12, 4, -19]) print(cost_(y3, y_hat3)) print(cost_(y3, y3)) plot(x3, y3, theta3)
def reg_linear_grad(y, x, theta, lambda_): """Computes the regularized linear gradient of three non-empty numpy.ndarray, with two ,→ for-loop. The three arrays must have compatible dimensions. Args: y: has to be a numpy.ndarray, a vector of dimension m * 1. x: has to be a numpy.ndarray, a matrix of dimesion m * n. theta: has to be a numpy.ndarray, a vector of dimension n * 1. lambda_: has to be a float. Returns: A numpy.ndarray, a vector of dimension n * 1, containing the results of the formula for all ,→ j. None if y, x, or theta are empty numpy.ndarray. None if y, x or theta does not share compatibles dimensions. Raises: This function should not raise any Exception. """ if x.size == 0 or y.size == 0 or theta.size == 0 or x.shape[0] != y.shape[ 0] or x is None or y is None: return None gr_vec = np.zeros((theta.shape[0], 1)) y_hat = predict_(x, theta) gr_vec[0] = np.sum((y_hat - y)) / float(y.shape[0]) for j in range(1, theta.shape[0]): gr_vec[j] = (np.sum((y_hat - y) * x[:, j - 1].reshape(-1, 1)) + (lambda_ * theta[j])) / y.shape[0] return gr_vec
def vec_reg_linear_grad(y, x, theta, lambda_): """Computes the regularized linear gradient of three non-empty numpy.ndarray, without any ,→ for-loop. The three arrays must have compatible dimensions. Args: y: has to be a numpy.ndarray, a vector of dimension m * 1. x: has to be a numpy.ndarray, a matrix of dimesion m * n. theta: has to be a numpy.ndarray, a vector of dimension n * 1. lambda_: has to be a float. Returns: A numpy.ndarray, a vector of dimension n * 1, containing the results of the formula for all ,→ j. None if y, x, or theta are empty numpy.ndarray. None if y, x or theta does not share compatibles dimensions. Raises: This function should not raise any Exception. """ if x.size == 0 or y.size == 0 or theta.size == 0 or x.shape[0] != y.shape[ 0] or x is None or y is None: return None y_hat = predict_(x, theta) theta2 = np.copy(theta) theta2[0] = 0 gr_vec = (np.matmul(np.transpose(add_intercept(x)), (y_hat - y)) + (lambda_ * theta2)) / y.shape[0] return gr_vec
def plot_with_cost(x, y, theta): y_hat = predict_(x, theta) # const_elm = cost_elem_(y, y_hat) plt.title(f'Cost: {cost_(y, y_hat)}') plt.scatter(x, y) plt.plot(x, y_hat, c='r') plt.show()
def simple_gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. The three arrays must have compatible dimensions. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 1. None if x, y, or theta are empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ if (not isinstance(x, np.ndarray) or not isinstance(y, np.ndarray) or not isinstance(theta, np.ndarray) or theta.size != 2 or x.size != y.size or x.ndim != 1 or y.ndim != 1 or theta.ndim != 1): return None else: h = predict_(x, theta) j0 = (1 / y.size) * (h - y) j0 = abs(j0.sum()) j1 = (1 / y.size) * ((h - y) * x) j1 = abs(j1.sum()) return np.array((j0, j1))
def simple_gradient(x: np.ndarray, y: np.ndarray, theta: np.ndarray) -> np.ndarray: """Computes a gradient vector from three non-empty numpy.ndarray. The three arrays must have compatible dimensions. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 1. None if x, y, or theta are empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ if (0 in [len(x), len(y), len(theta)] or x.shape != y.shape or (x.shape[1] + 1) != theta.shape[0]): return None res = np.zeros(shape=(theta.shape)) m = x.shape[0] y_hat = predict_(x, theta) for i in range(m): res[0][0] += (y_hat[i][0] - y[i][0]) res[1][0] += (y_hat[i][0] - y[i][0]) * (x[i][0]) res = res / m return res
def plot_with_cost(x, y, theta): plt.title("Cost : " + str(cost_(y, predict_(x, theta)))) plt.plot(x, y, 'ro', color='blue') pred = predict_(x, theta) plt.plot(x, pred, color='orange') zeroes = np.zeros_like(x) difs = np.concatenate((zeroes, x)) for valx, valpred, valy in zip(x, pred, y): if valpred > valy: mini = valy maxi = valpred else: mini = valpred maxi = valy plt.vlines(x=valx, ymin=mini, ymax=maxi, linestyle='--', color='red') plt.show()
def plot_with_cost(x, y, theta): plt.plot(x, y, 'o') plt.plot(x, (theta[1] * x + theta[0]), '-') y_hat = predict_(x, theta) cost = half_mse_(y, y_hat) plt.plot((x, x), (y, y_hat), '--r') plt.title(("Cost: " + str(round(cost, 6)))) plt.show()
def plot_with_cost(x, y, theta): y_hat = predict_(x, theta) cost = cost_(y, y_hat) plt.plot(x, y, 'bo') plt.plot(x, y_hat, 'r', c='orangered') plt.title(str(cost)) plt.plot((x, x), (y, y_hat), '--r') plt.show()
def simple_gradient(x, y, theta): h = predict_(x, theta) if h is None: return None check, y, h = check_size_and_shape(y, h) if not check: return None #x[:, :1] is the first column of x values (as x here does not have column of 1s) return np.array([np.sum(h - y) / y.size, np.sum((h - y) * x[:, :1]) / y.size])
def test_correc(): x = np.arange(1, 6) # Example 1: theta1 = np.array([5, 0]) assert np.equal(predict_(x, theta1), np.array([5., 5., 5., 5., 5.])).all() # Do you understand why y_hat contains only 5's here? # Example 2: theta2 = np.array([0, 1]) assert (predict_(x, theta2) == np.array([1., 2., 3., 4., 5.])).all() # Do you understand why y_hat == x here? # Example 3: theta3 = np.array([5, 3]) assert (predict_(x, theta3) == np.array([8., 11., 14., 17., 20.])).all() # Example 4: theta4 = np.array([-3, 1]) assert (predict_(x, theta4) == np.array([-2., -1., 0., 1., 2.])).all()
def simple_gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. The ,→ three arrays must have compatible dimensions. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. 11 theta: has to be an numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 1. None if x, y, or theta are empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ # try: return np.array([ 1.0 / len(x) * np.sum(predict_(x, theta) - y), 1.0 / len(x) * np.sum( (predict_(x, theta) - y) * x) ])
def simple_gradient(x, y, theta1): ret = [] s0 = 0 s1 = 0 y_hat = predict_(x, theta1) for i in range(len(y)): s0 += y_hat[i] - y[i] s1 += (y_hat[i] - y[i]) * x[i] ret.append(s0 / len(y)) ret.append(s1 / len(y)) return (np.array(ret))
def simple_gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. ,→ The three arrays must have compatible dimensions. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 1. None if x, y, or theta are empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ m = x.shape[0] # print(predict_(x, theta)) # print(y) # print(predict_(x, theta) - y) j0 = (predict_(x, theta) - y).sum() / m j1 = ((predict_(x, theta) - y).dot(x)) / m # print(j1) return np.array([j0, j1])
def plot(x, y, theta): """Plot the data and prediction line from three non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: Nothing. Raises: This function should not raise any Exceptions. """ plt.plot(x, y, 'bo') plt.plot(x, predict_(x, theta), 'r-') plt.show()
def test(): # Example 0: theta1 = fit_(x, y, alpha=5e-8, max_iter=1500000) print("theta1:", theta1) # Output: print("should be:", np.array([[1.40709365], [1.1150909]])) # Example 1: print("y_pred:", predict_(x, theta1)) # Output: print( "should be:", np.array([[15.3408728], [25.38243697], [36.59126492], [55.95130097], [65.53471499]])) assert True == False
def main(): 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.]) print(cost_elem_(y1, y_hat1)) print(cost_(y1, y_hat1)) 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.]]) print(cost_elem_(y2, y_hat2)) print(cost_(y2, y_hat2)) x3 = np.array([[0], [15], [-9], [7], [12], [3], [-21]]) theta3 = np.array([[0.], [1.]]) y_hat3 = predict_(x3, theta3) y3 = np.array([[2], [14], [-13], [5], [12], [4], [-19]]) print(cost_elem_(y3, y_hat3)) print(cost_(y3, y_hat3)) print(cost_(y3, y3))
def test2(): theta2 = np.array([1, -0.4]) res = simple_gradient(x, y, theta2) loss = mse_(x, predict_(x, theta2)) print(res, loss) theta2 -= 0.001 * res res = simple_gradient(x, y, theta2) loss = mse_(x, predict_(x, theta2)) print(res, loss) theta2 -= 0.001 * res res = simple_gradient(x, y, theta2) loss = mse_(x, predict_(x, theta2)) print(res, loss) theta2 -= 0.0001 * res res = simple_gradient(x, y, theta2) loss = mse_(x, predict_(x, theta2)) print(res, loss) theta2 -= 0.0001 * res res = simple_gradient(x, y, theta2) loss = mse_(x, predict_(x, theta2)) print(res, loss) assert (res == np.array([58.86823748, 2229.72297889])).all()
def plot(x, y, theta): """Plot the data and prediction line from three non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: Nothing. Raises: This function should not raise any Exceptions. """ for x_, y_ in zip(x, y): plt.scatter(x_, y_, color='b', s=30) predX = predict_(x, theta) plt.plot(x, predX, color='r') plt.show()
def mse_(x, theta, y): """ Description: Calculate the MSE between the predicted output and the real output. Args: y: has to be a numpy.ndarray, a vector of dimension m * 1. y_hat: has to be a numpy.ndarray, a vector of dimension m * 1. Returns: mse: has to be a float. None if there is a matching dimension problem. Raises: This function should not raise any Exceptions. """ y_hat = predict_(x, theta) if len(y) < 1 or len(y_hat) < 1: return None return np.sum((y_hat - y)**2) / float(y.shape[0])
def plot(x, y, theta): """Plot the data and prediction line from three non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: Nothing. Raises: This function should not raise any Exceptions. """ plt.plot(x, y, 'ro') linex = np.linspace(np.min(x), np.max(x), 2) plt.plot(linex, predict_(linex, theta), color="blue", linestyle="dotted") plt.show()
def plot_with_cost(x, y, theta): """Plot the data and prediction line from three non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: Nothing. Raises: This function should not raise any Exception. """ yb = predict_(x, theta) plt.plot(x, yb, 'y') plt.plot(x, y, 'bo') for i in range(0, y.size): x1, y1 = [x[i], x[i]], [y[i], yb[i]] plt.plot(x1, y1, "r--") plt.title(f"Cost : {cost_(y,yb)}") plt.show()
def plot_with_cost(x, y, theta): """Plot the data and prediction line from three non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: Nothing. Raises: This function should not raise any Exception. """ y_hat = predict_(x, theta) plt.title(f'Cost : {cost_(y, y_hat)}') for i in range(x.shape[0]): plt.plot([x[i], x[i]], [y[i], y_hat[i]], 'r--', color='r') plt.plot(x, y, '.') plt.plot(x, y_hat) plt.show()
def plot_with_cost(x, y, theta): """ Plot the data and prediction line from three non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: Nothing. Raises: This function should not raise any Exception. """ y_hat = predict_(x, theta) plt.title(f'Cost: {cost_(y, y_hat)}') plt.plot(x, y_hat, color='r') for x_, y_, y_hat in zip(x, y, y_hat): plt.scatter(x_, y_, color='b', s=30) plt.plot([x_, x_], [y_hat, y_], 'r--', color='g') plt.show()
def gradient(x, y, theta): """ Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. The three arrays must have the compatible dimensions. Args: x: has to be an numpy.ndarray, a matrix of dimension m * n. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector (n +1) * 1. Returns: The gradient as a numpy.ndarray, a vector of dimensions n * 1, containg the result of the formula for all j. None if x, y, or theta are empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ if x.shape[0] != y.shape[0] or x.shape[1] + 1 != theta.shape[0]: return None xi = add_ones(x) return (1 / y.shape[0]) * np.sum(xi.T * (predict_(x, theta) - y), axis=1)
def vec_gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. The → three arrays must have the compatible dimensions. Args: x: has to be an numpy.ndarray, a matrix of dimension m * n. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector (n +1) * 1. Returns: The gradient as a numpy.ndarray, a vector of dimensions n * 1, containg the result of the → formula for all j. None if x, y, or theta are empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ if len(x) < 1 or len(y) < 1 or len(theta) < 1 or x is None or y is None or theta is None or x.shape[0] != y.shape[0]: return None y_hat = predict_(x, theta) gr_vec = (np.matmul(add_intercept(x).transpose(), (y_hat - y))) / y.shape[0] return gr_vec
def plot_with_cost(x, y, theta): """Plot the data and prediction line from three non-empty numpy.ndarray. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a vector of dimension 2 * 1. Returns: Nothing. Raises: This function should not raise any Exception. """ plt.plot(x, y, 'o') plt.plot(x, theta[1] * x + theta[0]) y_hat = predict_(x, theta) for x_i, y_hat_i, y_i in zip(x, y_hat, y): plt.plot([x_i, x_i], [y_i, y_hat_i], 'r--') plt.show()
def gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for loop. The ,→ three arrays must have compatible dimensions. Args: x: has to be a numpy.ndarray, a matrix of dimension m * 1. y: has to be a numpy.ndarray, a vector of dimension m * 1. theta: has to be a numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 1. None if x, y, or theta is an empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ if len(x) < 1 or len(y) < 1 or len( theta) < 1 or x.shape != y.shape or theta.shape[ 0] < 1 or x is None or y is None: return None y_hat = predict_(x, theta) gr_vec = (np.matmul(np.transpose(add_intercept(x)), (y_hat - y))) / y.shape[0] return gr_vec
def simple_gradient(x, y, theta): """Computes a gradient vector from three non-empty numpy.ndarray, without any for-loop. The → three arrays must have compatible dimensions. Args: x: has to be an numpy.ndarray, a vector of dimension m * 1. y: has to be an numpy.ndarray, a vector of dimension m * 1. theta: has to be an numpy.ndarray, a 2 * 1 vector. Returns: The gradient as a numpy.ndarray, a vector of dimension 2 * 1. None if x, y, or theta are empty numpy.ndarray. None if x, y and theta do not have compatible dimensions. Raises: This function should not raise any Exception. """ if len(x) < 1 or len(y) < 1 or len( theta) < 1 or x.shape != y.shape or theta.shape[ 0] < 1 or x is None or y is None: return None gr_vec = np.zeros((2, )) y_hat = predict_(x, theta) gr_vec[0] = np.sum((y_hat - y)) / float(y.shape[0]) gr_vec[1] = np.sum((y_hat - y) * x) / float(y.shape[0]) return gr_vec