-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvm.py
More file actions
75 lines (58 loc) · 2.06 KB
/
Copy pathsvm.py
File metadata and controls
75 lines (58 loc) · 2.06 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
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import seaborn as sns
from sklearn.svm import SVC
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve
from sklearn.metrics import confusion_matrix
import time
# Importing dataset
df = pd.read_csv('/Users/macbookpro/Documents/umich/CIS405/Project/diabetes_prediction_dataset.csv')
# Feature engineering
df['is_male'] = (df['gender'] == 'Male').astype(int)
df = df.drop(columns=['gender'])
df['is_smoker'] = df['smoking_history'].isin(['current', 'former']).astype(int)
df = df.drop(columns=['smoking_history'])
X = df.drop('diabetes', axis=1)
y = df['diabetes']
# Display the modified DataFrame
print(df.head())
# Splitting the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Standardizing the features
scaling_x = StandardScaler()
X_train = scaling_x.fit_transform(X_train)
X_test = scaling_x.transform(X_test)
# Using SVM with linear kernel
svc = SVC(kernel='linear', probability=True)
# Start the timer
start_time = time.time()
# Train the model
svc.fit(X_train, y_train)
# End the timer
end_time = time.time()
# Calculate the training time
training_time = end_time - start_time
print(f"Training Time: {training_time} seconds")
y_pred = svc.predict(X_test)
# Display the accuracy
print("Accuracy:", svc.score(X_test, y_test))
# Display the classification report
target_names = ['Diabetes', 'Normal']
print(classification_report(y_test, y_pred, target_names=target_names))
# # ROC Curve
# y_pred_proba = svc.predict_proba(X_test)[:, 1]
# fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
# plt.plot([0, 1], [0, 1], 'k-')
# plt.plot(fpr, tpr, label='SVM (Linear Kernel)')
# plt.xlabel('False Positive Rate')
# plt.ylabel('True Positive Rate')
# plt.title('ROC Curve')
# plt.legend()
# plt.show()
mat = confusion_matrix(y_test, y_pred, normalize = 'true')
plt.figure(figsize=(7, 5))
sns.heatmap(mat, annot=True, fmt=".2%", cmap="Blues")
plt.show()