In [22]:
import tensorflow as tf
with tf.name_scope("var"):
with tf.name_scope("mean_x"):
a=tf.constant([5.0,7.0,20.2,17.32],shape=[1,4],name='a')
b=tf.constant([7.0,9.0,19.0,18.0],shape=[1,4],name='b')
x=tf.reduce_mean(a)
sess=tf.Session()
print("mean X",sess.run(x))
#mean of y
with tf.name_scope("mean_y"):
y=tf.reduce_mean(b)
sess=tf.Session()
print("mean Y",sess.run(y))
#variance of x
d=tf.subtract(a,x)
sess=tf.Session()
print(sess.run(d))
e=tf.square(d)
f=tf.reduce_sum(e)
sess=tf.Session()
print("variance",sess.run(f))
#covariance
with tf.name_scope("covariance"):
g=tf.subtract(b,y)
sess=tf.Session()
g=tf.multiply(d,g)
h=tf.reduce_sum(g)
print("covariance",sess.run(h))
#value of c
with tf.name_scope("value_of_c"):
j=tf.divide(h,f)
print("value c",sess.run(j))
#m value
with tf.name_scope("value_m"):
i=tf.multiply(j,x)
k=tf.subtract(y,i)
print("value m",sess.run(j))
#calculate root mean square error
with tf.name_scope("rmse"):
l=tf.subtract(b,y)
m=tf.multiply(l,l)
n=tf.reduce_sum(m)
o=tf.divide(n, 4,)
p=tf.sqrt(o)
print ("rmse",sess.run(p))
with tf.Session() as lb:
Writer =tf.summary.FileWriter("/tmp/tboard/output6",lb.graph)
Writer.close()
In [19]:
#calculate y using x
import numpy
with tf.Session() as sess:
x1=sess.run(a)
y1=sess.run(b)
g_x = numpy.asarray(x1)
g_y = numpy.asarray(y1)
x2=tf.constant([1.0,2.0, 3.0, 4.0, 5.0])
with tf.Session() as sess:
x3=sess.run(x2)
x4=numpy.asarray(x3)
y2=[0]*5
with tf.Session() as sess:
for i in range(len(sess.run(x2))):
xx = sess.run(x2[i])
a=tf.multiply(xx,j)
b=tf.add(a,k)
y2[i]=sess.run(b)
print(y2)
In [20]:
#graph
import matplotlib.pyplot as plt
plt.plot(g_x,g_y, 'ro', label='Actual values')
plt.plot(x4, y2, label='fitted line')
plt.legend()
plt.show()
In [ ]: