In [74]:
import tensorflow as tf

x=tf.constant([1.2, 2.1, 2.3, 4.1,5.3, 6.1])
y=tf.constant([1.3, 2.1, 3.2,3.3, 5.4,6.3])

print('Elements in array x')
with tf.name_scope ("ElementsofArray_X"):
    with tf.Session() as sess:
        for i in range(len(sess.run(x))):
            op1 = sess.run(x[i])
            print(op1)
    
print('Elements in array y')
with tf.name_scope ("ElementsofArray_Y"):
    with tf.Session() as sess:
        for i in range(len(sess.run(y))):
            op2 = sess.run(y[i])
            print(op2)


Elements in array x
1.2
2.1
2.3
4.1
5.3
6.1
Elements in array y
1.3
2.1
3.2
3.3
5.4
6.3

In [75]:
#calculate mean of x and y
sum1=tf.constant(0)

with tf.name_scope("Mean_X"):
    with tf.Session() as sess:
        for i in range(len(sess.run(x))):
            op3 =sess.run(x[i])
            sum1=tf.add(sum1,op3, name="AddX")
        a=tf.divide(sum1,len(sess.run(x)), name="DivideByN")
        avgx=sess.run(a)
    print('Mean of x')
    print(avgx)

sum1=tf.constant(0)
with tf.name_scope("Mean_Y"):
    with tf.Session() as sess:
        for i in range(len(sess.run(y))):
            op4 =sess.run(y[i])
            sum1=tf.add(sum1,op4, name="AddY")
        a=tf.divide(sum1,len(sess.run(y)), name="DivideByN")
        avgy=sess.run(a)
    print('Mean of y')
    print(avgy)


Mean of x
3.33333333333
Mean of y
3.33333333333

In [76]:
#Calculate variance for x
var=tf.constant(0.0)
with tf.name_scope("Calculate_Variance"):
    with tf.Session() as sess:
        for i in range(len(sess.run(x))):
            a=tf.subtract(sess.run(x[i]), avgx, name="Sub")
            b=tf.square(a, name="Squaring")
            var=tf.add(var,b, name="Add")
        vari=sess.run(var)
print('Variance of x')
print(vari)


Variance of x
19.25

In [77]:
#Calculate covariance of x & y
cov=tf.constant(0.0)
with tf.name_scope("Calculate_Covariance"):
    with tf.Session() as sess:
        for i in range(len(sess.run(x))):
            a=tf.subtract(sess.run(x[i]),avgx, name="Subtract1")
            b=tf.subtract(sess.run(y[i]),avgy, name="Subtract2")
            c=tf.multiply(a,b,name="Multiply")
            cov=tf.add(cov,c, name="AddThem")
        covar=sess.run(cov)
print('Covariance of x,y')
print(covar)


Covariance of x,y
18.2433

In [78]:
#Calculate value of m
with tf.name_scope("Calculate_slope"):
    with tf.Session() as sess:
        m=tf.divide(cov,var)
        slope=sess.run(m)
print('Value of slope')
print(slope)


Value of slope
0.947706

In [79]:
#Calculate value of c 
with tf.name_scope("Calculate_c"):
    c=tf.to_float(slope)
    d=tf.to_float(avgx)
    e=tf.multiply(c,d)
    f=tf.to_float(avgy)
    s=tf.subtract(f,e)
    with tf.Session() as sess:
        const_c=sess.run(s)
print('The value of c')
print(const_c)


The value of c
0.174314

In [80]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

In [81]:
with tf.Session() as sess:
    x1=sess.run(x)
    y1=sess.run(y)
tr_X = numpy.asarray(x1)
tr_Y = numpy.asarray(y1)


randomX=tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
with tf.Session() as sess:
    randomX1=sess.run(randomX)
randomX2=numpy.asarray(randomX1)

randomY=[0]*6
    
with tf.Session() as sess:
        for i in range(len(sess.run(randomX))):
            output = sess.run(randomX[i])
            a=tf.multiply(output,slope)
            b=tf.add(a,const_c)
            randomY[i]=sess.run(b)
print(randomY)


[1.12202, 2.0697258, 3.0174315, 3.9651372, 4.9128428, 5.860549]

In [82]:
plt.plot(tr_X, tr_Y, 'ro', label='Original data')
plt.plot(randomX2, randomY, label='fitted line')
plt.legend()
plt.show()



In [83]:
#part2(b)
import numpy as np
xnp=np.asarray(randomX2)
ynp=np.asarray(randomY)

graph=np.polyfit(xnp,ynp,1)
fitfunc=np.poly1d(graph)

plt.plot(xnp,ynp, 'yo',xnp, fitfunc(xnp), '--k')
plt.show()
with tf.name_scope("Root_mean_square_error"):
    rmse=tf.sqrt(tf.reduce_mean(tf.squared_difference(randomX2,randomY)))
    
    with tf.Session() as sess:
        print(sess.run(rmse))


0.0897337

In [ ]: