-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfb32f5
commit 90b34ab
Showing
13 changed files
with
697 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Mon Feb 26 14:33:05 2018 | ||
@author: sanath | ||
""" | ||
|
||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Mon Feb 26 13:54:34 2018 | ||
@author: sanath | ||
""" | ||
|
||
#using the formula method | ||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
|
||
#fit 1st order curve for x and y | ||
|
||
#fit 1 st order straight line | ||
#based on formula method | ||
|
||
def a1(x,y): | ||
x=np.array(x) | ||
y=np.array(y) | ||
val=((len(x)*sum(x*y))-(sum(x)*sum(y)))/((len(x)*sum(x*x))-(sum(x)*sum(x))) | ||
return(val) | ||
def a0(x,y,res_a1): | ||
x=np.array(x) | ||
y=np.array(y) | ||
val=(sum(y)/len(y))-((res_a1*sum(x))/len(x)) | ||
return(val) | ||
|
||
|
||
#n=int(input("Enter number of variables in X ")) | ||
#x = [float(x) for x in input().split()] | ||
#y = [float(x) for x in input().split()] | ||
|
||
|
||
|
||
dfs=pd.read_excel("Activity_2_Data.xlsx",sheetname="Sheet1") | ||
x=np.array(dfs.iloc[0:,0]) | ||
#print(x) | ||
|
||
|
||
#print(len(x)) | ||
y=np.array(dfs.iloc[0:,4]) | ||
#print(y) | ||
#print(len(y)) | ||
|
||
|
||
plt.plot(x,y) | ||
plt.show() | ||
|
||
xlen=len(x) | ||
ylen=len(y) | ||
plt.figure(1) | ||
plt.plot(x,y) | ||
plt.show() | ||
if xlen!=ylen: | ||
print("Enter equal number of samples for both x and y") | ||
quit() | ||
res_a1=a1(y,x) | ||
res_a0=a0(x,y,res_a1) | ||
print("y =",res_a0,'+',res_a1,'x') | ||
|
||
|
||
x_new=np.arange(1,101) | ||
#print(x_new) | ||
y_new=[] | ||
|
||
for ele in x_new: | ||
#print(ele) | ||
#print(res_a0+(res_a1*ele)) | ||
y_new.append(res_a0+(res_a1*ele)) | ||
#print(y_new) | ||
plt.figure(2) | ||
plt.plot(x,y) | ||
plt.plot(x_new,y_new) | ||
plt.show() | ||
|
||
pred_x=float(input("Enter the value to be predicted:- ")) | ||
pred_y=(res_a0+(res_a1*pred_x)) | ||
print("Predicted value for ",pred_x," is ", pred_y) | ||
|
||
""" | ||
x=np.array(x) | ||
y=np.array(y) | ||
x_first=[] | ||
for i in range(xlen): | ||
x_first.append(res_a0+(res_a1*x[i])) | ||
x_first=np.array(x_first) | ||
plt.figure(2) | ||
plt.plot(y,x_first) | ||
plt.show() | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Mon Mar 12 17:17:17 2018 | ||
@author: sanath | ||
""" | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
|
||
dfs=pd.read_excel("Activity_2_Data.xlsx",sheetname="Sheet1") | ||
x=np.array(dfs.iloc[0:,0]) | ||
y=np.array(dfs.iloc[0:,4]) | ||
plt.figure(1) | ||
plt.xlabel("X") | ||
plt.ylabel("Y") | ||
plt.plot(x,y) | ||
plt.show() | ||
|
||
m=len(x) | ||
|
||
X=np.array(x) | ||
Y=np.array(y) | ||
|
||
|
||
theta0=100 | ||
init_theta0=theta0 | ||
theta1=25 | ||
init_theta1=theta1 | ||
m=len(X) | ||
alpha=0.0001 | ||
|
||
print("Learning rate is ",alpha) | ||
print("Number of samples or data is",m) | ||
count=0 | ||
while(True): | ||
cost_J0=0 | ||
cost_J1=0 | ||
for i in range(m): | ||
h_x=theta0+theta1*X[i] | ||
cost_J0=cost_J0+(h_x-Y[i]) | ||
cost_J1=cost_J1+((h_x-Y[i])*X[i]) | ||
|
||
count+=1 | ||
#uncomment to see variation of theta | ||
#print("iteration",count,"theta0",theta0,"theta1",theta1) | ||
|
||
|
||
|
||
temp0=theta0-((alpha*cost_J0)/m) | ||
temp1=theta1-((alpha/m)*cost_J1) | ||
|
||
if((np.abs(temp0-theta0)<0.00001)and(np.abs(temp1-theta1)<0.00001)): | ||
break | ||
theta0=temp0 | ||
theta1=temp1 | ||
|
||
h=[] | ||
for i in range(m): | ||
h.append(theta1*X[i]+theta0) | ||
|
||
h=np.array(h) | ||
plt.figure(2) | ||
plt.xlabel("X") | ||
plt.ylabel("Hypothesis") | ||
plt.plot(X,Y) | ||
plt.plot(X,h) | ||
plt.show() | ||
|
||
|
||
|
||
|
||
print ("Assumed theta0 is ",init_theta0,"predicted theta0 is ",theta0) | ||
print("Assumed theta1 is ",init_theta1,"predicted theta1 is ",theta1) | ||
test_x=float(input("Enter a test sample:")) | ||
predicted_y=(test_x*theta1)+theta0 | ||
#implementation of gradient descent for 1 st order curve(straight line) | ||
|
||
print ("The value of predicted y is",predicted_y) | ||
|
||
print("Importance feature scaling ,here you can see that the number of iteration it took to calculate the theta0 and theta1 are ",count," iterations ,so here if we scale down the x and y values to certain range i.e by normalisation,we can reduce the number of iterations it took to calculate the theta0 and theta1 ,or the other way is to judge the random theta0 and theta1 values by seeing the plot which are very close to each other ,so by this also we can reduce the number of iterations and computing time of the program") | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
""" | ||
#dummy code (test code) | ||
theta0=4 | ||
theta1=3 | ||
m=len(X) | ||
alpha=0.001 | ||
while(True): | ||
cost_J0=0 | ||
cost_J1=0 | ||
for i in range(m): | ||
h_x=theta0+theta1*X[i] | ||
cost_J0=cost_J0+(h_x-Y[i]) | ||
cost_J1=cost_J1+((h_x-Y[i])*X[i]) | ||
temp0=theta0-((alpha*cost_J0)/m) | ||
temp1=theta1-((alpha/m)*cost_J1) | ||
if((np.abs(temp0-theta0)<0.01)and(np.abs(temp1-theta1)<0.01)): | ||
break | ||
theta0=temp0 | ||
theta1=temp1 | ||
h=[] | ||
for i in range(m): | ||
h.append(theta1*X[i]+theta0) | ||
h=np.array(h) | ||
plt.xlabel("X") | ||
plt.ylabel("Hypothesis") | ||
plt.plot(X,h) | ||
print ("theta0={0} theta1={1}").format(theta0,theta1) | ||
test_x=input("Enter a test sample:" ) | ||
predicted_y=(test_x*theta1)+theta0 | ||
print ("The value of predicted y is"),predicted_y | ||
""" | ||
|
||
|
||
""" | ||
# | ||
for j in range(m): | ||
res=res+((theta0+theta1*x[j])-y[j]) | ||
new_theta0=theta0-((alpha/m)*(res)) | ||
print("Assumed Theta0 is ",theta0," New theta1 is",new_theta0 ) | ||
for j in range(m): | ||
res_theta1=res_theta1+((theta0+theta1*x[j])-y[j])*x[j] | ||
new_theta1=theta1-((alpha/m)*res_theta1) | ||
print("Assumed Theta1 is ",theta1,"new theta1 is",new_theta1) | ||
hyp=[] | ||
for ele in x: | ||
hyp.append((new_theta0+(new_theta1*ele))) | ||
hyp=np.array(hyp) | ||
plt.figure(2) | ||
plt.plot(x,y) | ||
plt.plot(x,hyp) | ||
plt.show() | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Tue Feb 27 14:52:23 2018 | ||
@author: sanath | ||
""" | ||
|
||
|
||
|
||
#ssd implementation for theta0=0 and alpha(learning rate=1) | ||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
|
||
dfs=pd.read_excel("Activity_2_Data.xlsx",sheetname="Sheet1") | ||
x=np.array(dfs.iloc[0:,0]) | ||
#x=[1,2,3,4,5] | ||
y=np.array(dfs.iloc[0:,4]) | ||
#y=[1,2,3,4,5] | ||
plt.figure(1) | ||
plt.plot(x,y) | ||
plt.show() | ||
|
||
range_of_theta=[-10,10] | ||
number_of_samples=50 | ||
theta=np.linspace(range_of_theta[0],range_of_theta[1],number_of_samples) | ||
min_cost=999999 | ||
cost_array=[] | ||
m=len(y) | ||
for i in range(number_of_samples): | ||
cost_j=0 | ||
|
||
for j in range(m): | ||
cost_j=cost_j+(theta[i]*x[j]-y[j])**2 | ||
cost_j==1/(2.0*m)*cost_j | ||
cost_array.append(cost_j) | ||
|
||
|
||
if cost_j<min_cost: | ||
min_cost=cost_j | ||
theta_hyp = theta[i] | ||
index_min=i | ||
h=[] | ||
for i in range(m): | ||
h.append(theta_hyp*x[i]) | ||
|
||
plt.figure(1) | ||
plt.plot(theta,cost_array) | ||
plt.scatter(theta,cost_array) | ||
plt.show() | ||
|
||
|
||
plt.figure(2) | ||
plt.plot(theta,cost_array,'g',theta_hyp,min_cost,'rd') | ||
plt.show() | ||
|
||
plt.figure(3) | ||
print("Predicted curve vs actual curve") | ||
plt.xlabel("X") | ||
plt.ylabel("PREDECTED Y") | ||
plt.plot(x,y) | ||
#plt.scatter(x,y) | ||
plt.plot(x,h) | ||
plt.show() | ||
|
||
test_x=float(input("Enter the test value for x :-")) | ||
print("predicted hyphothesis value is :-",test_x*theta_hyp) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.