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
sns.set_style("white")
In [30]:
#functions
def feature_normalize(df_n) :
return ( ( df_n - df_n.mean() ) / df_n.std() )
def cost_function(X,y,theta):
h_theta = np.dot(theta.T,X.T).T
J = sum( ( 1 / ( 2 * m ) ) * ((h_theta - y) ** 2))
return J
def gradient_function():
for n in range(iterations):
global theta
last_j[n] = cost_function(X,y,theta)
h_theta = np.dot(theta.T,X.T).T
theta = theta - (alpha / m) * np.dot((h_theta - y).T,X).T
return theta
In [31]:
df = pd.read_csv('ex1data2.txt', sep=',', header=None)
df.describe()
Out[31]:
In [32]:
df.head()
Out[32]:
In [33]:
df_norm = feature_normalize(df)
In [34]:
m = df_norm.shape[0]
In [35]:
df_norm.insert(0,'3',np.ones(m))
df.insert(0,'3',np.ones(m))
In [36]:
df_norm.columns = np.arange(0,4)
df.columns = np.arange(0,4)
In [37]:
# initialization
theta = pd.Series(np.zeros(df_norm.columns.shape[0] - 1))
y = df_norm[3]
X = df_norm.T[0:3].T
alpha = 0.1
iterations = 50
last_j = np.zeros(50)
In [38]:
gradient_function()
Out[38]:
In [18]:
# plotting Cost function versus Number of iterations
itr_list = np.arange(0,50)
fig = plt.figure(figsize=(12,8))
plt.plot(itr_list,last_j,'-b')
Out[18]:
In [829]:
xx = ([1650, 3] - df.mean()[1:3]) / df.std()[1:3]
xx
Out[829]:
In [830]:
xx = pd.DataFrame(xx).T
xx
Out[830]:
In [831]:
xx.insert(0,3,1)
xx
Out[831]:
In [832]:
xx.columns = np.arange(0,3)
In [835]:
np.dot(theta.T,xx.T)[0]
Out[835]:
In [848]:
y = df[3]
X = df.T[0:3].T
In [837]:
theta = np.dot(np.dot(np.dot(X.T,X),X.T),y)
In [849]:
theta
Out[849]:
In [850]:
xx = [1650, 3]
In [851]:
xx = pd.DataFrame(xx).T
In [852]:
xx.insert(0,3,1)
In [853]:
xx.columns = np.arange(0,3)
In [854]:
np.dot(theta.T,xx.T)[0]
Out[854]:
In [ ]:
In [ ]: