def custo_reglog(theta, X, y): theta = np.matrix(theta) X = np.matrix(X) y = np.matrix(y) grad0 = np.multiply(-y, np.log(sigmoide(X * theta.T))) grad1 = np.multiply((1 - y), np.log(1 - sigmoide(X * theta.T))) return np.sum(grad0 - grad1) / (len(X))
def gd_reglog(theta, _lambda, X, y): m = y.size h = sigmoide(X.dot(theta.reshape(-1, 1))) reg = (_lambda / m) * np.r_[[[0]], theta[1:].reshape(-1, 1)] grad = (1 / m) * X.T.dot(h - y) + reg return grad.flatten()
def cost_function_reg(theta, _lambda, X, y): m = y.size theta = theta.T reg = (_lambda / (2 * m)) * np.square(theta[1:]).sum() gx = sigmoide(X.dot(theta)) grad0 = np.log(gx).T.dot(y) grad = np.log(1 - gx).T.dot(1 - y) J = -(1 / m) * (grad0 + grad) + reg return (J[0])
def gd_reglog(theta, X, y): theta = np.matrix(theta) X = np.matrix(X) y = np.matrix(y) parametros = int(theta.ravel().shape[1]) grad = np.zeros(parametros) erro = sigmoide(X * theta.T) - y for i in range(parametros): term = np.multiply(erro, X[:, i]) grad[i] = np.sum(term) / len(X) return grad
def predizer(theta, X): probabilidade = sigmoide(X * theta.T) return [1 if x >= 0.5 else 0 for x in probabilidade]
from Parte2.normalizacao import normalizar_caracteristica if __name__ == '__main__': warnings.filterwarnings("ignore") print("=== Parte 3 ===") print("== Visualizacao dos dados ==") data, _, _ = plot_ex2data1.importarDados() plot_ex2data1.plot(data) print("=== Done ===") print("== Sigmoide ==") print("> sig 0") s = sigmoide(0) print(s) print("> sig pos") s = sigmoide(1000000) print(s) print("> sig neg") s = sigmoide(-9999999999) print(s) print("> sig array") s = sigmoide(np.array([11,2222222,-3,444,-55])) print(s) print("=== Done ===") print("== Custo J ==") _, X, y = plot_ex2data1.importarDados() theta = np.array([0, 0, 0], ndmin=2)