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

ML Use Case 1 Rock vs Mine Prediction

මේක ඔයාගෙ පළවෙනි සම්පූර්ණ, සැබෑ ML project එක — sonar signals ඇතුලට, yes/no පිළිතුරක් එළියට. Part 1 එකේ ඉගෙන ගත්ත ඔක්කොම මුල්වතාවට එකට එකතු වෙන්නෙ මෙතන.

This is your first complete, real ML project — sonar signals in, a yes/no answer out. Everything from Part 1 comes together here for the first time.

8 May 20265 min readSelf Guide · Stage 13#Python & Data Basics#Hands-on Projects#Machine learning

Lesson 22 of 64 · Part 1 · Python & Data Basics · Hands-on Projects · bilingual notes (English + සිංහල)

Why this matters. This is your first complete, real ML project — sonar signals in, a yes/no answer out. Everything from Part 1 comes together here for the first time.

සිංහලෙන්: මේක ඔයාගෙ පළවෙනි සම්පූර්ණ, සැබෑ ML project එක — sonar signals ඇතුලට, yes/no පිළිතුරක් එළියට. Part 1 එකේ ඉගෙන ගත්ත ඔක්කොම මුල්වතාවට එකට එකතු වෙන්නෙ මෙතන.

What you'll use this for. The pattern here — load, clean, split, train, evaluate — is the exact same five-step pattern every project in this entire course follows.

සිංහලෙන්: මෙතන තියෙන pattern එක — load, clean, split, train, evaluate — මුළු course එකේම ඕනම project එකක් follow කරන step 5 එකම pattern එක.

Key principles

  • A binary classifier just draws a boundary between two groups. Logistic Regression here isn't predicting a number — it's finding the line (in many dimensions) that best separates "rock" readings from "mine" readings.
    Binary classifier එකක් කරන්නෙ group දෙකක් අතර boundary එකක් අඳින එක විතරයි

Walkthrough

Importing the Dependencies

ExamplePython
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

Data Collection and Data Processing

ExamplePython
#loading the dataset to a pandas Dataframe
sonar_data = pd.read_csv('/content/sonar data.csv', header=None)
ExamplePython
sonar_data.head()
0123456…
00.02000.03710.04280.02070.09540.09860.1539…
10.04530.05230.08430.06890.11830.25830.2156…
20.02620.05820.10990.10830.09740.22800.2431…
30.01000.01710.06230.02050.02050.03680.1098…
40.07620.06660.04810.03940.05900.06490.1209…
ExamplePython
# number of rows and columns
sonar_data.shape

Output

Output
(208, 61)
ExamplePython
sonar_data.describe()  #describe --> statistical measures of the data
0123456…
count208.000000208.000000208.000000208.000000208.000000208.000000208.000000…
mean0.0291640.0384370.0438320.0538920.0752020.1045700.121747…
std0.0229910.0329600.0384280.0465280.0555520.0591050.061788…
min0.0015000.0006000.0015000.0058000.0067000.0102000.003300…
25%0.0133500.0164500.0189500.0243750.0380500.0670250.080900…
50%0.0228000.0308000.0343000.0440500.0625000.0921500.106950…

… 2 more rows

ExamplePython
sonar_data[60].value_counts()

Output

Output
M    111
R     97
Name: 60, dtype: int64

M --> Mine

R --> Rock

ExamplePython
sonar_data.groupby(60).mean()
0123456…
60…
M0.0349890.0455440.0507200.0647680.0867150.1118640.128359…
R0.0224980.0303030.0359510.0414470.0620280.0962240.114180…
ExamplePython
# separating data and Labels
X = sonar_data.drop(columns=60, axis=1)
Y = sonar_data[60]
ExamplePython
print(X)
print(Y)

Output

Output
         0       1       2       3   ...      56      57      58      59
0    0.0200  0.0371  0.0428  0.0207  ...  0.0180  0.0084  0.0090  0.0032
1    0.0453  0.0523  0.0843  0.0689  ...  0.0140  0.0049  0.0052  0.0044
2    0.0262  0.0582  0.1099  0.1083  ...  0.0316  0.0164  0.0095  0.0078
3    0.0100  0.0171  0.0623  0.0205  ...  0.0050  0.0044  0.0040  0.0117
4    0.0762  0.0666  0.0481  0.0394  ...  0.0072  0.0048  0.0107  0.0094
..      ...     ...     ...     ...  ...     ...     ...     ...     ...
203  0.0187  0.0346  0.0168  0.0177  ...  0.0065  0.0115  0.0193  0.0157
204  0.0323  0.0101  0.0298  0.0564  ...  0.0034  0.0032  0.0062  0.0067
205  0.0522  0.0437  0.0180  0.0292  ...  0.0140  0.0138  0.0077  0.0031
206  0.0303  0.0353  0.0490  0.0608  ...  0.0034  0.0079  0.0036  0.0048
207  0.0260  0.0363  0.0136  0.0272  ...  0.0040  0.0036  0.0061  0.0115

[208 rows x 60 columns]
0      R
1      R
2      R
3      R
… (8 more lines)

Training and Test data

ExamplePython
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.1, stratify=Y, random_state=1)
ExamplePython
print(X.shape, X_train.shape, X_test.shape)

Output

Output
(208, 60) (187, 60) (21, 60)
ExamplePython
print(X_train)
print(Y_train)

Output

Output
         0       1       2       3   ...      56      57      58      59
115  0.0414  0.0436  0.0447  0.0844  ...  0.0141  0.0077  0.0246  0.0198
38   0.0123  0.0022  0.0196  0.0206  ...  0.0113  0.0058  0.0047  0.0071
56   0.0152  0.0102  0.0113  0.0263  ...  0.0037  0.0011  0.0034  0.0033
123  0.0270  0.0163  0.0341  0.0247  ...  0.0138  0.0094  0.0105  0.0093
18   0.0270  0.0092  0.0145  0.0278  ...  0.0120  0.0132  0.0070  0.0088
..      ...     ...     ...     ...  ...     ...     ...     ...     ...
140  0.0412  0.1135  0.0518  0.0232  ...  0.0095  0.0225  0.0098  0.0085
5    0.0286  0.0453  0.0277  0.0174  ...  0.0057  0.0027  0.0051  0.0062
154  0.0117  0.0069  0.0279  0.0583  ...  0.0020  0.0062  0.0026  0.0052
131  0.1150  0.1163  0.0866  0.0358  ...  0.0190  0.0141  0.0068  0.0086
203  0.0187  0.0346  0.0168  0.0177  ...  0.0065  0.0115  0.0193  0.0157

[187 rows x 60 columns]
115    M
38     R
56     R
123    M
… (8 more lines)

Model Training --> Logistic Regression

ExamplePython
model = LogisticRegression()
ExamplePython
#training the Logistic Regression model with training data
model.fit(X_train, Y_train)

Output

Output
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                   intercept_scaling=1, l1_ratio=None, max_iter=100,
                   multi_class='auto', n_jobs=None, penalty='l2',
                   random_state=None, solver='lbfgs', tol=0.0001, verbose=0,
                   warm_start=False)

Model Evaluation

ExamplePython
#accuracy on training data
X_train_prediction = model.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)
ExamplePython
print('Accuracy on training data : ', training_data_accuracy)

Output

Output
Accuracy on training data :  0.8342245989304813
ExamplePython
#accuracy on test data
X_test_prediction = model.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)
ExamplePython
print('Accuracy on test data : ', test_data_accuracy)

Output

Output
Accuracy on test data :  0.7619047619047619

Making a Predictive System

ExamplePython
input_data = (0.0307,0.0523,0.0653,0.0521,0.0611,0.0577,0.0665,0.0664,0.1460,0.2792,0.3877,0.4992,0.4981,0.4972,0.5607,0.7339,0.8230,0.9173,0.9975,0.9911,0.8240,0.6498,0.5980,0.4862,0.3150,0.1543,0.0989,0.0284,0.1008,0.2636,0.2694,0.2930,0.2925,0.3998,0.3660,0.3172,0.4609,0.4374,0.1820,0.3376,0.6202,0.4448,0.1863,0.1420,0.0589,0.0576,0.0672,0.0269,0.0245,0.0190,0.0063,0.0321,0.0189,0.0137,0.0277,0.0152,0.0052,0.0121,0.0124,0.0055)

# changing the input_data to a numpy array
input_data_as_numpy_array = np.asarray(input_data)

# reshape the np array as we are predicting for one instance
input_data_reshaped = input_data_as_numpy_array.reshape(1,-1)

prediction = model.predict(input_data_reshaped)
print(prediction)

if (prediction[0]=='R'):
  print('The object is a Rock')
else:
  print('The object is a mine')

Output

Output
['M']
The object is a mine

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