In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.mplot3d import axes3d, Axes3D #<-- Note the capitalization! 
%matplotlib inline

In [18]:
df = pd.read_csv('ex1data1.txt',sep=',',header= None)
df.columns = ['X','y']
m = df.shape[0]

In [19]:
sns.lmplot('X','y',data=df,size=9)
plt.plot(df['X'],df['y'],'rX',markersize=10)
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $10,000s')
plt.plot(X['X1'],h_theta,c="green")


Out[19]:
[<matplotlib.lines.Line2D at 0xc6a4b11240>]

In [20]:
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

In [21]:
X = pd.DataFrame({'X0': np.ones(m), 'X1': df['X']})
y = df['y']
theta = pd.Series(np.zeros(2))
iterations = 1500
alpha = 0.01
h_theta = 0

In [22]:
def cost_function(X,y,theta):
    global h_theta
    h_theta = theta[0] * X['X0'] + theta[1] * X['X1']
    J = sum((h_theta - y) ** 2) / (m * 2)
    return J

In [23]:
def cost_function_train():
    h_theta = theta[0] * X_train['X0'] + theta[1] * X_train['X1']
    J = sum((h_theta - y_train) ** 2) / (m * 2)
    return J

In [24]:
def gradient_function():
    for n in range(iterations):
        global theta
        cost_function(X,y,theta)
        theta = theta - (alpha / m) * np.dot((h_theta - y).T,X).T
    return theta

In [25]:
theta0_vals = np.linspace(-10,10,100)
theta1_vals = np.linspace(-1,4,100)
theta0_vals_len = theta0_vals.shape[0]
theta1_vals_len = theta1_vals.shape[0]
j_vals = np.zeros((theta0_vals_len,theta1_vals_len))

for i in range(1,theta0_vals_len):
    for j in range(1,theta1_vals_len):
        t = pd.Series([theta0_vals[i],theta1_vals[j]])
        j_vals[(i,j)] = cost_function(X,y,t)

In [26]:
fig = plt.figure(figsize=(12,8))
ax = fig.gca(projection='3d')
ax.set_xlabel("Theta 0")
ax.set_ylabel("Theta 1")
ax.plot_surface(theta0_vals,theta1_vals,j_vals,linewidth=0)
ax.invert_xaxis()



In [ ]:
from matplotlib import animation
def init():
    global fig;
    return fig,

def animate(i):
    ax.view_init(elev=10., azim=i)
    return fig,
plt.rcParams['animation.ffmpeg_path'] = 'C:/ffmpeg/bin/ffmpeg'

# Animate
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=360, interval=20, blit=True)
# Save
FFwriter = animation.FFMpegWriter()
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

In [ ]:
fig = plt.figure(figsize=(12,8))
CS = plt.contour(theta0_vals,theta1_vals,j_vals,linewidth=0)
plt.clabel(CS, inline=1, fontsize=10)
plt.title('Simplest default with labels')