forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonar.py
69 lines (53 loc) · 2.25 KB
/
sonar.py
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
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Load the dataset
data = pd.read_csv('sonar_data.csv')
# Display the shape of the dataset
print(data.shape())
# Display information about the dataset
data.info()
# Check for missing values in the dataset
print(data.isnull().sum())
# Get statistical summary of the dataset
print(data.describe())
# Display the columns of the dataset
print(data.columns)
# Plot the count of the target variable (assuming it is at index 60)
sns.countplot(data[60])
plt.show()
# Calculate the mean of each feature grouped by the target variable
data.groupby(60).mean()
# Separate features and target variable
x = data.drop(60, axis=1) # Features
y = data[60] # Target variable
# Split the dataset into training and testing sets (80% train, 20% test)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=42)
# Logistic Regression model
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(x_train, y_train) # Train the model
y_pred1 = lr.predict(x_test) # Make predictions
print("Logistic Regression Accuracy:", accuracy_score(y_test, y_pred1)) # Calculate accuracy
# K-Nearest Neighbors model
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(x_train, y_train) # Train the model
y_pred2 = knn.predict(x_test) # Make predictions
print("KNN Accuracy:", accuracy_score(y_test, y_pred2)) # Calculate accuracy
# Random Forest model
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier()
rf.fit(x_train, y_train) # Train the model
y_pred3 = rf.predict(x_test) # Make predictions
print("Random Forest Accuracy:", accuracy_score(y_test, y_pred3)) # Calculate accuracy
# Stochastic Gradient Descent model
from sklearn.linear_model import SGDClassifier
sgd = SGDClassifier()
# Train using partial_fit
for i in range(len(x_train)): # Corrected from 'ramge' to 'range'
sgd.partial_fit(x_train[i:i+1], y_train[i:i+1], classes=['R', 'M'])
# Calculate and print the score on the test set
score = sgd.score(x_test, y_test)
print("SGD Classifier Accuracy:", score)