def train_plot(features, labels): y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1 x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1 X = np.linspace(x0, x1, 100) Y = np.linspace(y0, y1, 100) X, Y = np.meshgrid(X, Y) model = learn_model(1, features[:, (0, 2)], np.array(labels)) C = apply_model(np.vstack([X.ravel(), Y.ravel()]).T, model).reshape(X.shape) if COLOUR_FIGURE: cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)]) else: cmap = ListedColormap([(1., 1., 1.), (.2, .2, .2), (.6, .6, .6)]) plt.xlim(x0, x1) plt.ylim(y0, y1) plt.xlabel(feature_names[0]) plt.ylabel(feature_names[2]) plt.pcolormesh(X, Y, C, cmap=cmap) if COLOUR_FIGURE: cmap = ListedColormap([(1., .0, .0), (.0, 1., .0), (.0, .0, 1.)]) plt.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap) else: for lab, ma in zip(range(3), "Do^"): plt.plot(features[labels == lab, 0], features[labels == lab, 2], ma, c=(1., 1., 1.))
def train_plot(features, labels): y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1 x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1 X = np.linspace(x0, x1, 100) Y = np.linspace(y0, y1, 100) X, Y = np.meshgrid(X, Y) model = learn_model(1, features[:, (0, 2)], np.array(labels)) C = apply_model( np.vstack([X.ravel(), Y.ravel()]).T, model).reshape(X.shape) if COLOUR_FIGURE: cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)]) else: cmap = ListedColormap([(1., 1., 1.), (.2, .2, .2), (.6, .6, .6)]) plt.xlim(x0, x1) plt.ylim(y0, y1) plt.xlabel(feature_names[0]) plt.ylabel(feature_names[2]) plt.pcolormesh(X, Y, C, cmap=cmap) if COLOUR_FIGURE: cmap = ListedColormap([(1., .0, .0), (.0, 1., .0), (.0, .0, 1.)]) plt.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap) else: for lab, ma in zip(range(3), "Do^"): plt.plot(features[labels == lab, 0], features[ labels == lab, 2], ma, c=(1., 1., 1.))
def train_plot(features, labels): y0, y1 = features[:, 2].min()*.9, features[:,2].max()*1.1 x0, x1 = features[:, 0].min()*.9, features[:,0].max()*1.1 X=np.linspace(x0, x1, 100) Y=np.linspace(y0, y1, 100) X, Y = np.meshgrid(X, Y) #meshgird is very useful to evaluate functions on a grid model = learn_model(1, features[:, (0,2)], np.array(labels)) C = apply_model( np.vstack([X.ravel(), Y.ravel()]).T, model).reshape(X.shape) if COLOUR_FIGURE: cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)]) else: cmap = ListedColormap([(1., 1, 1), (.2, .2, .2), (.6, .6, .6)]) plt.xlim(x0,x1) plt.ylim(y0,y1) plt.xlabel(feature_names[0]) plt.ylabel(feature_names[2]) plt.pcolormesh(X,Y,C, cmap=cmap) if COLOUR_FIGURE: #cmap = ListedColormap([(1., .0, .0), (.0, 1, .0), (.0, .0, 1.)]) #plt.scatter(features[:,0], features[:,2], c=labels, cmap=cmap) # this is a better way to plot instead of using the for . col=[(1., .0, .0,), (.0, 1, .0), (.0, .0, 1.)] for lab, c in zip(range(3), col): plt.scatter(features[labels==lab,0], features[labels==lab, 2], c=c) else: for lab, ma in zip(range(3), "Do^"): plt.plot(features[labels == lab, 0], features[ labels == lab, 2], ma, c=(1.,1.,1.))
def cross_validate(features, labels): error = 0.0 for fold in range(10): training = np.ones(len(features), bool) training[fold::10] = 0 testing = ~training model = learn_model(1, features[training], labels[training]) test_error = accuracy(features[testing], labels[testing], model) error += test_error return error / 10.0
def cross_validation(features, labels): error = 0.0 for fold in range(10): training = np.ones(len(features), bool) training[fold::10] = 0 testing = ~training model = learn_model(1, features[training], labels[training]) test_error = accuracy(features[testing], labels[testing], model) error += test_error return error / 10.0