The math behind Linear Regression and the Python way of implementation

Image for post
Image for post
Photo by Isaac Smith on Unsplash

Linear Regression in Python

Linear Regression is a machine learning algorithm based on supervised learning. Linear Regression is a predictive model that is used for finding the linear relationship between a dependent variable and one or more independent variables. Here,dependent variable/target variable(Y) should be continuous variable.

Let’s learn the math behind simple linear regression and the Python way of implementation using ski-kit learn

Dataset

Let’s looks at our dataset first. I have taken a simple dataset for an easy explanation. Years of Experience vs Salary.

We want to predict the salary of the person based on their years of experience?

Math Behind the Simple Linear Regression

Dataset

Image for post
Image for post
Exp vs Salary[Image by Author]

In the given dataset, we have Exp vs Salary. Now, we want to predict the salary for 3.5 years of experience? Let’s see how to predict that? …


Exploring different ways to import packages in Python

Image for post
Image for post
Photo by Marcin Jozwiak on Unsplash

Importing Packages in Python

Packages are a way of structuring Python’s module namespace by using “dotted module names”.

Refer to my story for importing modules in python.

For example, the module name A.B designates a submodule named B in a package named A

I have created the following packages and sub-packages. Let’s see how to import these packages.

Image for post
Image for post
Image by Author

I have created a package calc

I have created a sub-package mathcalc

I have created files addition.py,subtract.py under mathcalc subpackage.

All packages and subpackages have init.py.It …


merge (|) and update (|=) operators

two hands joined together to form the word “us”
two hands joined together to form the word “us”
Photo by Toa Heftiba on Unsplash

Merging Dictionaries in Python 3.9 Using the Union Operator

In Python 3.9, merge | and update |= operators have been added to the built-in dict class.

Let’s explore different ways to merge dictionaries before and after Python 3.9.

Different Ways to Merge Dictionaries Before Python 3.9

1.dict.update()

d1.update(d2) Update the dictionary d1 with the key/value pairs from d2, overwriting existing keys. Return None.

the sentence “last seen wins: last seen values will overwrite the overlapping keys” written in red letters
the sentence “last seen wins: last seen values will overwrite the overlapping keys” written in red letters
d1={'a':1,'b':2}
d2={'c':3,'b':9999}
d1.update(d2)
print (d1)
#Output:{'a': 1, 'b': 9999, 'c': 3}

Limitations

d1.update(d2) will modify the original dictionary d1. If the original dictionary need not be modified, create a copy of dictionary d1 and then update it.

d1={'a':1,'b':2}
d2={'c':3,'b':9999}
from copy import copy
d3=copy(d1)
d3.update(d2)
print (d3)
#Output:{'a': 1, 'b': 9999, 'c': 3}

2. Dictionary unpacking {**d1,**d2}

d3={**d1,**d2}

A double asterisk ** denotes dictionary unpacking. …

About

Indhumathy Chelliah

Aspiring DataScientist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store