Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn import datasets, tree
- from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
- # Load an iris dataset
- iris = datasets.load_iris()
- data = iris.data[:, [0, 1]] # Select only the 2 first columns (0, 1)
- # print("Dataset: ") # Length = 150
- # print(data)
- # print()
- target = iris.target # Like the class column
- # print("Target: ")
- # print(target)
- # print()
- target[100:125] = 0
- target[125:150] = 1
- # print("Target: ")
- # print(target)
- # print()
- # Split into training and testing data
- # Select 120/150 entries for training
- xtrain = np.concatenate((data[0:40], data[50:90], data[100:140]))
- ytrain = np.concatenate((target[0:40], target[50:90], target[100:140]))
- # Select 30/150 entries for testing
- xtest = np.concatenate((data[40:50], data[90:100], data[140:150]))
- ytest = np.concatenate((target[40:50], target[90:100], target[140:150]))
- print(str(len(xtrain)) + " training entries, " + str(len(xtest)) + " testing entries")
- # Classifier - Prediction
- clf = tree.DecisionTreeClassifier(min_samples_split=20)
- clf = clf.fit(xtrain, ytrain)
- pred = clf.predict(xtest)
- # Plot
- fig = plt.figure(figsize=(10, 9))
- tree.plot_tree(clf, class_names=['No', 'Yes'], filled=True)
- plt.show()
- # Metrics
- print("Confusion Matrix = ")
- print(confusion_matrix(ytest, pred))
- print("Accuracy = " + str(accuracy_score(ytest, pred)))
- print("Precision = " + str(precision_score(ytest, pred, pos_label=1)))
- print("Recall = " + str(recall_score(ytest, pred, pos_label=1)))
- print("F1 score = " + str(f1_score(ytest, pred, pos_label=1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement