<jupyter_start><jupyter_text>**Linear Regression**<jupyter_code>#We import the necessary libraries. Numpy and pandas for basic functions like vectors and reading data. 
import numpy as np                                               
import pandas as pd
import matplotlib.pyplot as plt                            #Matplotlib is used for plotting grpahs, histograms, etc.

from sklearn.linear_model import LinearRegression          #sklearn contians all pre-defined models, like linear regression, logistic regresion,etc.
from sklearn.metrics import r2_score                          

#Mount the Google drive which contains our dataset

from google.colab import drive          
drive.mount('/content/drive')
#read our dataset into a dataframe labelled data with the help of pandas. 
#We copy paste the path where our dataset was present in the drive
#Since our data is in a csv file we use pd.read_csv

data = pd.read_csv('/content/drive/My Drive/Height_weight Dataset.csv')
data                     #Print all the data
#Plot the graph using matplotlib 

plt.scatter(data['Height'],data['Weight'])        #plotting points on a graph 
plt.xlabel("Height")                              #Labelling the x and y axis
plt.ylabel("Weight")
plt.show()         
#We reshape the vector as when we read the data from csv we read it as a vector. 
#We reshape the vector so that we get a vector of 1 dimension only 

X = data['Height'].values.reshape(-1,1)
Y = data['Weight'].values.reshape(-1,1)<jupyter_output><empty_output><jupyter_text>The linear regression lgorithm is as follow:We **hypothesize y = mx + c** where m and c are randomly initialized. We generally take the **learning rate i.e. alpha as 0.01**. Many sophisticated algorithms change this learning rate too along with increasing number of epochs for the less training time. Epoch is one loop of all our data pointsNext we calculate the loss. We use the **mean squared Loss function** which is given by **L = (y - yP )^2/2 = (y-mx-c)^2/2** where y is the acual value in the dataset and yP is the predicted value according to our hypothesisNow we calculate the derivative of the loss functuion wrt to the parameters m and c. m is the weight of the feature and b is the bias taht we add. Here since we are dealing with only one feature we have only one weight but for multiple features we will have many weights assigned to each of them and sometimes assigned to a combination of the featuires too.  **dL/dm = dL/dy * dy/dw = -(y-yP)*x****dL/dc = dL/dy * dy/dc = -(y-yP)**Next we perform gradient descent. Gradient descent is basically updating the values of our weights and biases (i.e. m and c). This  is done by using the calculated derivatives. We introduce the learning rate (i.e. alpha) here and then update the values of m and c according to the below formula **m(new) = m - (alpha)*(dL/dm)****c(new) = c - (alpha)*(dL/dc)**Now using these updated values we calculate the y(predicted) for other instaces and the process continues. When all the instances ahve been gone through once, one epoch has been completed. Similarly **we run many epochs to get our required accuracy**.<jupyter_code>#initialize m and c randomly  
m=5
c=0
alpha=0.01            
epochs = 10000

def Linear_Regression(x,y,m,c,alpha):
    y_p = m*x + c                         #Our hypothesis for the regresssion 
    dL_dm = -(y - y_p)*x                   #Calculate the Loss wrt m i.e. dL/dm
    dL_dc = -(y - y_p)                     #Calculate the loss wrt c i.e. dL/dc
    m = m - alpha*(dL_dm)                 #Update m and c 
    c = c - alpha*(dL_dc)           
    return m,c                              

for i in range(epochs):                                    #First loop for the number of epochs 
  for j in range(15):                                      #Second loop for traversing through all the instantces
    m, c = Linear_Regression(X[j],Y[j],m,c,alpha)          #Calling linear regression function that we defined above
    #L=((Y[j]-m*X[j]-c)**2)/2
  if(i%1000==0):
    plt.scatter(data['Height'],data['Weight'])              #plotting the data points
    x_plot = data['Height']
    y_plot = m*(x_plot) + c                                 #predicting y from current m and c values
    plt.plot(x_plot,y_plot,color='green')                   #plotting regression line
    plt.show()

print(m)
print(c)

#Observation : If the m and c values are farther away from the actual values then we need more epochs to train
#Eg. if m=5 and c=0 we need 500000 epochs to train, for m=50 c=0 100000 are sufficient
reg = LinearRegression()         #Used the scikit Learn library containing the inbulit linear regression feature
reg.fit(X,Y)                     #Applied the linear regression on X i.e. height and Y i.e. weight.  

print(reg.coef_[0][0])
print(reg.intercept_[0])          #We print the m and the c values of the line that we obtain from linear regression

#We convert the numpy ndarray datatype into our float datatype for the values of M and C calculated by library 
M = np.float64(reg.coef_[0][0])             
M_library = M.item()
C = np.float64(reg.intercept_[0])
C_library = C.item()

p = float(input("Enter your height "))            #Take input from user about his height
q_library = (M_library)* p + C_library            #Calculate estimated weight 
q = m*p + c
print("Your weight might be around ")
print(q_library)
print(q)<jupyter_output>Enter your height 1.8
71.2279798569552
[71.27679014]
{'fraction_non_alphanumeric': 0.08374564779182701, 'fraction_numerical': 0.015759574857980574, 'mean_word_length': 2.740918437285812, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '321724', 'license': 'MIT', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1529, 'n_words': 689, 'path': 'Linear_Regression.ipynb', 'repo_name': 'm607stars/Machine-Learning-Algorithms', 'size': '5457'}