This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import partial, wraps | |
| from inspect import signature | |
| from typing import Callable | |
| def decorator_with_kwargs(decorator: Callable) -> Callable: | |
| """Decorator factory to give decorated decorators the skill to receive | |
| optional keyword arguments. | |
| If a decorator "some_decorator" is decorated with this function: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| y_pred = X.dot(theta_gd) | |
| _, ax = plt.subplots(figsize=(8,8)) | |
| ax.scatter(y, y_pred, marker='o', alpha=0.5) | |
| ax.set_xlabel('y', fontsize=16) | |
| ax.set_ylabel('y predito', fontsize=16) | |
| ax.tick_params(axis='both',labelsize=14) | |
| ax.grid() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def descida_de_gradiente(num_iteracoes, learning_rate, X, y): | |
| theta = np.random.uniform(size=(X.shape[1],)) | |
| J_list = [] | |
| for _ in range(num_iteracoes): | |
| J, gradiente = funcao_custo(theta, X, y) | |
| J_list.append(J) | |
| theta = theta - learning_rate*gradiente | |
| _, ax = plt.subplots(figsize=(10,6)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def funcao_custo(theta, X, y): | |
| m = len(y) | |
| y_pred = X.dot(theta).flatten() | |
| J = (1/m)*np.sum((y-y_pred)**2) | |
| gradiente = -(2/m)*((X.T).dot(y - y_pred)).flatten() | |
| return J, gradiente |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| def criar_dados_sinteticos(): | |
| m = 10000 # numero de exemplos | |
| D = 5 # numero de dimensoes | |
| sigma=0.05 # desvio padrao da distribuicao do ruido | |
| theta_sintetico = [1.5, 2.2, 1.7, -3.0, 0.3] # lista dos parâmetros theta (inventados) |