Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gourav_190103039/Assignment 3 Answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Features 2 and 10.
Reason: In all the plots consisting of features 2 and 10, we can see some sort of clustering of the data, which implies that we can predict the labelling of a variable plot depending on the closeness to either of the clusters just by plotting the graphs of these two features. That is why i claim that features 2 and 10 are sufficient to tell if a plot is label 1 orlabel 2 and hence classify that plot as one of the two.
35 changes: 35 additions & 0 deletions Gourav_190103039/Code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

data=np.genfromtxt('Data.txt',delimiter='\t',skip_header=1)
print(np.shape(data))

label=data[:,0]
print(label.dtype)
label=label.astype('int32')
print(label.dtype)
features=data[:,1:]

label1=np.array([]).reshape(0,10)
label2=np.array([]).reshape(0,10)
for i in range(999):
if label[i]==1:
label1=np.vstack((label1,features[i,:]))
else:
label2=np.vstack((label2,features[i,:]))

for i in range(10):
for j in range(i+1,10):
x1=np.array(label1[:,i])
y1=np.array(label1[:,j])
x2=np.array(label2[:,i])
y2=np.array(label2[:,j])
plt.scatter(x1,y1,label='1')
plt.scatter(x2,y2,label='2')
plt.legend()
plt.ylabel('Feature:{}'.format(j+1))
plt.xlabel('Feature:{}'.format(i+1))
plt.title('Feature:{} vs Feature:{}'.format(j+1,i+1))
plt.show()
Binary file added Gourav_190103039/Plots/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/27.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/28.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/29.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/31.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Gourav_190103039/Plots/32.png
Binary file added Gourav_190103039/Plots/33.png
Binary file added Gourav_190103039/Plots/34.png
Binary file added Gourav_190103039/Plots/35.png
Binary file added Gourav_190103039/Plots/36.png
Binary file added Gourav_190103039/Plots/37.png
Binary file added Gourav_190103039/Plots/38.png
Binary file added Gourav_190103039/Plots/39.png
Binary file added Gourav_190103039/Plots/4.png
Binary file added Gourav_190103039/Plots/40.png
Binary file added Gourav_190103039/Plots/41.png
Binary file added Gourav_190103039/Plots/42.png
Binary file added Gourav_190103039/Plots/43.png
Binary file added Gourav_190103039/Plots/44.png
Binary file added Gourav_190103039/Plots/45.png
Binary file added Gourav_190103039/Plots/5.png
Binary file added Gourav_190103039/Plots/6.png
Binary file added Gourav_190103039/Plots/7.png
Binary file added Gourav_190103039/Plots/8.png
Binary file added Gourav_190103039/Plots/9.png
1 change: 1 addition & 0 deletions Gourav_190103039/Plots/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

110 changes: 110 additions & 0 deletions Gourav_190103039/Week2/CodeWeek2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

def warmUpExercise():
A=np.eye(5)
return A

print(warmUpExercise())

data=np.genfromtxt('ex1data.txt',delimiter=',')
x=data[:,0]
y=data[:,1]

m=x.size

#Plotting the data
def plotShow(x,y):
plt.plot(x,y,'ro',ms=10,mec='k')
plt.xlabel("Population in 10,000s:")
plt.ylabel("Profit in $10000s")

plotShow(x,y)
plt.show()
x=np.stack([np.ones(m).T, x.T],axis=1)
print(x)

#Computing the Cost function
def computeCost(x,y,theta):
J=0
predictions= x.dot(theta)
sq_errors=(predictions-y)*(predictions-y)
J=(1/(2*m))*sum(sq_errors)
return J

J = computeCost(x, y, theta=np.array([0.0, 0.0]))
print('With theta = [0, 0] \nCost computed = %.2f' % J)
print('Expected cost value (approximately) 32.07\n')

J = computeCost(x, y, theta=np.array([-1, 2]))
print('With theta = [-1, 2]\nCost computed = %.2f' % J)
print('Expected cost value (approximately) 54.24')

#Gradient Descent
def gradientDescent(X, y, theta, alpha, num_iters):
m=y.size;
J_history = []
theta=theta.copy()
for i in range(num_iters):
temp=np.dot(X,theta)-y
temp0=theta[0]-((alpha/m)*sum(temp))
temp1=theta[1]-((alpha/m)*sum(temp*X[:,1]))
theta=np.array([temp0,temp1])
J_history.append(computeCost(X, y, theta))
return theta,J_history

# initialize fitting parameters
theta = np.zeros(2)

# some gradient descent settings
iterations = 1500
alpha = 0.01

theta, J_history = gradientDescent(x ,y, theta, alpha, iterations)
print('Theta found by gradient descent: {:.4f}, {:.4f}'.format(*theta))
print('Expected theta values (approximately): [-3.6303, 1.1664]')

plotShow(x[:, 1], y)
plt.plot(x[:, 1], np.dot(x, theta), '-',color='blue')
plt.legend(['Training data', 'Linear regression'])
plt.show()

# grid over which we will calculate J
theta0_vals = np.linspace(-10, 10, 100)
theta1_vals = np.linspace(-1, 4, 100)

# initialize J_vals to a matrix of 0's
J_vals = np.zeros((theta0_vals.shape[0], theta1_vals.shape[0]))

# Fill out J_vals
for i, theta0 in enumerate(theta0_vals):
for j, theta1 in enumerate(theta1_vals):
J_vals[i, j] = computeCost(x, y, [theta0, theta1])

# Because of the way meshgrids work in the surf command, we need to
# transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals.T

# surface plot
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(121, projection='3d')
ax.plot_surface(theta0_vals, theta1_vals, J_vals, cmap='viridis')
plt.xlabel('theta0')
plt.ylabel('theta1')
plt.title('Surface')
plt.show()

# contour plot
# Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
ax = plt.subplot(122)
plt.contour(theta0_vals, theta1_vals, J_vals, linewidths=2, cmap='viridis', levels=np.logspace(-2, 3, 20))
plt.xlabel('theta0')
plt.ylabel('theta1')
plt.plot(theta[0], theta[1], 'ro', ms=10, lw=2)
plt.title('Contour, showing minimum')
plt.show()



Binary file added Gourav_190103039/Week2/Figure_1.png
Binary file added Gourav_190103039/Week2/Figure_2.png
Binary file added Gourav_190103039/Week2/Figure_3.png
Binary file added Gourav_190103039/Week2/Figure_4.png
19 changes: 19 additions & 0 deletions Gourav_190103039/Week2/NormalEqn_optional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

data = np.loadtxt('ex2data.txt', delimiter=',')
X = data[:, :2]
y = data[:, 2]
m = y.size
X = np.concatenate([np.ones((m, 1)), X], axis=1)

def normalEqn(X, y):
theta = np.zeros(X.shape[1])
theta=np.dot(np.linalg.inv(np.dot(X.T,X)),np.dot(X.T,y))
return theta

# Calculate the parameters from the normal equation
theta = normalEqn(X, y)
print('Theta computed from the normal equations: {:s}'.format(str(theta)));
67 changes: 67 additions & 0 deletions Gourav_190103039/Week2/Optional Grad_desc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import style
style.use('ggplot')

data =np.genfromtxt('ex2data.txt',delimiter=',')
X=data[:,:2]
y=data[:,2]
m=y.size

print('{:>8s}{:>8s}{:>10s}'.format('X[:,0]', 'X[:, 1]', 'y'))
print('-'*26)
for i in range(10):
print('{:8.0f}{:8.0f}{:10.0f}'.format(X[i, 0], X[i, 1], y[i]))

#Normalisation
def featureNormalize(X):
X_norm = X.copy()
mu = np.zeros(X.shape)
sigma = np.zeros(X.shape)
mu=mu+(np.mean(X,axis=0))
sigma=sigma+(np.std(X,axis=0))
X_norm= (X_norm - mu)/ sigma
return X_norm

X_norm=featureNormalize(X)
X=np.concatenate([np.ones((m,1)), X_norm],axis=1)
print(X)

#Cost Function
def computeCostMulti(X, y, theta):
m=y.size
J=0
temp=np.dot(X,theta)
J=(1/(2*m))*sum((y-temp)*(y-temp))
return J

#Gradient Descent
def gradientDescentMulti(X, y, theta, alpha, num_iters):
m = y.shape[0]
theta = theta.copy()
J_history = []
for i in range(num_iters):
temp=np.dot(X,theta)-y
temp0=theta[0]-((alpha/m)*sum(temp))
temp1=theta[1]-((alpha/m)*sum(temp*X[:,1]))
temp2=theta[2]-((alpha/m)*sum(temp*X[:,2]))
theta=np.array([temp0,temp1,temp2])
J_history.append(computeCostMulti(X, y, theta))
return theta,J_history

alpha = 0.1
num_iters = 400

# init theta and run gradient descent
theta = np.zeros(3)
theta, J_history = gradientDescentMulti(X, y, theta, alpha, num_iters)

plt.plot(np.arange(len(J_history)), J_history, lw=2)
plt.xlabel('Number of iterations')
plt.ylabel('Cost J')
plt.show()

print('theta computed from gradient descent: {:s}'.format(str(theta)))