This file contains 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 plot_histories(*args): | |
''' | |
Plots histories | |
''' | |
epochs1 = np.arange(1, len(pd.DataFrame(args[0].history)) + 1) | |
fig, ax = plt.subplots(len(args), 2, figsize=(16, 7 * len(args))) | |
fig.suptitle('Comparing Model Histories') | |
for i in range(len(args)): | |
loss1 = pd.DataFrame(args[i].history["loss"]) | |
ax[i, 0].set_title(f"Model {i + 1} Loss") |
This file contains 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
class LinearRegressor: | |
def __init__(self, iters, X, y, lr): | |
self.iters = iters | |
self.X = X | |
self.lr = lr | |
self.y = y | |
self.b = 0 | |
self.w = 1 | |
def hypothesis_function(self, x): | |
list_of_hypothesis_functions = [] |
This file contains 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 | |
def hypothesis_function(θ0, θ1, X): | |
y = θ0 + θ1 * np.array(X) | |
return y |
This file contains 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 cost_function(y_preds, y_true): | |
return sum((y_preds - y_true) ** 2) / len(y_preds) |
This file contains 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 gradient_descent(θ0, θ1, x, y, lr): | |
new_weights = [] | |
y_preds = θ0 + θ1 * x | |
new_weights.append(θ0 - lr * (2 / len(y)) * sum(y_preds - y)) | |
new_weights.append(θ1 - lr * sum((y_preds - y) * x)) | |
return new_weights |
This file contains 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 | |
class LinearRegressor(): | |
def __init__(self): | |
self.θ0 = np.random.random((1,))[0] | |
self.θ1 = np.random.random((1,))[0] | |
def predict(self, x): | |
return self.θ0 + self.θ1 * np.array(x) | |
def compute_loss(self, y_preds, y_true): |
This file contains 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
np.random.seed(42) | |
area = np.array([round(i, 2) for i in np.abs(np.random.random(size=(1000, ))) * 2000]) | |
age = np.array([round(i, 2) for i in np.abs(np.random.random(size=(1000, ))) * 20]) | |
price = area * 100 + age * 30 | |
data = pd.DataFrame({"Area": area, "Age": age, "Price": price}) | |
data |
This file contains 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 | |
def hypothesis_function(θ, x): | |
return np.matmul(np.array(θ).T, np.array(x)) |