1 # version 1.0 2 3 import numpy 4 import pandas as pd 5 import seaborn as sn 6 import matplotlib.pyplot as plt 7 import matplotlib.cm as cm 8 from matplotlib.colors import ListedColormap, BoundaryNorm 9 from sklearn import neighbors 10 import matplotlib.patches as mpatches 11 import graphviz 12 from sklearn.tree import export_graphviz 13 import matplotlib.patches as mpatches 14 15 def load_crime_dataset(): 16 # Communities and Crime dataset for regression 17 # https://archive.ics.uci.edu/ml/datasets/Communities+and+Crime+Unnormalized 18 19 crime = pd.read_table('CommViolPredUnnormalizedData.txt', sep=',', na_values='?') 20 # remove features with poor coverage or lower relevance, and keep ViolentCrimesPerPop target column 21 columns_to_keep = [5, 6] + list(range(11,26)) + list(range(32, 103)) + [145] 22 crime = crime.ix[:,columns_to_keep].dropna() 23 24 X_crime = crime.ix[:,range(0,88)] 25 y_crime = crime['ViolentCrimesPerPop'] 26 27 return (X_crime, y_crime) 28 29 def plot_decision_tree(clf, feature_names, class_names): 30 # This function requires the pydotplus module and assumes it's been installed. 31 # In some cases (typically under Windows) even after running conda install, there is a problem where the 32 # pydotplus module is not found when running from within the notebook environment. The following code 33 # may help to guarantee the module is installed in the current notebook environment directory. 34 # 35 # import sys; sys.executable 36 # !{sys.executable} -m pip install pydotplus 37 38 export_graphviz(clf, out_file="adspy_temp.dot", feature_names=feature_names, class_names=class_names, filled = True, impurity = False) 39 with open("adspy_temp.dot") as f: 40 dot_graph = f.read() 41 # Alternate method using pydotplus, if installed. 42 # graph = pydotplus.graphviz.graph_from_dot_data(dot_graph) 43 # return graph.create_png() 44 return graphviz.Source(dot_graph) 45 46 def plot_feature_importances(clf, feature_names): 47 c_features = len(feature_names) 48 plt.barh(range(c_features), clf.feature_importances_) 49 plt.xlabel("Feature importance") 50 plt.ylabel("Feature name") 51 plt.yticks(numpy.arange(c_features), feature_names) 52 53 def plot_labelled_scatter(X, y, class_labels): 54 num_labels = len(class_labels) 55 56 x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 57 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 58 59 marker_array = ['o', '^', '*'] 60 color_array = ['#FFFF00', '#00AAFF', '#000000', '#FF00AA'] 61 cmap_bold = ListedColormap(color_array) 62 bnorm = BoundaryNorm(numpy.arange(0, num_labels + 1, 1), ncolors=num_labels) 63 plt.figure() 64 65 plt.scatter(X[:, 0], X[:, 1], s=65, c=y, cmap=cmap_bold, norm = bnorm, alpha = 0.40, edgecolor='black', lw = 1) 66 67 plt.xlim(x_min, x_max) 68 plt.ylim(y_min, y_max) 69 70 h = [] 71 for c in range(0, num_labels): 72 h.append(mpatches.Patch(color=color_array[c], label=class_labels[c])) 73 plt.legend(handles=h) 74 75 plt.show() 76 77 78 def plot_class_regions_for_classifier_subplot(clf, X, y, X_test, y_test, title, subplot, target_names = None, plot_decision_regions = True): 79 80 numClasses = numpy.amax(y) + 1 81 color_list_light = ['#FFFFAA', '#EFEFEF', '#AAFFAA', '#AAAAFF'] 82 color_list_bold = ['#EEEE00', '#000000', '#00CC00', '#0000CC'] 83 cmap_light = ListedColormap(color_list_light[0:numClasses]) 84 cmap_bold = ListedColormap(color_list_bold[0:numClasses]) 85 86 h = 0.03 87 k = 0.5 88 x_plot_adjust = 0.1 89 y_plot_adjust = 0.1 90 plot_symbol_size = 50 91 92 x_min = X[:, 0].min() 93 x_max = X[:, 0].max() 94 y_min = X[:, 1].min() 95 y_max = X[:, 1].max() 96 x2, y2 = numpy.meshgrid(numpy.arange(x_min-k, x_max+k, h), numpy.arange(y_min-k, y_max+k, h)) 97 98 P = clf.predict(numpy.c_[x2.ravel(), y2.ravel()]) 99 P = P.reshape(x2.shape) 100 101 if plot_decision_regions: 102 subplot.contourf(x2, y2, P, cmap=cmap_light, alpha = 0.8) 103 104 subplot.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, s=plot_symbol_size, edgecolor = 'black') 105 subplot.set_xlim(x_min - x_plot_adjust, x_max + x_plot_adjust) 106 subplot.set_ylim(y_min - y_plot_adjust, y_max + y_plot_adjust) 107 108 if (X_test is not None): 109 subplot.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cmap_bold, s=plot_symbol_size, marker='^', edgecolor = 'black') 110 train_score = clf.score(X, y) 111 test_score = clf.score(X_test, y_test) 112 title = title + "\nTrain score = {:.2f}, Test score = {:.2f}".format(train_score, test_score) 113 114 subplot.set_title(title) 115 116 if (target_names is not None): 117 legend_handles = [] 118 for i in range(0, len(target_names)): 119 patch = mpatches.Patch(color=color_list_bold[i], label=target_names[i]) 120 legend_handles.append(patch) 121 subplot.legend(loc=0, handles=legend_handles) 122 123 124 def plot_class_regions_for_classifier(clf, X, y, X_test=None, y_test=None, title=None, target_names = None, plot_decision_regions = True): 125 126 numClasses = numpy.amax(y) + 1 127 color_list_light = ['#FFFFAA', '#EFEFEF', '#AAFFAA', '#AAAAFF'] 128 color_list_bold = ['#EEEE00', '#000000', '#00CC00', '#0000CC'] 129 cmap_light = ListedColormap(color_list_light[0:numClasses]) 130 cmap_bold = ListedColormap(color_list_bold[0:numClasses]) 131 132 h = 0.03 133 k = 0.5 134 x_plot_adjust = 0.1 135 y_plot_adjust = 0.1 136 plot_symbol_size = 50 137 138 x_min = X[:, 0].min() 139 x_max = X[:, 0].max() 140 y_min = X[:, 1].min() 141 y_max = X[:, 1].max() 142 x2, y2 = numpy.meshgrid(numpy.arange(x_min-k, x_max+k, h), numpy.arange(y_min-k, y_max+k, h)) 143 144 P = clf.predict(numpy.c_[x2.ravel(), y2.ravel()]) 145 P = P.reshape(x2.shape) 146 plt.figure() 147 if plot_decision_regions: 148 plt.contourf(x2, y2, P, cmap=cmap_light, alpha = 0.8) 149 150 plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, s=plot_symbol_size, edgecolor = 'black') 151 plt.xlim(x_min - x_plot_adjust, x_max + x_plot_adjust) 152 plt.ylim(y_min - y_plot_adjust, y_max + y_plot_adjust) 153 154 if (X_test is not None): 155 plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cmap_bold, s=plot_symbol_size, marker='^', edgecolor = 'black') 156 train_score = clf.score(X, y) 157 test_score = clf.score(X_test, y_test) 158 title = title + "\nTrain score = {:.2f}, Test score = {:.2f}".format(train_score, test_score) 159 160 if (target_names is not None): 161 legend_handles = [] 162 for i in range(0, len(target_names)): 163 patch = mpatches.Patch(color=color_list_bold[i], label=target_names[i]) 164 legend_handles.append(patch) 165 plt.legend(loc=0, handles=legend_handles) 166 167 if (title is not None): 168 plt.title(title) 169 plt.show() 170 171 def plot_fruit_knn(X, y, n_neighbors, weights): 172 X_mat = X[['height', 'width']].as_matrix() 173 y_mat = y.as_matrix() 174 175 # Create color maps 176 cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF','#AFAFAF']) 177 cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF','#AFAFAF']) 178 179 clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights) 180 clf.fit(X_mat, y_mat) 181 182 # Plot the decision boundary by assigning a color in the color map 183 # to each mesh point. 184 185 mesh_step_size = .01 # step size in the mesh 186 plot_symbol_size = 50 187 188 x_min, x_max = X_mat[:, 0].min() - 1, X_mat[:, 0].max() + 1 189 y_min, y_max = X_mat[:, 1].min() - 1, X_mat[:, 1].max() + 1 190 xx, yy = numpy.meshgrid(numpy.arange(x_min, x_max, mesh_step_size), 191 numpy.arange(y_min, y_max, mesh_step_size)) 192 Z = clf.predict(numpy.c_[xx.ravel(), yy.ravel()]) 193 194 # Put the result into a color plot 195 Z = Z.reshape(xx.shape) 196 plt.figure() 197 plt.pcolormesh(xx, yy, Z, cmap=cmap_light) 198 199 # Plot training points 200 plt.scatter(X_mat[:, 0], X_mat[:, 1], s=plot_symbol_size, c=y, cmap=cmap_bold, edgecolor = 'black') 201 plt.xlim(xx.min(), xx.max()) 202 plt.ylim(yy.min(), yy.max()) 203 204 patch0 = mpatches.Patch(color='#FF0000', label='apple') 205 patch1 = mpatches.Patch(color='#00FF00', label='mandarin') 206 patch2 = mpatches.Patch(color='#0000FF', label='orange') 207 patch3 = mpatches.Patch(color='#AFAFAF', label='lemon') 208 plt.legend(handles=[patch0, patch1, patch2, patch3]) 209 210 211 plt.xlabel('height (cm)') 212 plt.ylabel('width (cm)') 213 214 plt.show() 215 216 def plot_two_class_knn(X, y, n_neighbors, weights, X_test, y_test): 217 X_mat = X 218 y_mat = y 219 220 # Create color maps 221 cmap_light = ListedColormap(['#FFFFAA', '#AAFFAA', '#AAAAFF','#EFEFEF']) 222 cmap_bold = ListedColormap(['#FFFF00', '#00FF00', '#0000FF','#000000']) 223 224 clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights) 225 clf.fit(X_mat, y_mat) 226 227 # Plot the decision boundary by assigning a color in the color map 228 # to each mesh point. 229 230 mesh_step_size = .01 # step size in the mesh 231 plot_symbol_size = 50 232 233 x_min, x_max = X_mat[:, 0].min() - 1, X_mat[:, 0].max() + 1 234 y_min, y_max = X_mat[:, 1].min() - 1, X_mat[:, 1].max() + 1 235 xx, yy = numpy.meshgrid(numpy.arange(x_min, x_max, mesh_step_size), 236 numpy.arange(y_min, y_max, mesh_step_size)) 237 Z = clf.predict(numpy.c_[xx.ravel(), yy.ravel()]) 238 239 # Put the result into a color plot 240 Z = Z.reshape(xx.shape) 241 plt.figure() 242 plt.pcolormesh(xx, yy, Z, cmap=cmap_light) 243 244 # Plot training points 245 plt.scatter(X_mat[:, 0], X_mat[:, 1], s=plot_symbol_size, c=y, cmap=cmap_bold, edgecolor = 'black') 246 plt.xlim(xx.min(), xx.max()) 247 plt.ylim(yy.min(), yy.max()) 248 249 title = "Neighbors = {}".format(n_neighbors) 250 if (X_test is not None): 251 train_score = clf.score(X_mat, y_mat) 252 test_score = clf.score(X_test, y_test) 253 title = title + "\nTrain score = {:.2f}, Test score = {:.2f}".format(train_score, test_score) 254 255 patch0 = mpatches.Patch(color='#FFFF00', label='class 0') 256 patch1 = mpatches.Patch(color='#000000', label='class 1') 257 plt.legend(handles=[patch0, patch1]) 258 259 plt.xlabel('Feature 0') 260 plt.ylabel('Feature 1') 261 plt.title(title) 262 263 plt.show() 264