x = np.array([1,2,3,4,5,6,7,8,9])
y = np.array([5,4,5,6,8,9,10,13,12])
xy = x*y
n = len(x)
# mean
x_mean = sum(x)/n
y_mean = sum(y)/n
#variance
x_var = sum(x**2)/n - x_mean**2
y_var = sum(y**2)/n - y_mean**2
# Covariance
covar = sum(xy)/n - x_mean*y_mean
# Correlation Coefficient
r = covar / (np.sqrt(x_var)*np.sqrt(y_var))
if(r > 0):
print("positive correlation")
elif (r < 0):
print("negative correlation")
else:
print("no correlation")
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.