<jupyter_start><jupyter_text>**Simple Linear Regression** Regression is the concept in Machine Learning where we want to find relationship among the variables. **Linear Regression**Simplest regression method is the most widely used and it's easy to interpret its results. To solve this problem we need to understand the Model.For any given predictor/input ${x}$ $\epsilon$ $\mathbb{R}$ the simple linear regression model predicts/outputs$\hat{Y}(x)$ := $\hat{\beta}_0 +\hat{\beta}_{x}$ with the paramters ($\hat{\beta}_0 ,\hat{\beta}_{x}$) called $\hat{\beta}_0$ Inteercept / Bias $\hat{\beta}_1$ SlopeTo find the Least Square Estimate it can be written in the closed form as follows: $\hat{\beta}_1$ = $\dfrac{\Sigma_{n=1}^{N}({x_n} - {\bar{x}})({y_n} - {\bar{y}})}{\Sigma_{n=1}^{N}({x_n} - {\bar{x}})^2}$$\hat{\beta}_0$ = $\bar{y}$ - ${\beta}_1{\bar{x}}$  **Algorithm 1: Learning Procedure**1. **Procedure**  Learn-simple-linreg $(D^{train}:= \{(x_1,y_1),...,(x_n,y_n)\}$ $\epsilon$ $\mathbb{R}$ x $\mathbb{R}$)2. $\hspace{2em}$ $\bar{x} := \frac{1}{N}\sum_{n=1}^{N}{x_n}$3. $\hspace{2em}$ $\bar{y} := \frac{1}{N}\sum_{n=1}^{N}{y_n}$4. $\hspace{2em}$ $\hat{\beta}_1$ = $\dfrac{\Sigma_{n=1}^{N}({x_n} - {\bar{x}})({y_n} - {\bar{y}})}{\Sigma_{n=1}^{N}({x_n} - {\bar{x}})^2}$5. $\hspace{2em}$ $\hat{\beta}_0$ = $\bar{y}$ - ${\beta}_1{\bar{x}}$6. $\hspace{2em}$ **return** ($\hat{\beta}_0$, $\hat{\beta}_1$)**Algorithm 2: Prediction Procedure**1. **Procedure** Predict-simple-linreg(x $\epsilon$ $\mathbb{R}$,$\hat{\beta}_0$, $\hat{\beta}_1$ $\epsilon$ $\mathbb{R}$)2. $\hspace{2em}$ $\hat{y}$ := $\hat{\beta}_0 +\hat{\beta}_{x}$ 3. $\hspace{2em}$ **return** $\hat{y}$<jupyter_code># We are going to create a normal Distributed Matrix of size 100,2
A = np.random.normal(2,0.01,size=(100,2))
print(A)
#Now we will split the data inot X and Y variables respectively.
def split():
  x,y = A[:,0],A[:,1]
  return x, y
X,Y = split()
# Now we will perform the Learning algorithm
def learnsimplelinreg(X,Y):
  X_bar = np.mean(X)
  Y_bar = np.mean(Y)

#Now calcuate Beta1 as follows
  Beta_1 = (np.sum(np.multiply((X-X_bar),(Y-Y_bar))))/(np.sum(np.square(X-X_bar)))

#Now lets Calcualte Beta0 as follows
  Beta_0 = Y_bar - np.multiply(Beta_1,X_bar)

  return (Beta_0,Beta_1)

Beta0, Beta1 = learnsimplelinreg(X,Y)
#Now we will perform the Predicting Algorithm
def predictsimplelinreg(X,Beta0,Beta1):
  Y_predict =[(Beta1*x) + Beta0 for x in X]
  return Y_predict
y_predict = predictsimplelinreg(X,Beta0,Beta1)
print(y_predict)<jupyter_output>[2.0032456404860364, 2.001344647472088, 1.9974795273883303, 1.9982661479358885, 2.0014505050507543, 1.9985450305042551, 1.9988094843029625, 2.001636215479062, 2.0001895341328026, 1.9983120080389927, 1.9988081355765208, 1.9993197886114238, 2.000889328953526, 1.9988349816070579, 1.9998555662141821, 1.9981276359216604, 1.9971047205504813, 1.998618043522053, 1.9988007779103278, 1.9992932787548419, 1.9995309996291493, 1.9992020341269776, 1.9992545999023565, 1.9986411115475706, 1.9947669564634933, 1.9986450748746165, 1.997593622499972, 2.0006138613379205, 2.002383209093731, 1.999329029054587, 2.00083746927438, 2.001708878016048, 2.0002987226421136, 1.9975378420979164, 2.0013359001277196, 1.9993338850757456, 1.9985449030702584, 2.0003377906025253, 1.9992678658017067, 2.0013619715306623, 2.002425921503425, 2.0021524349286026, 1.9954418277969355, 1.9997189999773806, 1.9981589606093597, 2.002748546386158, 2.001250417194102, 1.9998869450792487, 2.000199778187925, 1.9987219052368979, 2.00179771033[...]<jupyter_text>**Plotting**We are going to plot the graph for the actual values as well as the actual and the predicted values.<jupyter_code>plt.plot(X,Y, 'o',label="Given Data", markersize=7)
plt.xticks(size=10) # gives the size of the x values on the axis
plt.yticks(size=10) # gives the size of the y values on the y axis 
plt.plot(X,y_predict, color="red",label="Fitted Line")
plt.ylabel("Y Axis-values")
plt.xlabel("X Axis-values")
plt.title("Linear Regression Graph ")
plt.legend(loc="left") #it plots the legend for the graph 
plt.show()# it is used to physically show the output of the graph

# We are going tom create a matrix of length of X with all values as 1.

M = np.vstack([X,np.ones(len(X))]).T
# Now we will again predict the Beta0 and Beta1 values as follows with the matrix we created  using the predifined function
B1,B0=np.linalg.lstsq(M,Y)[0]
print(M)

#plotting of the graph using predefined function
plt.plot(X, Y, 'o', label='Given Data', markersize=7)
plt.plot(X, [Beta1*x + Beta0 for x in X], 'r', label='Fitted line')
plt.xlabel("X Axis- Values")
plt.ylabel("Y Axix-Values")
plt.title("Linear Regression Graph using Predefined Function")
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17833859095688748, 'fraction_numerical': 0.1968454258675079, 'mean_word_length': 5.746099290780142, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '187377', 'license': 'Apache-2.0', 'n_tokens_mistral': 2451, 'n_tokens_neox': 1873, 'n_words': 435, 'path': 'Simple Linear Regression/Simple_Linear_Regression_.ipynb', 'repo_name': 'vijitVM/Machine-Learning-', 'size': '4755'}