def svm_skin(): print("\nSVM Classification for Skin Cancer data:\n") x_train, x_test, y_train, y_test = get_data_skin() svm = SVM(x_train, np.where(y_train == 0, -1, y_train), 100, 0.01) y_pred = svm.predict(x_train) print("\nTraining Classification accuracy: ") print(100 - 100 * np.sum(np.abs(y_pred - y_train)) / y_pred.shape[0]) confusionMatrix(y_train, y_pred) y_pred = svm.predict(x_test) print("\nTesting Classification accuracy: ") print(100 - 100 * np.sum(np.abs(y_pred - y_test)) / y_pred.shape[0]) confusionMatrix(y_test, y_pred) print("ROC Curve: ") plot_roc_curve(y_test, y_pred)
def logistic_skin(): print("\nLogistic Regression for Skin Cancer data:\n") x_train, x_test, y_train, y_test = get_data_skin() logistic = Logistic(x_train, y_train) y_pred = logistic.predict(x_train) print("\nTraining Classification accuracy: ") print(100 - 100 * np.sum(np.abs(y_pred - y_train)) / y_pred.shape[0]) confusionMatrix(y_train, y_pred) y_pred = logistic.predict(x_test) print("\nTesting Classification accuracy: ") print(100 - 100 * np.sum(np.abs(y_pred - y_test)) / y_pred.shape[0]) confusionMatrix(y_test, y_pred) print("ROC Curve: ") plot_roc_curve(y_test, y_pred)
def predict(self, X_test, y_test): X_input = autograd.Variable(X_test) y_target = autograd.Variable(y_test.long()) with torch.no_grad(): y_output = self(X_input) prediction = y_output.max(1)[1] FP = (prediction - y_target).nonzero().shape[0] print( "\nFeed-Forward NN Test Classification Accuracy: {:.2f}%\n".format( (1 - FP / (y_target.shape[0])) * 100)) confusionMatrix(y_target, prediction) print("ROC Curve: ") plot_roc_curve(y_target, prediction)
def cnn(): x_train, x_test, y_train, y_test = get_data_skin(model='cnn') x_train = x_train / np.float32(255) y_train = y_train.astype(np.int32) x_test = x_test / np.float32(255) y_test = y_test.astype(np.int32) # Create the Estimator skin_cancer_classifier = tf.estimator.Estimator( model_fn=cnn_model_fn, model_dir="skin_convnet_model") # Set up logging for predictions tensors_to_log = {"probabilities": "softmax_tensor"} logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50) # Train the model train_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": x_train}, y=y_train, batch_size=100, num_epochs=None, shuffle=True) # train one step and display the probabilties skin_cancer_classifier.train(input_fn=train_input_fn, steps=1, hooks=[logging_hook]) skin_cancer_classifier.train(input_fn=train_input_fn, steps=100) eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": x_test}, y=y_test, num_epochs=1, shuffle=False) predictions = list(skin_cancer_classifier.predict(input_fn=eval_input_fn)) predict = [i['classes'] for i in predictions] confusionMatrix(y_test, np.asarray(predict)) print("ROC Curve: ") plot_roc_curve(y_test, np.asarray(predict)) eval_results = skin_cancer_classifier.evaluate(input_fn=eval_input_fn) print(eval_results)
def train(self, X_train, y_train): X_input = autograd.Variable(X_train) y_target = autograd.Variable(y_train.long()) opt = optim.SGD(params=self.parameters(), lr=0.01) for epoch in range(max_epochs): self.zero_grad() output = self(X_input) predict = output.max(1)[1] loss = F.cross_entropy(output, y_target) loss.backward() opt.step() FP = (predict - y_target).nonzero().shape[0] print("\nFeed-Forward NN Training Classification Accuracy: {:.2f}%". format((1 - FP / y_target.shape[0]) * 100)) confusionMatrix(y_target, predict) print("ROC Curve: ") plot_roc_curve(y_target, predict)