loading...
Cover image for Predicting Divorces Pronounced In Mauritius Using Linear Regression (ML)

Predicting Divorces Pronounced In Mauritius Using Linear Regression (ML)

abdurrahmaanj profile image Abdur-Rahmaan Janhangeer ・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

Our Data

we'll be downloading our csv from here . the first one. i renamed it divorce.csv

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

Alt text of image

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

Alt text of image

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([[2017]])# 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

Posted on by:

Discussion

pic
Editor guide