Machine Learning Fundamentals Sinhala Guide | Supervised & Unsupervised Learning Explained

ආයුබෝවන් යාළුවනේ! අද අපි කතා කරන්න යන්නේ මේ දවස් වල හැමෝම කතා කරන, හැම තැනම ඇහෙන Machine Learning (ML) කියන මාතෘකාව ගැන. ML කියන්නේ අපේ අනාගතය වෙනස් කරන්න පුළුවන් තරම් බලයක් තියෙන තාක්ෂණයක්. ඒත් මේක මොකක්ද? කොහොමද වැඩ කරන්නේ? කියලා ගොඩක් අයට ප්රශ්න ඇති නේද? බය වෙන්න එපා! අද අපි මේක සරලව, පහසුවෙන් තේරුම් ගන්න විදිහට සිංහලෙන් කතා කරමු.
මේ tutorial එක අවසානෙදි ඔයාලට Machine Learning වල මූලික සංකල්ප, ඒ කියන්නේ Supervised Learning, Unsupervised Learning, Regression, Classification, Clustering වගේ දේවල් ගැන පැහැදිලි අවබෝධයක් ලැබෙයි. එහෙනම් අපි පටන් ගමු!
Machine Learning කියන්නේ මොකක්ද?
සරලවම කියනවා නම්, Machine Learning කියන්නේ පරිගණක වලට දත්ත (data) වලින් ඉගෙන ගන්න පුළුවන් විදිහට පුහුණු කරන එක. හිතන්න පොඩි ළමයෙක්ට වගේ, අපි පරිගණක වලට උදාහරණ (examples) පෙන්නලා උගන්වනවා. ඊට පස්සේ ඒගොල්ලන්ට පුළුවන් අලුත් දේවල් ගැන තමන්ගේම තීරණ ගන්න, නැත්නම් අනාවැකි (predictions) කියන්න.
සාම්ප්රදායික Programming වලදී අපි පරිගණක වලට "මේක කරපන්", "මේ විදිහට කරපන්" කියලා පැහැදිලිව උපදෙස් (instructions) දෙනවා. ඒත් ML වලදී අපි කියන්නේ "මෙන්න දත්ත, මේ දත්ත වලින් ඉගෙනගෙන මේ ප්රශ්නයට විසඳුමක් හොයපන්" කියලා.
ML Application කිහිපයක්:
- ඔයාට එන Spam Emails identify කරන එක.
- Facebook එකේ Photos වල ඉන්න යාළුවෝ tag කරන එක.
- Netflix එක ඔයාට Movie suggestions දෙන එක.
- Google Maps වල හොඳම route එක හොයාගන්න එක.
මේ හැම එකක්ම ML use කරන Applications.
Supervised Learning: "උපදෙස් එක්ක ඉගෙන ගනිමු"
Supervised Learning කියන්නේ Machine Learning වල තියෙන ප්රධානම ක්රමයක්. "Supervised" කියන වචනෙම තේරෙනවා වගේ, මෙතනදී අපි පරිගණක වලට උපදෙස් (supervision) දෙනවා. ඒ කියන්නේ අපි පරිගණක වලට දෙන දත්ත වල ප්රතිඵලය (output) දැනටමත් තියෙනවා.
හිතන්න, ඔයා පොඩි ළමයෙක්ට පින්තූර පෙන්නලා "මේක බළලෙක්", "මේක බල්ලෙක්" කියලා කියලා දෙනවා වගේ. ළමයා ඒකෙන් ඉගෙනගෙන පස්සේ අලුත් පින්තූරයක් දැක්කම ඒක බළලෙක්ද බල්ලෙක්ද කියලා කියන්න පුළුවන් වෙනවා නේද? Supervised Learning වලදීත් වෙන්නේ ඒකමයි.
මේකට අපි කියන්නේ "labeled data" (ලේබල් කරපු දත්ත) කියලා. Data එකත් තියෙනවා, ඒ Data එකට අදාළ Output එකත් තියෙනවා. අපි මේ Data set එක use කරලා Model එක train කරනවා.
1. Regression: අගයන් අනාවැකි කියමු
Regression කියන්නේ Supervised Learning වල වැදගත් කොටසක්. මේකෙන් කරන්නේ අඛණ්ඩ අගයක් (continuous value) අනාවැකි කීම. ඒ කියන්නේ යම්කිසි numeric value එකක් predict කරන එක.
- උදාහරණ:
- ගෙයක මිල (house price) predict කරන එක (කාමර ගාන, ප්රදේශය, ප්රමාණය අනුව).
- කෙනෙක්ගේ වයස predict කරන එක.
- කෙනෙක්ගේ වැටුප (salary) predict කරන එක.
හිතන්න, ඔයාට ගෙවල් වල size එක, කාමර ගාන, තියෙන ප්රදේශය වගේ දත්ත එක්ක ඒ ගෙවල් වල මිලත් තියෙනවා. Regression Model එකක් use කරලා, අලුත් ගෙයක size එක, කාමර ගාන, ප්රදේශය දුන්නම ඒකේ මිල predict කරන්න පුළුවන්.
සරල Regression උදාහරණයක් (Python):
මෙතන අපි ගෙයක size එක (square feet) අනුව මිල predict කරන සරල Linear Regression Model එකක අදහසක් බලමු.
import numpy as np
from sklearn.linear_model import LinearRegression
# Training data
# X: House sizes in square feet
# y: House prices in thousands of dollars
X = np.array([[1500], [2000], [2500], [3000], [1800], [2200], [2800]])
y = np.array([300, 400, 500, 600, 350, 440, 560])
# Create a Linear Regression model
model = LinearRegression()
# Train the model with our data
model.fit(X, y)
# Make a prediction for a new house size
new_house_size = np.array([[2300]]) # A house of 2300 square feet
predicted_price = model.predict(new_house_size)
print(f"For a house of {new_house_size[0][0]} sq ft, the predicted price is: ${predicted_price[0]:.2f}k")
# Output might be something like: For a house of 2300 sq ft, the predicted price is: $460.00k
මේ Code එකෙන් වෙන්නේ, අපිට තියෙන Data (පරණ ගෙවල් වල ප්රමාණය සහ මිල) use කරලා Model එක පුහුණු කරලා, අලුත් ගෙයක ප්රමාණය දුන්නම ඒකේ මිල අනාවැකි කීමයි. සරලයි නේද?
2. Classification: වර්ග කරමු
Classification කියන්නෙත් Supervised Learning වල තවත් වැදගත් ක්රමයක්. මේකෙන් කරන්නේ දත්ත කාණ්ඩ වලට (categories) වර්ග කිරීම. Regression වල වගේ numeric value එකක් predict කරනවා වෙනුවට, මෙතනදී predict කරන්නේ category එකක්.
- උදාහරණ:
- Email එකක් Spam ද Not Spam ද කියලා classify කරන එක.
- රෝගියෙක්ට රෝගයක් තියෙනවද නැද්ද කියලා classify කරන එක.
- පින්තූරයක් බළලෙක්ද, බල්ලෙක්ද, කුරුල්ලෙක්ද කියලා classify කරන එක.
- බැංකුවක loan එකකට apply කරන කෙනෙක්ට loan එක දෙන්න හොඳද නැද්ද කියලා classify කරන එක.
Output එක Discrete value එකක් (categorical value) විදිහට තියෙනවා නම්, අපි Classification use කරනවා. (e.g., Yes/No, A/B/C, Cat/Dog/Bird).
සරල Classification උදාහරණයක් (Python):
Spam Email Classification එකක් ගැන හිතමු. මෙතන අපි සරල Text data එකක් use කරලා Email එකක් Spam ද නැද්ද කියලා classify කරන විදිහ බලමු.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Sample data
emails = [
("Win a free iPhone now!", "spam"),
("Meeting reminder for tomorrow", "not spam"),
("Your bank account needs verification", "spam"),
("Hello, hope you are doing well", "not spam"),
("Limited time offer - click here!", "spam"),
("Project update meeting", "not spam")
]
# Separate features (text) and labels (spam/not spam)
X = [email[0] for email in emails]
y = [email[1] for email in emails]
# Convert text data to numerical features (Bag of Words model)
vectorizer = CountVectorizer()
X_vectorized = vectorizer.fit_transform(X)
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_vectorized, y, test_size=0.2, random_state=42)
# Create a Naive Bayes classifier model
model = MultinomialNB()
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Evaluate the model
print(f"Accuracy: {accuracy_score(y_test, y_pred):.2f}")
# Predict new emails
new_emails = [
"Congratulations! You've won a prize!",
"Hi, how are you?"
]
new_emails_vectorized = vectorizer.transform(new_emails)
predictions = model.predict(new_emails_vectorized)
print(f"'{new_emails[0]}' is classified as: {predictions[0]}")
print(f"'{new_emails[1]}' is classified as: {predictions[1]}")
# Output might be something like:
# Accuracy: 1.00 (depends on test split, for small data could be 1.0)
# 'Congratulations! You've won a prize!' is classified as: spam
# 'Hi, how are you?' is classified as: not spam
මේකෙන් Email වල Text එක analyze කරලා ඒක Spam ද නැද්ද කියලා තීරණය කරනවා. මේකත් ගොඩක් practical Application එකක් නේද?
Unsupervised Learning: "අපේම රටා හොයාගනිමු"
Unsupervised Learning කියන්නේ Supervised Learning වලට වඩා වෙනස් ක්රමයක්. මෙතනදී අපි පරිගණක වලට labeled data දෙන්නේ නැහැ. ඒ කියන්නේ අපි දෙන දත්ත වල ප්රතිඵලය (output) මොකක්ද කියලා අපි දන්නේ නැහැ.
හිතන්න, ඔයාට එකතු කරපු විවිධ වර්ණ වලට, හැඩ වලට තියෙන ගල් ගොඩක් තියෙනවා කියලා. ඔයා ඒ ළමයින්ට කියන්නේ නැහැ "මේක නිල් පාට ගලක්", "මේක රවුම් ගලක්" කියලා. ඒ වෙනුවට, ළමයින්ට කියනවා "මේ ගල් ටික සමාන දේවල් අනුව එකතු කරන්න" කියලා. ළමයි ඒකෙන් වර්ණය අනුව හරි, හැඩය අනුව හරි ගල් වෙන් කරයි නේද? Unsupervised Learning වලදීත් වෙන්නේ ඒකමයි.
මෙතනදී Model එකේ අරමුණ වෙන්නේ Data එක ඇතුළේ තියෙන Patterns, Structures, නැත්නම් Hidden Relationships හොයාගන්න එක. මේක ගොඩක් useful වෙන්නේ අපිට Data එක ගැන ලොකු අදහසක් නැති වෙලාවට.
1. Clustering: කණ්ඩායම් වලට බෙදමු
Clustering කියන්නේ Unsupervised Learning වල ප්රධානම ක්රමයක්. මේකෙන් කරන්නේ සමාන දත්ත ලක්ෂණ තියෙන data points කණ්ඩායම් වලට (clusters) එකතු කරන එක.
- උදාහරණ:
- පාරිභෝගිකයින් (customers) ඔවුන්ගේ මිලදී ගැනීමේ රටා (purchase behavior) අනුව කණ්ඩායම් වලට වෙන් කරන එක.
- සමාජ මාධ්ය වල trending topics හොයාගන්න එක.
- ජාන දත්ත (genomic data) analyze කරලා සමාන ජාන රටා තියෙන ඒවා එකතු කරන එක.
Clustering Algorithm එකකින් වෙන්නේ Data set එකේ තියෙන data points වල සමානකම් (similarities) අනුව Group කරන එක. මෙතනදී අපි Model එකට කියන්නේ නැහැ කොපමණ Group ගානකට වෙන් කරන්නද කියලා. Algorithm එකෙන් ඒක තීරණය කරනවා (නැත්නම් අපි number of clusters define කරනවා K-Means වගේ වෙලාවට).
සරල Clustering උදාහරණයක් (Python):
අපි හිතමු අපිට customers ලාගේ වයස (age) සහ මාසික ආදායම (monthly income) වගේ දත්ත තියෙනවා කියලා. මේ data use කරලා අපි K-Means Clustering Algorithm එකෙන් customer segments (පාරිභෝගික කොටස්) කිහිපයකට වෙන් කරමු.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# Sample customer data: [Age, Monthly Income]
# (This is unlabeled data)
X = np.array([
[22, 25000], [25, 30000], [20, 22000], [28, 32000], # Young, Lower Income
[35, 60000], [38, 65000], [42, 70000], [40, 68000], # Middle-aged, Higher Income
[55, 45000], [58, 48000], [60, 52000], [52, 43000] # Older, Medium Income
])
# Use K-Means to find 3 clusters (customer segments)
kmeans = KMeans(n_clusters=3, random_state=0, n_init=10) # n_init is important for stability
kmeans.fit(X)
# Get the cluster labels for each data point
labels = kmeans.labels_
# Get the coordinates of the cluster centers
centers = kmeans.cluster_centers_
# Visualize the clusters
plt.figure(figsize=(8, 6))
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=100, alpha=0.8)
plt.scatter(centers[:, 0], centers[:, 1], c='red', marker='X', s=200, label='Cluster Centers')
plt.title('Customer Segmentation using K-Means Clustering')
plt.xlabel('Age')
plt.ylabel('Monthly Income (Rs.)')
plt.legend()
plt.grid(True)
plt.show()
print("Cluster labels for each customer:", labels)
print("Cluster Centers (Age, Income):")
for i, center in enumerate(centers):
print(f" Cluster {i+1}: Age={center[0]:.2f}, Income={center[1]:.2f}")
# Example Output for labels:
# Cluster labels for each customer: [0 0 0 0 2 2 2 2 1 1 1 1] (or similar, depends on random_state and initialization)
# Cluster Centers (Age, Income):
# Cluster 1: Age=23.75, Income=27250.00
# Cluster 2: Age=56.25, Income=47000.00
# Cluster 3: Age=38.75, Income=65750.00
මේ Code එක run කරාම ඔයාට ලස්සන Graph එකක් ලැබෙයි, ඒකෙන් customers ලා කණ්ඩායම් තුනකට වෙන් වෙලා තියෙන හැටි පෙන්නනවා. මේකෙන් අපිට පාරිභෝගික රටා තේරුම් ගන්නත්, ඒ අනුව marketing strategies හදන්නත් පුළුවන්.
අවසාන වශයෙන්
ඉතින් යාළුවනේ, අපි අද Machine Learning වල මූලික සංකල්ප ගොඩක් සරලව කතා කළා. ඔයාලට Supervised Learning (Regression, Classification) සහ Unsupervised Learning (Clustering) කියන්නේ මොකක්ද, ඒවා කොහොමද වැඩ කරන්නේ, මොන වගේ අවස්ථාවලදීද use කරන්නේ කියලා පැහැදිලි අවබෝධයක් ලැබෙන්න ඇති කියලා හිතනවා.
Machine Learning කියන්නේ හරිම උද්යෝගිමත් ක්ෂේත්රයක්. මේක පටන් ගන්න හොඳම තැන තමයි මේ මූලික සංකල්ප තේරුම් ගන්න එක. මේ tutorial එක ඔයාලට ඒකට හොඳ පදනමක් වෙන්න ඇති.
මේ ගැන ඔයාලගේ අදහස්, ප්රශ්න පහළින් comment කරන්න. මේ concepts ඔයාලගේ next project එකට implement කරන්න try කරන්න. තව මොනවා ගැනද දැනගන්න කැමති කියලා කියන්න. අපි ඊළඟට තවත් වැදගත් මාතෘකාවකින් හම්බවෙමු! ඔබට ජය!