<jupyter_start><jupyter_text>Linear RegressionLinear regression is usually the first basic machine learning algorithm. The main purpose of linear regression model is to find a relationship between one or more features(independent variables) and a continuous target variable(dependent variable). When there is only feature it is called Uni-variate Linear Regression and if there are multiple features, it is called Multiple Linear Regression. Implementing Linear Regression from scratch using Least Square method<jupyter_code>%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Generate random data-set
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2 + 3 * x + np.random.rand(100, 1)

# plotting the actual values as scatter plot
plt.scatter(x,y,s=10, label='Scatter Plot')

# Mean X and Y
mean_x = np.mean(x)
mean_y = np.mean(y)

# Total number of values
n = len(x)

# calculating cross-deviation and deviation about x
xx = 0
xy = 0
for i in range(m):
      xx += (x[i] - mean_x) * (y[i] - mean_y)
      xy += (x[i] - mean_x) ** 2

# calculating regression coefficients
b1 = xx / xy
b0 = mean_y - (b1 * mean_x)

print("Estimated Coefficients: ")
print("b0 = ", b0)
print("b1 = ", b1)

y_pred = b0+ b1 *x

# Plotting Line
plt.plot(x, y_pred, color='r', label='Regression Line')


plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()<jupyter_output>Estimated Coefficients: 
b0 =  [2.55808002]
b1 =  [2.93655106]<jupyter_text>Implementing Linear Regression using scikit-learn<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# generate random data-set
np.random.seed(0)
x = np.random.rand(100, 1)
y = 2 + 3 * x + np.random.rand(100, 1)

# sckit-learn implementation

# Model initialization
regression_model = LinearRegression()
# Fit the data(train the model)
regression_model.fit(x, y)
# Predict
y_predicted = regression_model.predict(x)

# model evaluation
rmse = mean_squared_error(y, y_predicted)
r2 = r2_score(y, y_predicted)

# printing values
print('Slope:' ,regression_model.coef_)
print('Intercept:', regression_model.intercept_)
print('Root mean squared error: ', rmse)
print('R2 score: ', r2)

# plotting values

# data points
plt.scatter(x, y, s=10)
plt.xlabel('x')
plt.ylabel('y')

# predicted values
plt.plot(x, y_predicted, color='r')
plt.show()<jupyter_output>Slope: [[2.93655106]]
Intercept: [2.55808002]
Root mean squared error:  0.07623324582875007
R2 score:  0.9038655568672764
{'fraction_non_alphanumeric': 0.11271450858034321, 'fraction_numerical': 0.04563182527301092, 'mean_word_length': 5.107142857142857, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '160942', 'license': 'MIT', 'n_tokens_mistral': 998, 'n_tokens_neox': 900, 'n_words': 273, 'path': 'ML Algorithms/LinearRegression.ipynb', 'repo_name': 'Rohit-varma/Avid-Algorithms', 'size': '2564'}