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

Numerical Dataset Pre Processing Use Case

මේ section එකේ ඉගෙන ගත්ත ඔක්කොම — missing values, standardization, encoding, splitting — එකට එකතු කරලා, මින් ඉස්සරහට ඕනම real dataset එකකට repeat කරන එකම pipeline එකට ගේනවා මේ පාඩම.

This lesson strings together everything from this section — missing values, standardization, encoding, splitting — into the one pipeline you'll repeat on every real dataset from now on.

7 May 20264 min readSelf Guide · Stage 13#Python & Data Basics#Data Pre-processing#Machine learning

Lesson 20 of 64 · Part 1 · Python & Data Basics · Data Pre-processing · bilingual notes (English + සිංහල)

Why this matters. This lesson strings together everything from this section — missing values, standardization, encoding, splitting — into the one pipeline you'll repeat on every real dataset from now on.

සිංහලෙන්: මේ section එකේ ඉගෙන ගත්ත ඔක්කොම — missing values, standardization, encoding, splitting — එකට එකතු කරලා, මින් ඉස්සරහට ඕනම real dataset එකකට repeat කරන එකම pipeline එකට ගේනවා මේ පාඩම.

What you'll use this for. Whenever you start a brand-new project, this is the checklist you mentally run through before writing a single line of model code.

සිංහලෙන්: අලුත් project එකක් පටන් ගන්නකොට, model code එකක් ලියන්නත් කලින් හිතින්ම run කරන checklist එකයි මේක.

Key principles

  • Order matters: clean, then encode, then split, then scale. Splitting before cleaning risks leaking information between train and test sets — this pipeline exists to get that order right, every time.
    පිළිවෙළ වැදගත්: clean කරන්න, එහෙනම් encode, එහෙනම් split, එහෙනම් scale

Walkthrough

Importing the Dependencies

ExamplePython
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

Data Collection & Pre-Processing

ExamplePython
# loading the data from csv file to a pandas dataframe
diabetes_data = pd.read_csv('/content/diabetes.csv')
ExamplePython
# first 5 rows of the dataframe
diabetes_data.head()
PregnanciesGlucoseBloodPressureSkinThicknessInsulinBMIDiabetesPedigreeFunction…
061487235033.60.627…
11856629026.60.351…
28183640023.30.672…
318966239428.10.167…
40137403516843.12.288…
ExamplePython
# number of rows & columns
diabetes_data.shape

Output

Output
(768, 9)
ExamplePython
diabetes_data.describe()
PregnanciesGlucoseBloodPressureSkinThicknessInsulinBMIDiabetesPedigreeFunction…
count768.000000768.000000768.000000768.000000768.000000768.000000768.000000…
mean3.845052120.89453169.10546920.53645879.79947931.9925780.471876…
std3.36957831.97261819.35580715.952218115.2440027.8841600.331329…
min0.0000000.0000000.0000000.0000000.0000000.0000000.078000…
25%1.00000099.00000062.0000000.0000000.00000027.3000000.243750…
50%3.000000117.00000072.00000023.00000030.50000032.0000000.372500…

… 2 more rows

Separating Features and Target

ExamplePython
X = diabetes_data.drop(columns='Outcome', axis =1)
Y = diabetes_data['Outcome']
ExamplePython
print(X)

Output

Output
     Pregnancies  Glucose  BloodPressure  ...   BMI  DiabetesPedigreeFunction  Age
0              6      148             72  ...  33.6                     0.627   50
1              1       85             66  ...  26.6                     0.351   31
2              8      183             64  ...  23.3                     0.672   32
3              1       89             66  ...  28.1                     0.167   21
4              0      137             40  ...  43.1                     2.288   33
..           ...      ...            ...  ...   ...                       ...  ...
763           10      101             76  ...  32.9                     0.171   63
764            2      122             70  ...  36.8                     0.340   27
765            5      121             72  ...  26.2                     0.245   30
766            1      126             60  ...  30.1                     0.349   47
767            1       93             70  ...  30.4                     0.315   23

[768 rows x 8 columns]
ExamplePython
print(Y)

Output

Output
0      1
1      0
2      1
3      0
4      1
      ..
763    0
764    0
765    0
766    1
767    0
Name: Outcome, Length: 768, dtype: int64

0 --> Non - Diabetic

1 --> Diabetic

Data Standardization

ExamplePython
scaler = StandardScaler()
ExamplePython
standardized_data = scaler.fit_transform(X)
ExamplePython
print(standardized_data)

Output

Output
[[ 0.63994726  0.84832379  0.14964075 ...  0.20401277  0.46849198
   1.4259954 ]
 [-0.84488505 -1.12339636 -0.16054575 ... -0.68442195 -0.36506078
  -0.19067191]
 [ 1.23388019  1.94372388 -0.26394125 ... -1.10325546  0.60439732
  -0.10558415]
 ...
 [ 0.3429808   0.00330087  0.14964075 ... -0.73518964 -0.68519336
  -0.27575966]
 [-0.84488505  0.1597866  -0.47073225 ... -0.24020459 -0.37110101
   1.17073215]
 [-0.84488505 -0.8730192   0.04624525 ... -0.20212881 -0.47378505
  -0.87137393]]
ExamplePython
X = standardized_data
ExamplePython
print(X)

Output

Output
[[ 0.63994726  0.84832379  0.14964075 ...  0.20401277  0.46849198
   1.4259954 ]
 [-0.84488505 -1.12339636 -0.16054575 ... -0.68442195 -0.36506078
  -0.19067191]
 [ 1.23388019  1.94372388 -0.26394125 ... -1.10325546  0.60439732
  -0.10558415]
 ...
 [ 0.3429808   0.00330087  0.14964075 ... -0.73518964 -0.68519336
  -0.27575966]
 [-0.84488505  0.1597866  -0.47073225 ... -0.24020459 -0.37110101
   1.17073215]
 [-0.84488505 -0.8730192   0.04624525 ... -0.20212881 -0.47378505
  -0.87137393]]
ExamplePython
print(Y)

Output

Output
0      1
1      0
2      1
3      0
4      1
      ..
763    0
764    0
765    0
766    1
767    0
Name: Outcome, Length: 768, dtype: int64

Splitting the dataset into Training data & Testing Data

ExamplePython
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=2)
ExamplePython
print(X.shape, X_train.shape, X_test.shape)

Output

Output
(768, 8) (614, 8) (154, 8)

Keep going: ← Lesson 19 · Lesson 21 → · Track it in the Self Guide