-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwdbc_classification_skeleton.py
More file actions
47 lines (41 loc) · 2.42 KB
/
wdbc_classification_skeleton.py
File metadata and controls
47 lines (41 loc) · 2.42 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
import numpy as np
import matplotlib.pyplot as plt
from sklearn import (datasets, svm, metrics)
from matplotlib.lines import Line2D # For the custom legend
def load_wdbc_data(filename):
class WDBCData:
data = [] # Shape: (569, 30)
target = [] # Shape: (569, )
target_names = ['malignant', 'benign']
feature_names = ['mean radius', 'mean texture', 'mean perimeter', 'mean area', 'mean smoothness', 'mean compactness', 'mean concavity', 'mean concave points', 'mean symmetry', 'mean fractal dimension',
'radius error', 'texture error', 'perimeter error', 'area error', 'smoothness error', 'compactness error', 'concavity error', 'concave points error', 'symmetry error', 'fractal dimension error',
'worst radius', 'worst texture', 'worst perimeter', 'worst area', 'worst smoothness', 'worst compactness', 'worst concavity', 'worst concave points', 'worst symmetry', 'worst fractal dimension']
wdbc = WDBCData()
with open(filename) as f:
for line in f.readlines():
items = line.split(',')
wdbc.target.append(items[1]) # TODO #1) Add the true label (0 for M / 1 for others)
wdbc.data.append(items[2:]) # TODO #1) Add 30 attributes (as floating-point numbers)
wdbc.data = np.array(wdbc.data)
return wdbc
if __name__ == '__main__':
# Load a dataset
wdbc = load_wdbc_data('data/wdbc.data') # TODO #1) Implement 'load_wdbc_data()'
# Train a model
model = svm.SVC() # TODO #2) Find a better classifier (SVC accuracy: 0.902)
model.fit(wdbc.data, wdbc.target)
# Test the model
predict = model.predict(wdbc.data)
accuracy = metrics.balanced_accuracy_score(wdbc.target, predict)
# TODO #3) Visualize the confusion matrix
# Visualize testing results
cmap = np.array([(1, 0, 0), (0, 1, 0)])
clabel = [Line2D([0], [0], marker='o', lw=0, label=wdbc.target_names[i], color=cmap[i]) for i in range(len(cmap))]
for (x, y) in [(0, 1)]: # Not mandatory, but try [(i, i+1) for i in range(0, 30, 2)]
plt.figure()
plt.title(f'My Classifier (Accuracy: {accuracy:.3f})')
plt.scatter(wdbc.data[:,x], wdbc.data[:,y], c=cmap[wdbc.target], edgecolors=cmap[predict])
plt.xlabel(wdbc.feature_names[x])
plt.ylabel(wdbc.feature_names[y])
plt.legend(handles=clabel, framealpha=0.5)
plt.show()