Skip to content

Instantly share code, notes, and snippets.

View Chiraagkv's full-sized avatar
💭
Playing with Machine Learning and Python

Chiraagkv

💭
Playing with Machine Learning and Python
View GitHub Profile
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")
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 = []
import numpy as np
def hypothesis_function(θ0, θ1, X):
y = θ0 + θ1 * np.array(X)
return y
def cost_function(y_preds, y_true):
return sum((y_preds - y_true) ** 2) / len(y_preds)
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
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):
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
import numpy as np
def hypothesis_function(θ, x):
return np.matmul(np.array(θ).T, np.array(x))