Skip to content

Instantly share code, notes, and snippets.

@ice2heart
Created November 6, 2019 12:18
Show Gist options
  • Save ice2heart/6d9ecb007bcb10f3914e0069b255632e to your computer and use it in GitHub Desktop.
Save ice2heart/6d9ecb007bcb10f3914e0069b255632e to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
data = pd.read_csv('weights_heights.csv', index_col='Index')
# height = data.Height.values
# weight = data.Weight.values
# vfunc = np.vectorize(lambda x, y: (y - (w0 + w1 * x))**2)
def error(w0, w1):
# return sum(map(vfunc, data.Weight, data.Height)) # 4.867
return sum(map(lambda x, y: (y - (w0 + w1 * x))**2, data.Weight, data.Height)) # cpu 2.862 total
# return sum(map(lambda x, y: (y - (w0 + w1 * x))**2, weight, height)) # 3.343 total
fig = plt.figure()
fig.set_size_inches(15, 10)
ax = fig.gca(projection='3d')
w0 = np.arange(-100, 100, 1)
w1 = np.arange(-5, 5, 0.1)
w0, w1 = np.meshgrid(w0, w1)
er = error(w0, w1)
surf = ax.plot_surface(w0, w1, er)
ax.set_xlabel('Intercept')
ax.set_ylabel('Slope')
ax.set_zlabel('Error')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment