Skip to content

Instantly share code, notes, and snippets.

@vndee
Last active September 6, 2022 11:23
Show Gist options
  • Save vndee/a43d002275c6c4a2b432244833a4f721 to your computer and use it in GitHub Desktop.
Save vndee/a43d002275c6c4a2b432244833a4f721 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""ml101-02.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/gist/HTInnovation/c548ec37a0fa3c48f85c04adea8ca1bf/ml101-02.ipynb
"""
import numpy as np
# Dataset
X = np.array([[1, 60, 2],
[1, 70, 3],
[1, 50, 1],
[1, 65, 3]])
Y = np.array([[1.8], [2.4], [1.2], [2.2]])
theta = np.linalg.inv(X.T @ X) @ X.T @ Y
print(f"Theta_0: {theta[0].item()}\nTheta_1: {theta[1].item()}\nTheta_2: {theta[2].item()}\n")
# Theta_0: -1.0000000000001164
# Theta_1: 0.04000000000000292
# Theta_2: 0.1999999999999862
import numpy as np
import matplotlib.pyplot as plt
def f(theta, x1, x2):
return theta[0] + theta[1] * x1 + theta[2] * x2
fig = plt.figure(figsize=(8, 8))
ax = plt.axes(projection='3d')
x = np.linspace(40, 80)
y = np.linspace(0, 5)
x1, x2 = np.meshgrid(x, y)
y = f(theta, x1, x2)
ax.scatter(X[:, 1], X[:, 2], Y[:, 0], c="r", marker="^")
ax.contour(x1, x2, y, 100)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('y')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment