In [99]:
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
sns.set_style("white")

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

In [101]:
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[101]:
[<matplotlib.lines.Line2D at 0x7ea41ed2e8>]

In [102]:
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 [103]:
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 [104]:
def cost_function(X,y,theta):
    h_theta = theta[0] * X['X0'] + theta[1] * X['X1']
    J = sum((h_theta - y) ** 2) / (m * 2)
    return J

In [105]:
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 [106]:
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 [107]:
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(0,theta0_vals_len):
    for j in range(0,theta1_vals_len):
        t = pd.Series([theta0_vals[i],theta1_vals[j]])
        j_vals[(i,j)] = cost_function(X,y,t)

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



In [109]:
# 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(fps=30, extra_args=['-vcodec', 'libx264'])
# anim.save('basic_animation.mp4',writer=FFwriter)

In [111]:
fig = plt.figure(figsize=(12,8))
CS = plt.contour(theta0_vals,theta1_vals,j_vals,30,cmap='viridis')
plt.clabel(CS, inline=20, fontsize=10)
plt.title('Simplest default with labels')


Out[111]:
<matplotlib.text.Text at 0x7e9f85a400>

In [67]:



Out[67]:
0.0

In [ ]: