Dataset dapat diunduh di link berikut ini:
https://drive.google.com/file/d/1pQxtljlNVh0DHYg-Ye7dtpDTlFceHVfa/view
Importing the Depedencies
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
#loading the dataset to a pandas Dataframe
sonar_data = pd.read_csv('sonar_data.csv', header=None)
sonar_data.head()
# number of rows and columns
sonar_data.shape
# describe --> statistical measures of the data
sonar_data.describe()
sonar_data[60].value_counts()
M –> Mine
R –> Rock
sonar_data.groupby(60).mean()
# separating data and Labels
X = sonar_data.drop(columns=60, axis=1)
Y = sonar_data[60]
print(X)
print(Y)
Training and Test Data
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.1, stratify=Y, random_state=1)
print(X.shape, X_train.shape, X_test.shape)
print(X_train)
print(Y_train)
Model Training –> Logistic Regression
model = LogisticRegression()
# training the Logistic Regression model with training data
model.fit(X_train, Y_train)
Model Evaluation
# accuracy on training data
X_train_prediction = model.predict(X_train)
training_data_accuracy = accuracy_score(X_train_prediction, Y_train)
print('Accuracy on training data : ', training_data_accuracy)
# accuracy on test data
X_test_prediction = model.predict(X_test)
test_data_accuracy = accuracy_score(X_test_prediction, Y_test)
print('Accuracy on test data : ', test_data_accuracy)
Making a Predictive System
input_data = (0.0056,0.0267,0.0221,0.0561,0.0936,0.1146,0.0706,0.0996,0.1673,0.1859,0.2481,0.2712,0.2934,0.2637,0.1880,0.1405,0.2028,0.2613,0.2778,0.3346,0.3830,0.4003,0.5114,0.6860,0.7490,0.7843,0.9021,1.0000,0.8888,0.6511,0.6083,0.4463,0.2948,0.1729,0.1488,0.0801,0.1770,0.1382,0.2404,0.2046,0.1970,0.2778,0.1377,0.0685,0.0664,0.1665,0.1807,0.1245,0.0516,0.0044,0.0185,0.0072,0.0055,0.0074,0.0068,0.0084,0.0037,0.0024,0.0034,0.0007)
# 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')
The following two tabs change content below.
Seorang pemuda yang mempunyai hobi menulis, membaca, dan bermusik. Tertarik dengan bidang ilmu komputer untuk memecahkan beberapa persoalan. Sejak tahun 2018 bekerja di BMKG di Direktorat Data dan Komputasi. Blog tentang Teknologi: Mahisadev.com
Latest posts by mahisaajy (see all)
- Running Job MPI di LiCO HPC BMKG (Step-by-Step) - February 26, 2026
- Panduan Instalasi FortiClient VPN di Linux (Mint/Ubuntu) - February 25, 2026
- Instalasi SAC (Seismic Analysis Code) di MAC untuk Analisis Seismik - December 5, 2024



