Assignment 2 Part1


In [31]:
x = [1.1,4.3,7.4,3.45,2.54,3.3,9.7,6.77,0.65,4.22]
y = [3.2,6.66,3.43,0.06,3.44,2.2,3.43,2.32,4.11,1.65]
sumofx = sum(x)
sumofy = sum(y)

#Mean
meanx = (sumofx/len(x))
meany = (sumofy/len(y))
print("Mean of x is :",meanx)
print("Mean of y is :",meany)

#Variance
varofx = sum([(xi - meanx)**2 for xi in x])/len(x)
print("Variance of x is :",varofx)

#Covariance
cov = 0
for i in range(0, len(x)):
    cov += ((x[i] - meanx)*(y[i] - meany))

print("Covariance is :", cov)

#Value of m
valm = cov/varofx
print("Value of m is :",valm)

#Value of c
valc = (meany - valm * meanx)
print("Value of c is :",valc)


Mean of x is : 4.343
Mean of y is : 3.05
Variance of x is : 7.324141
Covariance is : -0.10500000000000204
Value of m is : -0.014336152184945926
Value of c is : 3.11226190893922

In [63]:
##USING TENSORFLOW
import numpy as np
import tensorflow as tf
arr1=np.asarray([1.1,4.3,7.4,3.45,2.54,3.3,9.7,6.77,0.65,4.22])
arr2=np.asarray([3.2,6.66,3.43,0.06,3.44,2.2,3.43,2.32,4.11,1.65])


x = tf.constant(arr1,dtype = tf.float32)
y = tf.constant(arr2,dtype = tf.float32)


ses=tf.Session()
print( ses.run(x))
ses=tf.Session()
print( ses.run(y))

#Mean
meanx=tf.reduce_mean(x)
with tf.Session() as ses:
    ans = ses.run(meanx)
    print("Mean of x is :",ans)
meany=tf.reduce_mean(y)
with tf.Session() as ses:
    ans = ses.run(meany)
    print("Mean of y is :",ans)
    
    
#Variance
mean,var = tf.nn.moments(x, axes=[0])
with tf.Session() as ses:
    ans1 = ses.run(mean)
    ans2=ses.run(var)
    print("Mean of x is :",ans)
    print("Variance of  is :",ans2)
    

#Covariance
xi=tf.subtract(x,meanx)
with tf.Session() as ses:
    ans = ses.run(xi)
    print(ans)
    
yi=tf.subtract(y,meany)
with tf.Session() as ses:
    ans = ses.run(yi)
    print(ans)
    
mult=tf.multiply(xi,yi)
with tf.Session() as ses:
    ans = ses.run(mult)
    print(ans)
    
    
val=tf.reduce_sum(mult)
with tf.Session() as ses:
    ans = ses.run(val)
    print(ans)
    
lenn=len(arr1)

cov=tf.divide(val,lenn)
with tf.Session() as ses:
    ans = ses.run(cov)
    print("Covariance is :",ans)
    
    
#Value of m
m=tf.divide(cov,var)
with tf.Session() as ses:
    ans = ses.run(m)
    print("The value of m is :",ans)
    
    
#Value of c
a=tf.multiply(m,meanx)
c=tf.subtract(meany,a)
with tf.Session() as ses:
    ans = ses.run(c)
    print("The value of c is :",ans)


[ 1.10000002  4.30000019  7.4000001   3.45000005  2.53999996  3.29999995
  9.69999981  6.76999998  0.64999998  4.21999979]
[ 3.20000005  6.65999985  3.43000007  0.06        3.44000006  2.20000005
  3.43000007  2.31999993  4.11000013  1.64999998]
Mean of x is : 4.343
Mean of y is : 3.05
Mean of x is : 3.05
Variance of  is : 7.32414
[-3.24299955 -0.04299927  3.05700064 -0.89299941 -1.8029995  -1.04299951
  5.35700035  2.42700052 -3.69299936 -0.12299967]
[ 0.1500001   3.6099999   0.38000011 -2.99000001  0.3900001  -0.8499999
  0.38000011 -0.73000002  1.06000018 -1.39999998]
[-0.48645025 -0.15522735  1.16166055  2.67006826 -0.70317     0.88654947
  2.03566074 -1.7717104  -3.91458011  0.17219953]
-0.105
Covariance is : -0.0105
The value of m is : -0.00143361
The value of c is : 3.05623

Part 2


In [65]:
import numpy as np
import matplotlib.pyplot as plt
learning_rate = 0.0001
training_epochs = 1000
display_step = 50

n_samples = len(arr1)
rangee = np.random
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set model weights
W = tf.Variable(rangee.randn(), name="weight")
b = tf.Variable(rangee.randn(), name="bias")

# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)


# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (p, r) in zip(arr1, arr2):
            sess.run(optimizer, feed_dict={X: p, Y: r})

        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: arr1, Y: arr2})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "W=", sess.run(W), "b=", sess.run(b))

    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={X: arr1, Y: arr2})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

    # Graphic display
    plt.plot(arr1, arr2, 'ro', label='Original data')
    plt.plot(arr1, sess.run(W) * arr1 + sess.run(b), label='Fitted line')
    plt.legend()
    plt.show()


Epoch: 0050 cost= 35.913913727 W= -1.04378 b= -0.239471
Epoch: 0100 cost= 28.055017471 W= -0.849139 b= -0.202607
Epoch: 0150 cost= 22.050054550 W= -0.679102 b= -0.169871
Epoch: 0200 cost= 17.461471558 W= -0.530565 b= -0.140744
Epoch: 0250 cost= 13.955011368 W= -0.400821 b= -0.114773
Epoch: 0300 cost= 11.275259018 W= -0.287502 b= -0.0915602
Epoch: 0350 cost= 9.227122307 W= -0.188541 b= -0.0707595
Epoch: 0400 cost= 7.661532402 W= -0.102128 b= -0.0520675
Epoch: 0450 cost= 6.464608192 W= -0.0266843 b= -0.0352195
Epoch: 0500 cost= 5.549342155 W= 0.039173 b= -0.0199839
Epoch: 0550 cost= 4.849264145 W= 0.096651 b= -0.00615831
Epoch: 0600 cost= 4.313593864 W= 0.146805 b= 0.00643409
Epoch: 0650 cost= 3.903532505 W= 0.190558 b= 0.017948
Epoch: 0700 cost= 3.589437962 W= 0.228715 b= 0.0285184
Epoch: 0750 cost= 3.348665714 W= 0.261982 b= 0.0382636
Epoch: 0800 cost= 3.163916111 W= 0.290975 b= 0.0472868
Epoch: 0850 cost= 3.021969318 W= 0.316232 b= 0.0556782
Epoch: 0900 cost= 2.912728786 W= 0.338223 b= 0.0635167
Epoch: 0950 cost= 2.828475714 W= 0.357361 b= 0.0708713
Epoch: 1000 cost= 2.763319254 W= 0.374004 b= 0.0778023
Optimization Finished!
Training cost= 2.76332 W= 0.374004 b= 0.0778023 


In [ ]: