# Split the dataset into test and train sets from sklearn.model_selection import train_test_split as skl_tts ind_train_set, ind_test_set, dep_train_set, dep_test_set = skl_tts( ind_var, dep_var, test_size=0.25) # Fit the model from sklearn.naive_bayes import GaussianNB as skl_gn classifier = skl_gn() classifier.fit(ind_train_set, dep_train_set) # Predict the test set prediction = classifier.predict(ind_test_set) # Create confusion matrix to verify the results from sklearn.metrics import confusion_matrix as skl_cm conf_matrix = skl_cm(y_true=dep_test_set, y_pred=prediction) conf_matrix # ============================================= # Disclaimer: All the plotting code below is not mine. # Font: Machine Learning A-Z™: Hands-On Python & R In Data Science - kernel_svm.py source code # ============================================= from matplotlib.colors import ListedColormap import matplotlib.pyplot as plt import numpy as np X_set, y_set = ind_train_set, dep_train_set X1, X2 = np.meshgrid( np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01), np.arange(start=X_set[:, 1].min() - 1,
lin_classifier.fit(ind_train_set, dep_train_set) rbf_classifier.fit(ind_train_set, dep_train_set) sig_classifier.fit(ind_train_set, dep_train_set) pol_classifier.fit(ind_train_set, dep_train_set) # Predict the test set lin_prediction = lin_classifier.predict(ind_test_set) rbf_prediction = rbf_classifier.predict(ind_test_set) sig_prediction = sig_classifier.predict(ind_test_set) pol_prediction = pol_classifier.predict(ind_test_set) # Create confusion matrix to verify the results from sklearn.metrics import confusion_matrix as skl_cm print 'Linear Kernel confusion matrix' skl_cm(y_true = dep_test_set, y_pred = lin_prediction) print 'RBF (Gaussian) Kernel confusion matrix' skl_cm(y_true = dep_test_set, y_pred = rbf_prediction) print 'Sigmoid Kernel confusion matrix' skl_cm(y_true = dep_test_set, y_pred = sig_prediction) print 'Polynomial (3degree) Kernel confusion matrix' skl_cm(y_true = dep_test_set, y_pred = pol_prediction) # ============================================= # Disclaimer: All the plotting code below is not mine. # Font: Machine Learning A-Z™: Hands-On Python & R In Data Science - kernel_svm.py source code # ============================================= from matplotlib.colors import ListedColormap import matplotlib.pyplot as plt import numpy as np
ind_var, dep_var, test_size=0.25) # Fit the model from sklearn.tree import DecisionTreeClassifier as skl_dtc classifier = skl_dtc( criterion='entropy', # Could be gini splitter='best', max_depth=5) classifier.fit(ind_train_set, dep_train_set) # Predict the test set prediction = classifier.predict(ind_test_set) # Create confusion matrix to verify the results from sklearn.metrics import confusion_matrix as skl_cm skl_cm(y_true=dep_test_set, y_pred=prediction) # ============================================= # Disclaimer: All the plotting code below is not mine. # Font: Machine Learning A-Z™: Hands-On Python & R In Data Science - kernel_svm.py source code # ============================================= from matplotlib.colors import ListedColormap import matplotlib.pyplot as plt import numpy as np X_set, y_set = ind_train_set, dep_train_set X1, X2 = np.meshgrid( np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01), np.arange(start=X_set[:, 1].min() - 1,
def confusion_matrix(output, target): with torch.no_grad(): pred = torch.argmax(output, dim=1) result = skl_cm(target.detach().cpu().numpy(), pred.detach().cpu().numpy()) return result