<jupyter_start><jupyter_text>Linear Regression Using Basic Python **1. x = [2, 3, 4, 5.6], y = [12.8978, 17.7586, 23.3192, 28.3129, 32.1351]&emsp;Apply Linear regressison using basic python**<jupyter_code>import pandas as pd
from scipy.stats import t
data=pd.DataFrame({'x':[2,3,4,5,6],'y':[12.8978,17.7586,23.3192,28.3129,32.1351]})

data<jupyter_output><empty_output><jupyter_text>Here **x** is the independent varaiable and **y** is the dependent varaible Finding Straight line for x and yFormula for finding straight line is `y=mx+c`where ${m} = \frac{{n}\sum{xy} - \sum{x}\sum{y}}{n\sum{x^2}-(\sum{x})^2}$&emsp;&emsp;&nbsp;&nbsp;&nbsp; $c = \bar {y} - {m \bar{x}}$<jupyter_code>data['x**2']=data['x']**2
data['x*y']=data['x']*data['y']
data
#### Length of the dataset
n=data.shape[0]
#### Calculating 'm' value
m=round(((n*data['x*y'].sum())-(data['x'].sum()*data['y'].sum()))/((n*data['x**2'].sum())-(data['x'].sum())**2), 3)
#### Calculating 'c' value
c = round(data['y'].mean()-(m*data['x'].mean()), 3)
#### Printing straight line for x and y
print(f'The straight line equation for y(dependent variable) and x(independent variable) is y={m}x+{c}')<jupyter_output>The straight line equation for y(dependent variable) and x(independent variable) is y=4.903x+3.273<jupyter_text>Calculating R-Squared and Adj.R-SquaredFormula for &emsp;&emsp; **R-squared ${R^2} = {1}-{\frac{\sum(\hat y - y)^2}{\sum(y - \bar y)^2}}$ &emsp;&emsp; Adj.R-squared ${Adj.R^2} = {1}-{\frac{(1-R^2)(n-1)}{n-p-1}}$**Where ${\hat y}$ is **Predicted Values** of y&emsp;&emsp;&nbsp;&nbsp;&nbsp; ${y}$ is **Actual Values** of y&emsp;&emsp;&nbsp;&nbsp;&nbsp; ${n}$ is No.of **rows** in the dataset&emsp;&emsp;&nbsp;&nbsp;&nbsp; ${p}$ is No.of **independent variables** in the dataset<jupyter_code>data['ycap']=3.273+(4.903*data['x'])
data['(ycap-y)**2']=(data['ycap']-data['y'])**2
data['(y-ybar)**2'] = (data['y']-data['y'].mean())**2
data
#### Calculating R-squared
R_squared = 1-(data['(ycap-y)**2'].sum()/data['(y-ybar)**2'].sum())
#### Calculating Adjusted R-squared
n = data.shape[0]
Adj_R_squared =1-(((1-R_squared)*(n-1))/(n-2))
#### Printing R-squared and Adjusted R-squared
print(f'R-squared     : {round(R_squared,3)*100}')
print(f'Adj.R-squared : {round(Adj_R_squared,3)*100}')<jupyter_output>R-squared     : 99.6
Adj.R-squared : 99.5<jupyter_text>Calculating MAE, MSE, RMSEFormulas for &emsp;&emsp; **Mean Absolute Error ${MAE} = \frac{\sum|\hat y-y|}{n}$&emsp;&emsp; Mean Square Error ${MSE} = \frac{\sum (\hat y - y)^2}{n}$&emsp;&emsp; Root Mean Square Error ${RMSE} = \sqrt{\frac{\sum (\hat y - y)^2}{n}}$**<jupyter_code>data['|ycap-y|']=abs(data['ycap']-data['y'])
data
#### Calculating Mean Absolute Error 
MAE = data['|ycap-y|'].sum()/n
#### Calculating Mean Square Error
MSE = data['(ycap-y)**2'].sum()/n
#### Calculating Root Mean Square Error
RMSE = MSE**0.5
#### Print MAE, MSE, RMSE
print(f'MAE  : {round(MAE, 4)}')
print(f'MSE  : {round(MSE, 4)}')
print(f'RMSE : {round(RMSE, 4)}')<jupyter_output>MAE  : 0.3839
MSE  : 0.1712
RMSE : 0.4137<jupyter_text>Calculating Standard ErrorsStandard Error(SE)for&emsp;&emsp; **Estimator ${SE_e} = \frac{\sum(\hat y-y)^2}{n-p-1}$&emsp;&emsp; Intercept ${SE_i} = \sqrt{SE_e[\frac{1}{n}+\frac{\bar x^2}{\sum(x-\bar x)^2}]}$&emsp;&emsp; independent variable x ${SE_x} = \sqrt{\frac{SE_e}{\sum(x-\bar x)^2}}$**<jupyter_code>data['(x-xbar)**2'] = (data['x']-data['x'].mean())**2
data
#### Calculating standard error for Estimator
SE_e = data['(ycap-y)**2'].sum()/(n-2)
#### Calculating standard error for intercept
SE_i = (SE_e*((1/n)+(data['x'].mean()**2/data['(x-xbar)**2'].sum())))**0.5
#### Calculating standard error for x
SE_x = (SE_e/data['(x-xbar)**2'].sum())**0.5
#### Printing standard error for Estimator, intercept & x
print(f'standard error for Estimator : {round(SE_e, 3)}')
print(f'standard error for intercept : {round(SE_i, 3)}')
print(f'standard error for x         : {round(SE_x, 3)}')<jupyter_output>standard error for Estimator : 0.285
standard error for intercept : 0.717
standard error for x         : 0.169<jupyter_text>T-testT values for&emsp;&emsp; **critical ${t_{crit}} = {t_{\frac{\alpha}{2},df}}$&emsp;&emsp; intercept ${t_i} = \frac{c}{SE_i}$&emsp;&emsp; independent variable x ${t_x} = \frac{m}{SE_x}$**<jupyter_code>#### Calculating t critical
t_crit = t.ppf(0.975, n-2)
#### Calculating t value for intercept
t_i = c/SE_i
#### Calculating t value for x
t_x = m/SE_x
#### Printing t values for critical, intercept & x
print(f't critical            : {round(t_crit, 3)}')
print(f't value for intercept : {round(t_i, 3)}')
print(f't value for x         : {round(t_x, 3)}')<jupyter_output>t critical            : 3.182
t value for intercept : 4.568
t value for x         : 29.029<jupyter_text>Calculating P-Value<jupyter_code>#### Calculating p value for intercept
p_i = t.sf(t_i, n-2)*2
#### Calculating p value for x
p_x = t.sf(t_x, n-2)*2
#### Printing t values for intercept & x
print(f'p value for intercept : {round(p_i, 3)}')
print(f'p value for x         : {round(p_x, 3)}')<jupyter_output>p value for intercept : 0.02
p value for x         : 0.0<jupyter_text>Calculating Confidence Interval<jupyter_code>#### Calculating confidence interval for intercept
CI_i_Lower = round(c - (t_crit*SE_i), 3)
CI_i_Upper = round(c + (t_crit*SE_i), 3)
#### Calculating confidence interval for x
CI_x_Lower = round(m - (t_crit*SE_x), 3)
CI_x_Upper = round(m + (t_crit*SE_x), 3)
#### Printing confidence interval for intercept & x
print(f'confidence interval for intercept : [{CI_i_Lower}, {CI_i_Upper}]')
print(f'confidence interval for x         : [{CI_x_Lower}, {CI_x_Upper}]')<jupyter_output>confidence interval for intercept : [0.993, 5.553]
confidence interval for x         : [4.365, 5.441]
{'fraction_non_alphanumeric': 0.22517822987306554, 'fraction_numerical': 0.04190575552077899, 'mean_word_length': 5.52894438138479, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '226839', 'license': 'MIT', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2296, 'n_words': 497, 'path': 'Supervised/Regression/Linear Regression/LinearRegression_Using_Basic_Python.ipynb', 'repo_name': 'Naga-kalyan/Machine_Learning', 'size': '5751'}