-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm.py
46 lines (36 loc) · 1.4 KB
/
svm.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
import time
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import warnings
warnings.filterwarnings("ignore")
# Load data from CSV file
file_path = 'data.csv' # Replace with your actual file path
df = pd.read_csv(file_path)
# Feature engineering (if needed)
# For example, adding a new feature 'mean_radius_squared'
df['mean_radius_squared'] = df['mean radius'] ** 2
# Split the data into features (X) and target variable (y)
X = df.drop('diagnosis', axis=1)
y = df['diagnosis']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
start_time = time.time()
# Train the Support Vector Machine (SVM) model
svm_model = SVC(kernel='linear', random_state=42)
svm_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = svm_model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
classification_rep = classification_report(y_test, y_pred)
# Display evaluation metrics
print("Accuracy:", accuracy)
print("\nConfusion Matrix:")
print(conf_matrix)
print("\nClassification Report:")
print(classification_rep)
end_time = time.time()
elapsed_time = end_time - start_time