Objetivos:
In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import ipywidgets as wg
from ipywidgets import interactive, fixed
%matplotlib inline
# jupyter nbextension enable --py widgetsnbextension --sys-prefix
# restart jupyter notebook
In [ ]:
df = pd.read_csv('data/medidas.csv')
print(df.shape)
df.head(10)
In [ ]:
x = df.Altura
y = df.Peso
plt.figure()
plt.scatter(x, y)
plt.xlabel('Altura')
plt.ylabel('Peso')
In [ ]:
def plot_line(w, b):
plt.figure(0, figsize=(20,4))
plt.subplot(1,3,3)
plt.scatter(x, y)
y_pred = x*w + b
plt.plot(x, y_pred, c='red')
plt.xlim(140, 210)
plt.ylim(40, 120)
plt.subplot(1,3,2)
x_ = np.array([0, x.max()])
y_ = x_*w + b
plt.scatter(x, y)
plt.plot(x_, y_, c='red')
plt.xlim(0, 210)
plt.ylim(-160, 120)
plt.subplot(1,3,1)
mse = np.mean((y - y_pred)**2)
loss.append(mse)
plt.plot(loss)
plt.title('Loss')
plt.show()
In [ ]:
loss = []
interactive_plot = interactive(plot_line, w=(1, 1.5, 0.01), b=(-200, 0, 1))
output = interactive_plot.children[-1]
output.layout_height = '350px'
interactive_plot
In [ ]:
from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(x.values.reshape(-1,1), y)
print("w: {:.2f} \nb: {:.2f}".format(reg.coef_[0], reg.intercept_))
In [ ]:
def plot_line(w1, w2, b):
x1, x2 = np.meshgrid(np.linspace(0,1,100), np.linspace(0,1,100))
x_mesh = np.array([x1.ravel(), x2.ravel()]).T
plt.figure(0, figsize=(10,4))
plt.subplot(1,2,2)
plt.scatter(x[:,0], x[:,1], c=y.ravel(), s=100, cmap='bwr')
y_mesh = np.dot(x_mesh, np.array([w1, w2]).T) + b
y_mesh = np.where(y_mesh <= 0, 0, 1)
plt.contourf(x1, x2, y_mesh.reshape(x1.shape), cmap='bwr')
y_pred = np.dot(x, np.array([w1, w2]).T) + b
y_bin = np.where(y_pred <= 0, 0, 1)
print('{0} => {1}'.format(y_pred, y_bin))
plt.subplot(1,2,1)
mse = np.mean((y.ravel() - y_bin)**2)
loss.append(mse)
plt.plot(loss)
plt.title('Loss')
plt.show()
In [ ]:
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0, 0, 0, 1]]).T
print(x, y, sep='\n')
In [ ]:
plt.scatter(x[:,0], x[:,1], c=y.ravel(), s=50, cmap='bwr')
In [ ]:
loss = []
interactive_plot = interactive(plot_line, w1=(-1,1,0.01), w2=(-1,1,0.01), b=(-1.5, 1.5, 0.01))
interactive_plot
In [ ]:
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0, 1, 1, 1]]).T
print(x, y, sep='\n')
In [ ]:
plt.scatter(x[:,0], x[:,1], c=y.ravel(), s=50, cmap='bwr')
In [ ]:
loss = []
interactive_plot = interactive(plot_line, w1=(-1,1,0.01), w2=(-1,1,0.01), b=(-1.5, 1.5, 0.01))
interactive_plot
In [ ]:
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0, 1, 1, 0]]).T
print(x, y, sep='\n')
In [ ]:
plt.scatter(x[:,0], x[:,1], c=y.ravel(), s=50, cmap='bwr')
In [ ]:
loss = []
interactive_plot = interactive(plot_line, w1=(-1,1,0.01), w2=(-1,1,0.01), b=(-1.5, 1.5, 0.01))
interactive_plot