forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrock_or_mine.py
More file actions
156 lines (122 loc) · 2.82 KB
/
rock_or_mine.py
File metadata and controls
156 lines (122 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Project-1
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1uJlXbRnd1mLtAv8fp2wDPVeWG1QHKbUw
Importing libraries
"""
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
"""Data collection + processing"""
# loading the dataset to a panda df
sonar_data = pd.read_csv("/content/Copy of sonar data.csv", header=None)
sonar_data.head()
# number of rows and column
sonar_data.shape
sonar_data.describe()
sonar_data[60].value_counts()
"""M-->mine
R-->rock
"""
sonar_data.groupby(60).mean()
# seperate 60th column
X = sonar_data.drop(columns=60, axis=1) ##axis = 0 for rows
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 reg
"""
model = LogisticRegression()
# training the Logistic Regression model with training data
model.fit(X_train, Y_train)
"""MODEL evaluation
"""
# accuracy of 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)
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")