# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="6Si1xoyKJfR7"
# Importing the dependencies

# + id="qxT_JlHfpYW3" executionInfo={"status": "ok", "timestamp": 1636634885230, "user_tz": -330, "elapsed": 1266, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}}
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# + [markdown] id="pgWcxd8hM0By"
# Data collection and Data processing

# + id="ksiCftdCMJnr" executionInfo={"status": "ok", "timestamp": 1636634886462, "user_tz": -330, "elapsed": 11, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}}
sonar_data=pd.read_csv("/content/Copy of sonar data.csv",header=None)

# + colab={"base_uri": "https://localhost:8080/", "height": 226} id="8hribBItNQtU" executionInfo={"status": "ok", "timestamp": 1636634887173, "user_tz": -330, "elapsed": 719, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="c595e1b2-3be1-4ba3-9a40-c38b025ee2b8"
sonar_data.head()

# + colab={"base_uri": "https://localhost:8080/"} id="iki7JXNGNkNg" executionInfo={"status": "ok", "timestamp": 1636621251418, "user_tz": -330, "elapsed": 805, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="80568b40-44af-4c7d-d996-a549e921eaf6"
# number of rpows and columns
sonar_data.shape

# + colab={"base_uri": "https://localhost:8080/", "height": 320} id="RczX4XsBOQ-t" executionInfo={"status": "ok", "timestamp": 1636621284269, "user_tz": -330, "elapsed": 631, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="08561a6a-af23-44f4-9b37-2e3f7c0b65b3"
sonar_data.describe()  #describe --> statstical measures of the data

# + colab={"base_uri": "https://localhost:8080/"} id="eCs-w6DdOu0A" executionInfo={"status": "ok", "timestamp": 1636621432066, "user_tz": -330, "elapsed": 571, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="1ae33900-1679-42e9-e28e-2a83f739e181"
sonar_data[60].value_counts()

# + [markdown] id="43hVSqmbPn6d"
# M-->Mine
# R-->Rock

# + colab={"base_uri": "https://localhost:8080/", "height": 163} id="OVGyP6gqPSwB" executionInfo={"status": "ok", "timestamp": 1636621898377, "user_tz": -330, "elapsed": 587, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="6bbfb14d-68f1-4132-fd8a-4b197baed178"
sonar_data.groupby(60).mean()

# + id="YmV6FeWxP2vI" executionInfo={"status": "ok", "timestamp": 1636622158977, "user_tz": -330, "elapsed": 550, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}}
# separating data and Labels
X = sonar_data.drop(columns=60,axis=1)
Y = sonar_data[60]

# + colab={"base_uri": "https://localhost:8080/"} id="in8JsZ1rSEco" executionInfo={"status": "ok", "timestamp": 1636622183671, "user_tz": -330, "elapsed": 565, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="fd14bff4-ebee-48d9-9fd7-a58ef12f0788"
print(X,Y)

# + [markdown] id="LIp4Yj06SSVb"
# Training and Testing data

# + id="uvwtOc94SHzi" executionInfo={"status": "ok", "timestamp": 1636622523526, "user_tz": -330, "elapsed": 590, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}}
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.1,stratify=Y,random_state=1)

# + colab={"base_uri": "https://localhost:8080/"} id="61IRn7a9Sjbh" executionInfo={"status": "ok", "timestamp": 1636622512140, "user_tz": -330, "elapsed": 8, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="0bcd7976-36f4-4d79-c5d4-15bda11fd8e1"
print(X.shape,X_train.shape,X_test.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="xMPjxuqUTWsv" executionInfo={"status": "ok", "timestamp": 1636622590304, "user_tz": -330, "elapsed": 794, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="db345d58-fa99-4f52-b355-1e6483ea0968"
print(Y.shape,Y_train.shape,Y_test.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="wp0ZQqNPTtsn" executionInfo={"status": "ok", "timestamp": 1636622622485, "user_tz": -330, "elapsed": 597, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="0a6c73a0-30ba-4dbd-da99-dcafaea6f6b9"
print(X_train,Y_train)

# + [markdown] id="QVyJAfs2UC70"
# Model Regression--> Logistic Regression

# + id="x5sqdKhlT1Zz" executionInfo={"status": "ok", "timestamp": 1636622764506, "user_tz": -330, "elapsed": 691, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}}
model=LogisticRegression()

# + colab={"base_uri": "https://localhost:8080/"} id="gXJFgjUuUX5j" executionInfo={"status": "ok", "timestamp": 1636622788305, "user_tz": -330, "elapsed": 656, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="ed432781-d52d-48a8-89c0-1b94a36a6081"
model.fit(X_train,Y_train)

# + [markdown] id="Y2WCbZiyUjRc"
# Model Evaluation

# + id="9kJBqhJMUd2w" executionInfo={"status": "ok", "timestamp": 1636623274709, "user_tz": -330, "elapsed": 616, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}}
#accuracy on training data
X_train_prediction = model.predict(X_train)
training_data_accuracy=accuracy_score(X_train_prediction,Y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="p2h4PSpuUsDw" executionInfo={"status": "ok", "timestamp": 1636623277138, "user_tz": -330, "elapsed": 13, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="979eb549-7677-4abf-934b-e0561d990918"
print("accuracy on training data :",training_data_accuracy.round(2))

# + id="kdeMhOSPVsj_" executionInfo={"status": "ok", "timestamp": 1636623696205, "user_tz": -330, "elapsed": 592, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}}
#accuracy of test data
X_test_prediction=model.predict(X_test)
test_data_accuracy=accuracy_score(X_test_prediction,Y_test)

# + colab={"base_uri": "https://localhost:8080/"} id="BK8vC5NPXy7n" executionInfo={"status": "ok", "timestamp": 1636623743532, "user_tz": -330, "elapsed": 645, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="6a3108ef-bd26-4c7e-fc0c-95ecbfb35041"
print("accuracy od test data :",test_data_accuracy)

# + colab={"base_uri": "https://localhost:8080/"} id="X2dlBg5yYHRr" executionInfo={"status": "ok", "timestamp": 1636623883859, "user_tz": -330, "elapsed": 585, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="fb46aa00-7ef2-445c-c9e3-8dce8bac3fca"


# + colab={"base_uri": "https://localhost:8080/"} id="eZI1hfABYpju" executionInfo={"status": "ok", "timestamp": 1636625781129, "user_tz": -330, "elapsed": 795, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="d8aef585-98a2-4298-a6f7-701b292e0d22"
input_data=(0.0453,0.0523,0.0843,0.0689,0.1183,0.2583,0.2156,0.3481,0.3337,0.2872,0.4918,0.6552,0.6919,0.7797,0.7464,0.9444,1.0000,0.8874,0.8024,0.7818,0.5212,0.4052,0.3957,0.3914,0.3250,0.3200,0.3271,0.2767,0.4423,0.2028,0.3788,0.2947,0.1984,0.2341,0.1306,0.4182,0.3835,0.1057,0.1840,0.1970,0.1674,0.0583,0.1401,0.1628,0.0621,0.0203,0.0530,0.0742,0.0409,0.0061,0.0125,0.0084,0.0089,0.0048,0.0094,0.0191,0.0140,0.0049,0.0052,0.0044)


#chaning the input_data to 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')

# + colab={"base_uri": "https://localhost:8080/"} id="DjizaIG8eq7y" executionInfo={"status": "ok", "timestamp": 1636625785979, "user_tz": -330, "elapsed": 641, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="228b025c-e9ef-4dfc-8a32-1ec20243ae23"
if (prediction[0]=='R'):
  print('The object is a Rock')
else:
  print('The object is a mine')

# + colab={"base_uri": "https://localhost:8080/"} id="Ub4U0AYifnm5" executionInfo={"status": "ok", "timestamp": 1636625814513, "user_tz": -330, "elapsed": 653, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "09496595787393543044"}} outputId="6d4f4d50-fd2f-4c49-f512-1a6920f4d522"
prediction

# + id="BoBH_JHwgAo2"


StarcoderdataJupyterScriptsDedupFiltered