x_m, y_sd, X_train = ef.normalize(X_train) # Normalizes the data, extracting mean and SD of training data for normalizing the testing data later. clf = SVC(kernel = 'linear', C = 0.14, random_state = seed, probability = True) clf.fit(X_train, y_train) # Finding the Training Accuracy: correct = 0 total = 0 # ================================== # TESTING PART: # ------------ # Extracting Testing Data: (X_test, y_test) = ef.fetch_test() x_m, y_sd, X_test = ef.normalize(X_test, xm = x_m, ysd = y_sd) # Normalizes the testing data with the mean and SD of the training set. # Fitting the classifier: print '\nUsing %s estimators of depth %s.\n' % (str(num_est), str(tree_depth)) # Deriving the ROC curve: y_check = clf.predict(X_test) y_hat = clf.predict_proba(X_test) y_hat = array([ entry[1] for entry in y_hat ]) fpr, tpr, thresholds = roc_curve(y_test, y_hat, pos_label=1) # Plotting the result: plt.plot(fpr, tpr) plt.plot([0,1], [0,1], 'k--') plt.xlabel('False Positive Rate')
print('Epoch:', epoch, 'Loss:', loss) Losses.append(loss.item()) # Zero gradients, perform a backward pass, and update the weights. optimizer.zero_grad() # perform a backward pass (backpropagation) for i in range(6): list(model.parameters())[i].retain_grad() loss.backward() # Update the parameters optimizer.step() # Testing the model x_test, y_test = ef.fetch_test() x_ts = Variable(torch.Tensor(x_test).type(torch.FloatTensor)) y_ts = torch.Tensor([i for i in y_test]).type(torch.LongTensor) y_pred = model(x_ts) labels = y_ts.detach().numpy() decs_1 = y_pred[:, 1].detach().numpy() print('Labels:', labels.shape) print('Decisions:', decs_1.shape) fpr, tpr, thr = roc_curve(labels, decs_1) # Plotting the result: plt.plot(fpr, tpr) plt.plot([0, 1], [0, 1], 'k--') plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate')