-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreastCancerModel.py
More file actions
92 lines (62 loc) · 2.53 KB
/
BreastCancerModel.py
File metadata and controls
92 lines (62 loc) · 2.53 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
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
#PREPROCESSING ---------------------------------------------------
# Load Data
data = pd.read_csv('data.csv')
# Remove id
data = data.drop(columns = ['id'])
# Remove null rows
data = data.dropna()
# Seperate the target variable diagnosis and feature matrix
X = data.drop(columns=['diagnosis'])
y = data['diagnosis']
# Label encoder
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.10, random_state=42)
# Standardizing the training and test sets
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
X_train = pd.DataFrame(X_train_scaled, columns=X_train.columns)
X_test = pd.DataFrame(X_test_scaled, columns=X_test.columns)
#DIMENSION REDUCTION ------------------------------------------------
pca = PCA(n_components=2)
X_train_pca = pca.fit_transform(X_train)
X_test_pca = pca.transform(X_test)
#TRAIN & EVALUATE MODEL -----------------------------------------------------------
# Train logistic regression model
model = LogisticRegression()
model.fit(X_train_pca, y_train)
# Predict
predictions = model.predict(X_test_pca)
# Generate classification report
report = classification_report(y_test, predictions)
print(report)
# Plot real values vs predicted values
plt.figure()
plt.scatter(X_test_pca[y_test == 0, 0], X_test_pca[y_test == 0, 1], color='red', alpha=0.5, label='Benign')
plt.scatter(X_test_pca[y_test == 1, 0], X_test_pca[y_test == 1, 1], color='blue', alpha=0.5, label='Malignant')
plt.scatter(X_test_pca[predictions != y_test, 0], X_test_pca[predictions != y_test, 1], color='yellow', label='Misclassified', edgecolors='black')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.legend()
plt.title('PCA of Breast Cancer Test Set')
plt.show()
# Generate confusion matrix
cm = confusion_matrix(y_test, predictions)
# Display the confusion matrix
plt.figure(figsize=(10,7))
sns.heatmap(cm, annot=True, fmt='d')
plt.xlabel('Predicted')
plt.ylabel('True ')
plt.show()