Machine Learning Basics Sinhala | Supervised vs Unsupervised | Regression, Classification, Clustering

Machine Learning Basics Sinhala | Supervised vs Unsupervised | Regression, Classification, Clustering

කොහොමද යාලුවනේ, Machine Learning ලෝකයට සාදරයෙන් පිළිගනිමු!

අද ලෝකයේ හැමතැනම AI, Machine Learning (ML) කියන වචන ඇහෙනවා. Smart phones වල facial recognition, Netflix, YouTube වගේ platforms වල අපිට recommend කරන movies/videos, Google search results, spam emails filter කරන එක වගේ දේවල් වලට මේ ML algorithms තමයි පදනම. මේවා දැක්කාම 'අනේ මේවා නම් මට තේරෙන්නේ නෑ' කියලා හිතෙනවා ඇති නේද? ඒත් එහෙම හිතන්න එපා! ඇත්තටම Machine Learning කියන්නේ හිතන තරම් සංකීර්ණ දෙයක් නෙවෙයි. මේ guide එකෙන් අපි ML වල මූලික සංකල්ප සරලව, පහසුවෙන් තේරුම් ගන්න බලමු.

මේ tutorial එකෙන් අපි මොනවද කතා කරන්නේ?

  • Machine Learning කියන්නේ මොකක්ද? (සරලවම)
  • Machine Learning වල ප්‍රධාන වර්ග දෙක: Supervised Learning සහ Unsupervised Learning.
  • Supervised Learning යටතේ එන වැදගත් tasks දෙකක්: Regression සහ Classification.
  • Unsupervised Learning යටතේ එන වැදගත් task එකක්: Clustering.
  • මේ හැම සංකල්පයක්ම practical code snippets එක්ක අපි විස්තර කරමු.

ඉතින්, ඔයාලා computer science පසුබිමක් තියෙන කෙනෙක්ද, නැත්නම් අලුතින් මේ ක්ෂේත්‍රයට එන්න බලාපොරොත්තු වෙන කෙනෙක්ද කියන එක ගැටළුවක් නෙවෙයි. මේ guide එකෙන් ML වලට හොඳ පදනමක් දාගන්න පුළුවන් වෙයි කියලා මම විශ්වාස කරනවා. එහෙනම් අපි පටන් ගමු!

Machine Learning කියන්නේ මොකක්ද? (What is Machine Learning?)

සරලවම කිව්වොත්, Machine Learning කියන්නේ computers වලට, explicitly program නොකර, data වලින් ඉගෙන ගන්න පුළුවන් හැකියාව දෙන ක්‍රමවේදයක්. සාමාන්‍යයෙන් අපි software එකක් හදනකොට, අපි rules set එකක් ලියනවා. (උදා: if this happens, do that). ඒත් ML වලදී, අපි computer එකට ගොඩක් data දෙනවා, ඊටපස්සේ ඒ data එකෙන් patterns හොයාගෙන, අලුත් data ආවම ඒ patterns අනුව predictions කරන්න, decisions ගන්න computer එකට ඉගෙන ගන්න පුළුවන් වෙනවා.

හිතන්න පොඩි ළමයෙක්ට සත්තු අඳුනගන්න උගන්වනවා වගේ. අපි ළමයට බල්ලෝ ගොඩකගේ රූප පෙන්නලා කියනවා 'මේ බල්ලෙක්', බළලුන් ගොඩකගේ රූප පෙන්නලා කියනවා 'මේ බළලෙක්' කියලා. ටික කාලයක් යනකොට ළමයාට පුළුවන් වෙනවා, කලින් නොදැකපු බල්ලෙක් දැක්කම 'මේ බල්ලෙක්' කියලා හඳුනගන්න. Machine Learning algorithms වැඩ කරන්නෙත් හරියටම ඒ වගේ තමයි. Data තමයි අපේ 'ගුරුවරයා'.

Machine Learning වල ප්‍රධාන වර්ග: Supervised vs Unsupervised Learning

Machine Learning algorithms ප්‍රධාන වශයෙන් වර්ග දෙකකට බෙදන්න පුළුවන්. ඒ තමයි Supervised Learning සහ Unsupervised Learning. මේ දෙකේ වෙනස හරියට තේරුම් ගන්න එක ML වල වැදගත්ම පියවරක්.

Supervised Learning

Supervised Learning කියන්නේ, අපි labeled data වලින් computer එකට උගන්වන ක්‍රමයක්. 'Labeled data' කියන්නේ, අපිට කලින්ම result එක (output) දන්නා data. හරියට exam එකකට කලින් model papers වලට answers දීලා තියෙනවා වගේ.

උදාහරණයක්:

  • ඔබට house prices predict කරන්න ඕන නම්, ඔබ කලින් විකුණපු ගෙවල් වල size, location, bedrooms ගාන වගේ දේවල් එක්ක, ඒ ගෙවල් විකුණපු මිල (price) කියන output එකත් එක්ක data set එකක් algorithm එකට දෙනවා.
  • Spam email detect කරන්න නම්, ඔබට email content එක එක්ක 'Spam' ද 'Not Spam' ද කියන label එකත් එක්ක data set එකක් දෙනවා.

මේක හරියට ගුරුවරයෙක් ඉන්න පන්තියක ඉගෙන ගන්නවා වගේ. ගුරුවරයා (labels) අපිට නිවැරදි පිළිතුරු මොනවද කියලා කියලා දෙනවා. Supervised Learning වල ප්‍රධාන tasks දෙකක් තමයි Regression සහ Classification.

Unsupervised Learning

Unsupervised Learning කියන්නේ, අපි unlabeled data වලින් computer එකට උගන්වන ක්‍රමයක්. මෙතනදී computer එකට කලින්ම result එක (output) මොකක්ද කියලා අපි කියලා දෙන්නේ නෑ. Computer එකට සිද්ධ වෙනවා දීපු data එක ඇතුළේ තියෙන patterns, similarities, differences තමන්ම හොයාගන්න.

උදාහරණයක්:

  • ඔබට customers ලා පිරිසක් Group කරන්න (segment) ඕන නම්, ඔබ ඒ customers ලාගේ buying patterns, demographics වගේ data algorithm එකට දෙනවා. ඊටපස්සේ algorithm එක තමන්ටම සමාන customers ලා එක group එකකට දාලා, විවිධ customer groups හදනවා. මෙතනදී 'මේ customer group එක' කියලා කලින්ම label එකක් නෑ.

මේක හරියට ගුරුවරයෙක් නැති පන්තියක පොත්පත් දිහා බලාගෙන තමන්ම ඉගෙන ගන්නවා වගේ. Unsupervised Learning වල ප්‍රධාන task එකක් තමයි Clustering.

Supervised vs Unsupervised Learning - වෙනස සරලව

ලක්ෂණය (Feature) Supervised Learning Unsupervised Learning
Data වර්ගය Labeled data (input + output) Unlabeled data (input only)
ඉලක්කය (Goal) Known output එකක් predict කිරීම Data ඇතුළේ තියෙන patterns, structures සොයා ගැනීම
පොදු tasks Regression, Classification Clustering, Dimensionality Reduction
උදාහරණ House price prediction, Spam detection Customer segmentation, Anomaly detection

Supervised Learning එකේ යෙදීම්: Regression සහ Classification

Regression: අඛණ්ඩ අගයන් (Continuous Values) පුරෝකථනය කිරීම

Regression කියන්නේ Supervised Learning වලදී, අපිට අඛණ්ඩ අගයක් (continuous value) predict කරන්න අවශ්‍ය වුණාම භාවිතා කරන ක්‍රමයක්. උදාහරණයක් ලෙස, ගෙදරක මිලක් (මිල ගණන් අඛණ්ඩ අගයන්), පුද්ගලයෙකුගේ වයස, තොග මිලක්, කාලගුණ අනාවැකි (උෂ්ණත්වය) වගේ දේවල්.

සරලව කියනවා නම්, 'කීයක්ද?' (How much?) නැත්නම් 'කීයටද?' (At what price?) වගේ ප්‍රශ්න වලට උත්තර දෙන්නේ Regression algorithms.

Regression සඳහා සරල Python උදාහරණයක් (Linear Regression)

අපි හිතමු ගෙවල් වල වර්ග අඩි ගාන (square footage) අනුව මිල (price) predict කරනවා කියලා. Python වල scikit-learn library එක මේ වගේ දේවල් වලට හරිම ප්‍රයෝජනවත්.


import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

# Sample Data: square footage (X) and prices (y)
# මේවා තමයි අපේ 'labeled data'. වර්ග අඩි ගාන (input) එක්ක මිල (output) තියෙනවා.
X = np.array([["1000"], ["1200"], ["1500"], ["1800"], ["2000"]])
y = np.array([["200000"], ["250000"], ["300000"], ["380000"], ["420000"]])

# Create a Linear Regression model
model = LinearRegression()

# Train the model using the data
# මේක තමයි 'learning' process එක
model.fit(X, y)

# Predict the price for a new house (e.g., 1600 sq ft)
new_house_sqft = np.array([["1600"]])
predicted_price = model.predict(new_house_sqft)

print(f"ගෙදර වර්ග අඩි {new_house_sqft[0][0]} නම්, පුරෝකථනය කළ මිල: ${predicted_price[0][0]:,.2f}")

# Plotting for visualization (optional)
plt.scatter(X, y, color='blue', label='Actual Prices')
plt.plot(X, model.predict(X), color='red', label='Regression Line')
plt.scatter(new_house_sqft, predicted_price, color='green', marker='X', s=100, label='Predicted Price')
plt.xlabel('Square Footage')
plt.ylabel('Price')
plt.title('House Price Prediction (Simple Linear Regression)')
plt.legend()
plt.grid(True)
plt.show()

මේ කෝඩ් එකෙන් අපිට පුළුවන් දීපු දත්ත වලින් වර්ග අඩි ගානට අනුව මිලක් predict කරන්න. fit() කියන function එකෙන් තමයි model එක data වලින් ඉගෙන ගන්නේ. predict() වලින් අලුත් data වලට අගයන් පුරෝකථනය කරනවා.

Classification: වර්ගීකරණය කිරීම (Categorical Labels)

Classification කියන්නේ Supervised Learning වලදී, අපිට data එකක් විවිධ වර්ග වලට (categories/labels) බෙදන්න අවශ්‍ය වුණාම භාවිතා කරන ක්‍රමයක්. උදාහරණයක් ලෙස, email එකක් spam ද නැද්ද (spam/not spam), රෝගයක් තිබෙනවාද නැද්ද (sick/healthy), ඡායාරූපයක ඉන්නේ බල්ලෙක්ද බළලෙක්ද (dog/cat) වගේ දේවල්.

සරලව කියනවා නම්, 'මේක මොන වර්ගයටද අයිති?' (What category does this belong to?) වගේ ප්‍රශ්න වලට උත්තර දෙන්නේ Classification algorithms.

Classification සඳහා සරල Python උදාහරණයක් (Logistic Regression)

අපි හිතමු student කෙනෙක් විභාගයක් pass වෙයිද නැත්නම් fail වෙයිද කියලා predict කරනවා කියලා, ඒ student ගේ study hours අනුව. (Output එක: Pass/Fail - මේවා categorical labels).


import numpy as np
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt

# Sample Data: Study hours (X) and Exam Result (y: 0 for Fail, 1 for Pass)
X = np.array([["1"], ["2"], ["3"], ["4"], ["5"], ["6"], ["7"], ["8"], ["9"], ["10"]])
y = np.array([0, 0, 0, 1, 0, 1, 1, 1, 1, 1]) # 0=Fail, 1=Pass

# Create a Logistic Regression model
model = LogisticRegression()

# Train the model
model.fit(X, y)

# Predict for new study hours (e.g., 2.5 hours and 7.5 hours)
new_study_hours = np.array([["2.5"], ["7.5"]])
predictions = model.predict(new_study_hours)
probabilities = model.predict_proba(new_study_hours)

print(f"Study hours {new_study_hours[0][0]} නම්, පුරෝකථනය: {'Pass' if predictions[0] == 1 else 'Fail'} (Prob: {probabilities[0][1]*100:.2f}%) ")
print(f"Study hours {new_study_hours[1][0]} නම්, පුරෝකථනය: {'Pass' if predictions[1] == 1 else 'Fail'} (Prob: {probabilities[1][1]*100:.2f}%) ")

# Plotting for visualization
plt.scatter(X, y, color='blue', label='Actual Results (0=Fail, 1=Pass)')
x_test = np.linspace(0, 10, 300).reshape(-1, 1)
y_prob = model.predict_proba(x_test)[:, 1]
plt.plot(x_test, y_prob, color='red', label='Pass Probability')
plt.xlabel('Study Hours')
plt.ylabel('Exam Result (0=Fail, 1=Pass)')
plt.title('Exam Pass/Fail Prediction (Logistic Regression)')
plt.legend()
plt.grid(True)
plt.show()

මේ කෝඩ් එකෙන් student කෙනෙක් යම් study hours ගානක් දුන්නොත් pass වෙයිද fail වෙයිද කියලා predict කරන්න පුළුවන්. predict_proba() කියන එකෙන් අපිට පුළුවන් 'pass' වෙන්න තියෙන සම්භාවිතාව (probability) කීයද කියලා බලන්න.

Unsupervised Learning එකේ යෙදීම්: Clustering

Clustering: දත්ත කාණ්ඩ කිරීම

Clustering කියන්නේ Unsupervised Learning වලදී, අපිට unlabeled data set එකක් දීලා, ඒ data points වල තියෙන සමානකම් (similarities) අනුව ඒවා groups වලට (clusters) වෙන් කරන්න අවශ්‍ය වුණාම භාවිතා කරන ක්‍රමයක්. මෙතනදී අපි කලින්ම කියලා දෙන්නේ නෑ මොන data point එක මොන group එකටද අයිති වෙන්න ඕන කියලා.

උදාහරණයක් ලෙස, online shopping කරන customers ලාගේ මිලදී ගැනීමේ රටා (buying patterns) අනුව වෙනම customer groups (උදා: 'budget shoppers', 'luxury buyers') හදන එක, සමාන DNA රටා තියෙන අය එකට කණ්ඩායම් කරන එක, news articles සමාන මාතෘකා අනුව වර්ග කරන එක වගේ දේවල්.

සරලව කියනවා නම්, 'මේවා එකට අයිති මොනවාද?' (What belongs together?) වගේ ප්‍රශ්න වලට උත්තර දෙන්නේ Clustering algorithms.

Clustering සඳහා සරල Python උදාහරණයක් (K-Means Clustering)

අපි හිතමු customers ලා පිරිසකගේ shopping habits (පහසුව සඳහා අපි 'Loyalty Score' එකක් සහ 'Spending Score' එකක් උපකල්පනය කරමු) අනුව ඔවුන්ව groups වලට වෙන් කරනවා කියලා.


import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

# Sample Data: Customer Loyalty Score (X[:,0]) and Spending Score (X[:,1])
# මේක 'unlabeled data'. අපි කලින්ම දන්නේ නෑ මේ customers ලා මොන group එකටද අයිති වෙන්නේ කියලා.
X = np.array([
    [1, 1], [1.5, 2], [3, 4], [5, 7], [3.5, 5],
    [6, 8], [7, 9], [8, 10], [9, 8], [1.2, 0.8],
    [0.5, 1.5], [4.5, 6], [7.5, 7.5], [2.5, 3.5], [6.5, 9.5]
])

# Define the number of clusters we want (e.g., 3 customer groups)
k = 3

# Create a KMeans model
model = KMeans(n_clusters=k, random_state=0, n_init=10) # n_init=10 to suppress future warning

# Train the model (find clusters)
# මෙතනදී model එක තමන්ටම data ඇතුළේ තියෙන patterns අනුව groups හදනවා
model.fit(X)

# Get the cluster assignments for each data point
labels = model.labels_

# Get the coordinates of the cluster centers
centroids = model.cluster_centers_

print("Customer Cluster Assignments:", labels)
print("Cluster Centroids (Centers):")
for i, centroid in enumerate(centroids):
    print(f"  Cluster {i}: Loyalty={centroid[0]:.2f}, Spending={centroid[1]:.2f}")

# Plotting for visualization
plt.figure(figsize=(8, 6))
colors = ['red', 'blue', 'green', 'purple', 'orange']
for i in range(k):
    plt.scatter(X[labels == i, 0], X[labels == i, 1], s=50, c=colors[i], label=f'Cluster {i}')
plt.scatter(centroids[:, 0], centroids[:, 1], s=200, marker='*', c='black', label='Centroids')
plt.xlabel('Loyalty Score')
plt.ylabel('Spending Score')
plt.title(f'Customer Segmentation using K-Means Clustering ({k} Clusters)')
plt.legend()
plt.grid(True)
plt.show()

මේ කෝඩ් එකෙන්, KMeans algorithm එක භාවිතා කරලා, අපේ customers ලාව, ඔවුන්ගේ Loyalty Score සහ Spending Score මත පදනම්ව, groups 3 කට වෙන් කරගන්න පුළුවන්. labels_ කියන එකෙන් අපිට පුළුවන් එක් එක් customer අයිති මොන group එකටද කියලා බලාගන්න.

නිගමනය (Conclusion)

ඉතින් යාලුවනේ, මේ tutorial එකෙන් අපි Machine Learning වල තියෙන මූලිකම සංකල්ප කිහිපයක් ගැන කතා කළා. Supervised Learning සහ Unsupervised Learning කියන ප්‍රධාන වර්ග දෙක, ඒ යටතේ එන Regression, Classification, සහ Clustering කියන tasks මොනවද, ඒවා කොහොමද වැඩ කරන්නේ කියලා ඔයාලට දැන් පැහැදිලි ඇති කියලා හිතනවා. ඒ වගේම, Python වල scikit-learn library එක පාවිච්චි කරලා මේවා practical විදියට implement කරන්නේ කොහොමද කියලත් අපි සරල උදාහරණ කිහිපයකින් බැලුවා.

Machine Learning කියන්නේ හරිම fascinating ක්ෂේත්‍රයක්. මේවා තමයි අනාගතයේ ලෝකය ගෙනියන්නේ. මේ දැනුම ඔයාලගේ Machine Learning ගමනට හොඳ ආරම්භයක් වෙයි කියලා මම විශ්වාස කරනවා. වැදගත්ම දේ තමයි මේ concepts තේරුම් අරන්, පොඩි පොඩි projects කරලා practice කරන එක.

ඔයාලා මේ concepts තේරුම් ගන්න මේ tutorial එක උදව් වුණාද? ඔයාලගේ අදහස්, ප්‍රශ්න, අත්දැකීම් පහලින් Comment කරන්න. තවත් මොන වගේ topics ගැනද ඔයාලට ඉගෙන ගන්න ඕන කියලා කියන්න. මේ වගේ තවත් tutorials එක්ක නැවත හමුවෙමු!