Let x be the independent variable and y be the dependent variable. We will define a linear relationship between these two variables as follows:

` y = a_0 + a_1*x`

where a_1 is the slope of the line and a_0 is the y-intercept. This equation will be used for predicting the value of Y.Before moving on to the algorithm, lets have a look at two important concepts to understand linear regression.

**Loss function:**It is the function used to find the best value of a_1 and a_0. We will use mean squared error by following steps:- Find the difference between the actual and predicted value of y for given x.
- Square the difference
- Find the mean of squares for every value of x.

**Gradient descent algorithm:**It is an optimization algorithm used to find the minimum of function. In linear regression, it used to minimize the cost function, by updating the value of a_1 and a_0.

Imagine a U shaped valley. Suppose our aim is to reach the bottommost position. We start from the topmost position. There is a catch, you can only take a discrete number of steps to reach the bottom. If you decide to take one step at a time you would eventually reach the bottom of the valley but this would take a longer time. If you choose to take longer steps each time, you would reach sooner but, there is a chance that you could overshoot the bottom of the valley and not exactly at the bottom.

In the gradient descent algorithm, the number of steps you take is the learning rate. This decides on how fast the algorithm converges to the minima. To update a_1 and a_0, we take gradients from the loss function. To find these gradients, we take partial derivatives with respect to a_1 and a_0.

The partial derivates are the gradients and they are used to update the values of a_1 and a_0. Alpha is the learning rate which is a hyperparameter. A smaller learning rate could get you closer to the minima but takes more time to reach the minima, a larger learning rate converges sooner but there is a chance that you could overshoot the minima.

**Linear Regression selection criteria: **

- classification and regression capabilities
- data quality (outliers can affect)
- computational complexity

**Implementation: **I have used this dataset , which consists of marks scored by students and no. of hour student studied. Our aim is to predict the marks scored by the student.

- Code using the above discussion of loss function and gradient descent.

`#import required librariesimport pandas as pdimport numpy as npimport matplotlib.pyplot as plt %matplotlib inline#reading data from given linkurl = "http://bit.ly/w-data"df = pd.read_csv(url)X = df.iloc[:, 0]Y = df.iloc[:, 1]plt.scatter(X, Y)plt.show()`

`# Building the modelm = 0c = 0L = 0.0001 # The learning Rateepochs = 1000 # The number of iterations to perform gradient descentn = float(len(X)) # Number of elements in X# Performing Gradient Descent for i in range(epochs): Y_pred = m*X + c # The current predicted value of Y D_m = (-2/n) * sum(X * (Y - Y_pred)) # Derivative wrt m D_c = (-2/n) * sum(Y - Y_pred) # Derivative wrt c m = m - L * D_m # Update m c = c - L * D_c # Update cprint (m, c)`

9.896964110671043 1.6314708810783134

`Y_pred = m*X + cplt.scatter(X, Y)plt.plot([min(X), max(X)], [min(Y_pred), max(Y_pred)], color='red') # predictedplt.show()`

**Code using scikit learn library can be found here .**

Linear Regression is used for evaluating and analyzing trends and sales estimate, assessment of risk in the financial sector and insurance domain etc.

**Conclusion:** Linear Regression is a very useful and simple algorithm of machine learning.

Hope you find this article helpful. Keep learning!

]]>- Stack: A stack (sometimes called a push-down stack) is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end. This end is commonly referred to as the top. The end opposite the top is known as the base. The ordering principle of stack is called LIFO, last-in-first-out.

Eg: stack=[1,a,True]

Operation performed on stack:

Implementation of stack:

`class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items)`

- Queue: A queue is an ordered collection of items where the addition of new items happens at one end, called the rear, and the removal of existing items occurs at the other end, commonly called the front. The ordering principle of queue is called FIFO, first-in-first-out.

Eg: queue=[1,a, True]

Operation performed on queue:

Implementation of queue:

`class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def size(self): return len(self.items)`

- Deque: A deque, also known as a double-ended queue, is an ordered collection of items similar to the queue. It has two ends, a front and a rear, and the items remain positioned in the collection. New items can be added at either the front or the rear. Likewise, existing items can be removed from either end.

Eg: deque=[1,a,True]

Operation performed on deque:

Implementation of deque:

`class Deque: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def addFront(self, item): self.items.append(item) def addRear(self, item): self.items.insert(0,item) def removeFront(self): return self.items.pop() def removeRear(self): return self.items.pop(0) def size(self): return len(self.items)`

Hope you find this article helpful. Keep learning!

]]>**List:**It is a linear data structure in Python that is a mutable, or changeable, ordered sequence of elements. They are heterogeneous, meaning that the data objects need not all be from the same class and the collection can be assigned to a variable.

eg: List=[1,2,3,a,True]

**Operation performed on the list:**

**Tuple:**They are very similar to lists in that they are heterogeneous sequences of data. The difference is that a tuple is immutable, like a string. A tuple cannot be changed. Operations performed on tuple are same as that of the list except the assignment operation as we cannot change items in the tuple.

eg: Tuple = (2,True,4.96)

**Sets:**A set is an unordered collection of zero or more immutable Python data objects. Sets do not allow duplicates and are written as comma-delimited values enclosed in curly braces. The empty set is represented by set(). Sets are heterogeneous, and the collection can be assigned to a variable.

eg: set={1,2,3,a,True}

**Operation performed on the set:**

-** Dictionary: **Dictionaries are collections of associated pairs of items where each pair consists of a key and a value. This key-value pair is typically written as key: value. It is unordered structure.

eg: dicti={1:a;2:b:3:c}

**Operation performed on the dictionary:**

**String:**They are sequential collections of zero or more letters, numbers and other symbols. We call these letters, numbers and other symbols characters.

eg: s=abcbd

**Operation performed on string:**

Hope you find this article helpful. Check out my next article, to learn about other data structure in Python. Until then, keep learning!

]]>