12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #!/usr/bin/env python3
- import numpy as np
- c = 0.1
- LIMIT = 100
- print_epoches = [5, 10, 100, 1000]
- DATA_SLICE = 5
- NUM_CLASS = 1
- INPUT_FEATURE = 22
- def get_one_hot(targets, nb_classes):
- res = np.eye(nb_classes)[np.array(targets).reshape(-1)]
- return res.reshape(list(targets.shape)+[nb_classes])
- def load(file):
- raw_data = np.loadtxt(file, delimiter=',')
- data_size = len(raw_data)
- np.random.shuffle(raw_data)
- ret = np.split(raw_data, [1,], axis=1)
- return ret
- train_pieces = load('SPECT.train')
- train_label = train_pieces[0]
- train_data = train_pieces[1]
- train_label = train_label.flatten().astype(int)
- val_pieces = load('SPECT.test')
- val_label = val_pieces[0]
- val_data = val_pieces[1]
- val_label = val_label.flatten().astype(int)
- v = np.array(val_data, dtype='float32')
- v = np.concatenate((v, np.ones((len(v), 1))), axis=1)
- def pla(train_data, train_label, val_data, val_label, picked_class):
- x = np.array(train_data, dtype='float32')
- x = np.concatenate((x, np.ones((len(x), 1))), axis=1)
- for i in range(len(x)):
- if train_label[i] != picked_class:
- x[i] *= -1
- w = np.random.rand(INPUT_FEATURE + 1)
- for j in range(LIMIT):
- flag = False
- for i in x:
- z = sum(i * w)
- if z <= 0:
- w = w + c * i
- flag = True
- if not flag:
- print('')
- break
- #if j in print_epoches:
- print(j)
- print('train acc:', np.sum(np.sum(x * w, axis=1) > 0) / len(x))
- print('val acc:', np.sum((np.sum(v * w, axis=1) > 0) == (val_label == picked_class)) / len(v))
- return w
- w = [pla(train_data, train_label, val_data, val_label, i) for i in range(NUM_CLASS)]
- pred = [np.sum(v * w[i], axis=1) > 0 for i in range(NUM_CLASS)]
- TP = sum(pred[0][i] == 1 and val_label[i] == 1 for i in range(len(val_label)))
- FP = sum(pred[0][i] == 1 and val_label[i] == 0 for i in range(len(val_label)))
- FN = sum(pred[0][i] == 0 and val_label[i] == 1 for i in range(len(val_label)))
- P = TP / (TP + FP)
- R = TP / (TP + FN)
- print('val acc:', sum([all([pred[j][i] == (j == val_label[i]) for j in range(NUM_CLASS)]) for i in range(len(val_label))]) / len(val_label))
- print('F1', 2 * P * R / (P + R))
|