DEV Community

Cover image for Predicting Divorces Pronounced In Mauritius Using Linear Regression (ML)
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer

Posted on

Predicting Divorces Pronounced In Mauritius Using Linear Regression (ML)

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
Enter fullscreen mode Exit fullscreen mode

let's have a look at our data

df = pd.read_csv('<path-to-file>/divorce.csv')
df
Enter fullscreen mode Exit fullscreen mode

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'])
Enter fullscreen mode Exit fullscreen mode

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'])
Enter fullscreen mode Exit fullscreen mode

and predict for the year 2017

reg.predict([[2017]])# it was 1,921
Enter fullscreen mode Exit fullscreen mode

we get

array([ 2140.22222222])
Enter fullscreen mode Exit fullscreen mode

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_
Enter fullscreen mode Exit fullscreen mode

outputs

array([ 57.79118774])
Enter fullscreen mode Exit fullscreen mode

and intercept by

reg.intercept_
Enter fullscreen mode Exit fullscreen mode

outputs

-114424.60344827584
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

and use it

devdotto_predict(2017)
Enter fullscreen mode Exit fullscreen mode

we get

2140.222223304154
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

The Simplest It Can Get!