Galle, LK--:--5/6 sites live

Lin Reg implementation

ඒම algorithm එකටම දෙවෙනි වතාව, ටිකක් වෙනස් dataset එකකින් ශක්තිමත් කරනවා — මේ repetition එක හිතාමතාම, මොකද මින් පස්සේ එන ගොඩක් දේවලට root එක මේකයි.

A second pass at the same algorithm, reinforcing it with a slightly different dataset — repetition here is deliberate, because this algorithm underlies so much of what follows.

12 May 20263 min readSelf Guide · Stage 15#Building Models from Scratch#Algorithms from Scratch#Machine learning

Lesson 34 of 64 · Part 3 · Building Models from Scratch · Algorithms from Scratch · bilingual notes (English + සිංහල)

Why this matters. A second pass at the same algorithm, reinforcing it with a slightly different dataset — repetition here is deliberate, because this algorithm underlies so much of what follows.

සිංහලෙන්: ඒම algorithm එකටම දෙවෙනි වතාව, ටිකක් වෙනස් dataset එකකින් ශක්තිමත් කරනවා — මේ repetition එක හිතාමතාම, මොකද මින් පස්සේ එන ගොඩක් දේවලට root එක මේකයි.

What you'll use this for. Seeing the same idea implemented twice, slightly differently, is what turns "I followed along once" into "I actually understand this."

සිංහලෙන්: එකම idea එක දෙපාරක්, ටිකක් වෙනස්ව implement වෙනවා දකින එකෙන් තමයි "මං එක පාරක් follow කලා" කියන එක "මට ඇත්තටම මේක තේරෙනවා" කියන එකට හැරෙන්නෙ.

Key principles

  • Re-implementing a concept from a different angle is how it actually sticks. If you found this version easier or harder than the first, that difference is telling you exactly which part of the concept needs more attention.
    Concept එකක් වෙනස් angle එකකින් නැවත implement කිරීමෙන් තමයි ඒක ඇත්තටම මතක තියෙන්නෙ

Walkthrough

ExamplePython
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

import Lin_Reg_model

Data Processing

ExamplePython
# loading the data from csv file to pandas dataframe
salary_data = pd.read_csv('/content/salary_data.csv')
ExamplePython
# printing the first 5 rows of the dataframe
salary_data.head()
YearsExperienceSalary
01.139343
11.346205
21.537731
32.043525
42.239891
ExamplePython
# number of rows & columns in the dataset
salary_data.shape

Output

Output
(30, 2)
ExamplePython
# checking for missing values
salary_data.isnull().sum()

Output

Output
YearsExperience    0
Salary             0
dtype: int64

Splitting the feature & target

ExamplePython
X = salary_data.iloc[:,:-1].values
Y = salary_data.iloc[:,1].values
ExamplePython
print(X)

Output

Output
[[ 1.1]
 [ 1.3]
 [ 1.5]
 [ 2. ]
 [ 2.2]
 [ 2.9]
 [ 3. ]
 [ 3.2]
 [ 3.2]
 [ 3.7]
 [ 3.9]
 [ 4. ]
 [ 4. ]
 [ 4.1]
 [ 4.5]
 [ 4.9]
 [ 5.1]
 [ 5.3]
… (12 more lines)
ExamplePython
print(Y)

Output

Output
[ 39343  46205  37731  43525  39891  56642  60150  54445  64445  57189
  63218  55794  56957  57081  61111  67938  66029  83088  81363  93940
  91738  98273 101302 113812 109431 105582 116969 112635 122391 121872]

Splitting the data to training data & Test data

ExamplePython
 X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.33,
                                                     random_state = 2 )

Train the Logistic Regression Model

ExamplePython
model = Lin_Reg_model.Linear_Regression(learning_rate=0.01, no_of_iterations=1000)
ExamplePython
model.fit(X_train, Y_train)
ExamplePython
# print the parameters

print('weight = ', model.w[0])
print('bias = ', model.b)

Output

Output
weight =  9580.301196603597
bias =  23226.38946603212

Predict the salary value for test data

ExamplePython
test_data_prediction = model.predict(X_test)
ExamplePython
print(test_data_prediction)

Output

Output
[ 35680.78102162  33764.7207823   66337.74485075  58673.50389347
  91246.52796192  80708.19664565 101784.85927818  51967.29305584
  42386.99185924  88372.43760294]

Visualizing the predicted values and actual values

ExamplePython
plt.scatter( X_test, Y_test, color = 'red' )
plt.plot( X_test, test_data_prediction, color = 'blue' )    
plt.xlabel( 'Work Experience' )
plt.ylabel( 'Salary' )
plt.title( 'Salary vs Experience' )
plt.show()

Lin Reg implementation — figure 1


Keep going: ← Lesson 33 · Lesson 35 → · Track it in the Self Guide