Read 2 arrays x,y containing floating point values


In [2]:
import numpy as np
import pylab as pl
x=np.array([1,2,3,8,5,6,7,8],dtype=np.float32)
y=np.array([1,2,3,4,5,6,7,8],dtype=np.float32)
print("x : ",x)
print("y : ",y)


x :  [ 1.  2.  3.  8.  5.  6.  7.  8.]
y :  [ 1.  2.  3.  4.  5.  6.  7.  8.]

Calculate mean of x & y


In [3]:
mean1=np.mean(x)
mean2=np.mean(y)
print("Mean of x : ",mean1)
print("Mean of y : ",mean2)


Mean of x :  5.0
Mean of y :  4.5

Calculate variance for x variance(x)=sum((x-mean(x))^2)


In [7]:
var=0
for i in np.nditer(x.T):
        var+=np.sum(np.square(i-mean1))
variace=var/x.size
print("variance : ",variace)


variance :  6.5

In [ ]:

Calculate covariance of x & y covariance = sum((x(i) - mean(x)) * (y(i) - mean(y)))


In [8]:
if(x.size!=y.size):
    print('Array size is different ')
else:    
    covar=0.0
    for i in range(0,len(x)):
        covar+=((x[i]-mean1)*(y[i]-mean2))
    covariance=covar/(len(x)-1)
    print("covariance : ",covariance)


covariance :  5.71428571429

Calculate value of m m = covariance(x,y)/variance(x)$$


In [9]:
m=covariance/variace
print("value m : ",m)


value m :  0.879120879121

Calculate value of c cc = mean(y) -m* mean(x)


In [10]:
cc=mean2-m*mean1
print("value c : ", cc)


value c :  0.104395604396

PART B

Plot graph for actual values against predicted value


In [11]:
pl.plot(x,y)
    pl.title("Graph")
    pl.show()


Calculate root mean square error


In [12]:
rmse=np.sqrt(np.mean(np.square(x - y)))
print("Root mean square Error : ",rmse)


Root mean square Error :  1.41421

In [ ]: