W_test = pca.transform(X_test) params = {'C': 1, 'gamma': 2e-4, 'kernel': 'linear'} ovr = OVR(**params) ovr.fit(W_train, y_train) y_hat = ovr.predict(W_test[::-1]).ravel() done = {'success': False, 'failure': False} fig, axes = plt.subplots(ncols=2) for y, t, w in zip(y_hat, y_test.T.ravel(), W_test.T): if y == t and done['success'] is False: x_hat = pca.reconstruct(w) axes[0].imshow(x_hat.reshape(SHAPE).T, cmap=plt.get_cmap('gray')) axes[0].set_title( 'Successful SVM Classification\nPredicted Class: %d, True Class: %d' % (y, t), color=_palette[1]) done['success'] = True elif y != t and done['failure'] is False: x_hat = pca.reconstruct(w) axes[1].imshow(x_hat.reshape(SHAPE).T, cmap=plt.get_cmap('gray')) axes[1].set_title( 'Failed SVM Classification\nPredicted Class: %d, True Class: %d' % (y, t), color=_palette[2]) done['failure'] = True elif done['failure'] is True and done['success'] is True:
ovr = OVR(**params) ovr.fit(W_train, y_train) n_idx = 4 np.random.seed(13) fig, axes = plt.subplots(nrows=2, ncols=n_idx) idx = np.array(list(ovr.classifiers.keys()))[np.random.choice( len(ovr.classifiers.keys()), n_idx)] for l, i in zip(idx, range(n_idx)): w_hat = ovr.classifiers[l].support_vectors_[0] x_hat = pca.reconstruct(w_hat) axes[0, i].imshow(x_hat.reshape(SHAPE).T, cmap=plt.get_cmap('gray')) axes[0, i].set_title('OVR\nSupport Vector for label %d\n' % l) ovo = OVO(**params) ovo.fit(W_train, y_train) idx = np.array(list(ovo.classifiers.keys()))[np.random.choice( len(ovo.classifiers.keys()), n_idx)] for l, i in zip(idx, range(n_idx)): w_hat = ovo.classifiers[tuple(l)].support_vectors_[0] x_hat = pca.reconstruct(w_hat)