Predicting Divorces Pronounced In Mauritius Using Linear Regression (ML) Abdur-Rahmaan Janhangeer github logo Feb 20・2 min read

here is a post about linear regression, the first step of machine learning. we'll be using python to predict divorce cases for mauritius, my country

What Is Linear Regression

regression just means prediction.

read more on those two dev.to articles

Opening Up Jupyter

let us import our libs

``````import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
# psst inspect sklearn to see what other models there are
``````

let's have a look at our data

``````df = pd.read_csv('<path-to-file>/divorce.csv')
df
``````

gives us we'll be predicting the column Number of cases disposed of which Divorce pronounced

but let us have a look at our data

``````plt.scatter(df['Year'], df['Number of cases disposed of which Divorce pronounced'])
``````

we get let us train our model

``````reg = linear_model.LinearRegression()
reg.fit(df[['Year']], df['Number of cases disposed of which Divorce pronounced'])
``````

and predict for the year 2017

``````reg.predict([])# it was 1,921
``````

we get

``````array([ 2140.22222222])
``````

Getting m And c

since linear regression is just a straight line (really that whole machine learning world is just some maths), we can get the coefficient (our m) and the intercept (our c)

coefficient is given by

``````reg.coef_
``````

outputs

``````array([ 57.79118774])
``````

and intercept by

``````reg.intercept_
``````

outputs

``````-114424.60344827584
``````

Building Our Own Predictor Function

we can now build a simple function to predict without passing by ml

``````def devdotto_predict(year_):
# m * x + c
return 57.79118774 * year_ + -114424.60344827584
``````

and use it

``````devdotto_predict(2017)
``````

we get

``````2140.222223304154
``````

compare above

Conclusion

machine learning is super easy if you understand the concept!

cover img credit : Photo by Xavier Coiffic On Unsplash
real pic of mauritius

DISCUSS

One last thing:

Now reaching over 3 million visitors per month, DEV is the fastest growing software development community in the world.

It's free, open source, devoted to the open web, and will never have popups or a pay wall.

Classic DEV Post from Jul 12 '18

Ways to enhance your DEV posts! Useful embeds.  