0
552views
Correlation and regression

Correlation and regression

abcd

1 Answer
0
5views

2) Solution : -

Let us assume that Prices (In Rs) are variable X and sales units are variable Y.


Calculation of Karl Pearson’s coefficient of correlation : -


enter image description here

Here,


$\sum X = 915$

$\sum y = 6850$


$X̂ = \frac{\sum X}{n} = \frac{915}{10} = 91.5$

$ŷ = \frac{\sum y}{n} = \frac{6850}{10} = 685$


a = X - X̂

b = y - ŷ


$\sum a*a = 244.5$

$\sum b*b = 74250$

$\sum ab = -3485$


$Now,\ coefficient\ of\ correlation,\ r\ =\ \frac{\sum ab}{\sqrt{\sum a*a}{\sum b*b}}$

$\therefore\ r\ =\ \frac{-3485}{\sqrt{244.5\ *\ 74250}} =\ -0.817928.\ Ans$


Calculating coefficient of correlation by Python in-built function : -


Input -

import numpy as np
x_simple = np.array([100,98,85,92,90,84,88,90,93,95])
y_simple = np.array([500,610,700,630,670,800,800,750,700,690])

ans = np.corrcoef(x_simple, y_simple)
print(ans)

Output -

[[ 1.         -0.81792809]
 [-0.81792809  1.        ]]




3). Solution -


import pandas as pd
d = pd.read_csv('book1.csv')
d

enter image description here

d.shape

(7,2)

import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(d['Age of Cars (in yrs)'], d['Annual Maintanence cost (Rs.)'])
plt.xlabel('Age')
plt.ylabel('Cost')

enter image description here

from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(d[['Age of Cars (in yrs)']], d['Annual Maintanence cost (Rs.)'])

LinearRegression()

model.predict([[3]])

array([1588.57142857])

model.coef_

array([52.85714286])

model.intercept_

1430.0


Here, Intercept is c and coefficient is m.


Here, As you can see the predicted maintenance cost of the 3 year old car is Rs.1588.5714. and the linear equation will will be y = mx + c = 52.85714286 * 3 + 1430 = 1588.55. Ans.

Please log in to add an answer.