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

Functions in Python

scikit-learn වල `.fit()` සහ `.predict()` කියන්නෙ කවුරුහරි ලියපු functions විතරයි. Functions තේරුම් ගැනීමෙන් library එකක් black box එකකින් ඔයාට control කරන්න පුළුවන් tool set එකක් බවට පත් වෙනවා.

scikit-learn's `.fit()` and `.predict()` are just functions someone else wrote. Understanding functions is what turns a library from a black box into a set of tools you control.

3 May 20263 min readSelf Guide · Stage 13#Python & Data Basics#Python Fundamentals#Machine learning

Lesson 8 of 64 · Part 1 · Python & Data Basics · Python Fundamentals · bilingual notes (English + සිංහල)

Why this matters. scikit-learn's .fit() and .predict() are just functions someone else wrote. Understanding functions is what turns a library from a black box into a set of tools you control.

සිංහලෙන්: scikit-learn වල .fit() සහ .predict() කියන්නෙ කවුරුහරි ලියපු functions විතරයි. Functions තේරුම් ගැනීමෙන් library එකක් black box එකකින් ඔයාට control කරන්න පුළුවන් tool set එකක් බවට පත් වෙනවා.

What you'll use this for. Once your cleaning steps work on one dataset, you'll wrap them in a function so you can reuse them on the next dataset without copy-pasting.

සිංහලෙන්: ඔයාගෙ cleaning steps එක dataset එකකට වැඩ කරාට පස්සෙ, ඊළඟ dataset එකටත් copy-paste නොකර පාවිච්චි කරන්න function එකක් ඇතුළට දානවා.

Key principles

  • A function is a named, reusable recipe. Write the cleaning steps once inside a function, call it on 50 different columns — no retyping, no copy-paste mistakes.
    Function එකක් කියන්නෙ නම තියෙන, reuse කරන්න පුළුවන් recipe එකක්

Walkthrough

Functions:

Function is a block of code that can be reused in a Program

Factorial of a Number

Factorial of a number is the product of all the positive integers less than or equal to the given number

Factorial of 5 = 5 x 4 x 3 x 2 x 1 = 120

ExamplePython
number = int(input(' Enter a number to find its factorial : '))

factorial = 1

if number == 0:
  print(' The Factorial of 0 is 1')

else:
  for i in range(1, number+1):    
    factorial = factorial*i
  print('The factorial of ',number,' is',factorial)

Output

Output
 Enter a number to find its factorial : 10
The factorial of  10  is 3628800

Factoraial Function

ExamplePython
def factorial_value(num):

  factorial = 1

  if num == 0:
    return factorial

  else:
    for i in range(1, num+1):
      factorial = factorial*i
    return factorial
ExamplePython
print(factorial_value(5))

Output

Output
120
ExamplePython
print(factorial_value(10))

Output

Output
3628800
ExamplePython
print(factorial_value(6))

Output

Output
720

Keep going: ← Lesson 7 · Lesson 9 → · Track it in the Self Guide