def plot_multiple(plot_function, args, filename=None, figsize=(14,9), label_left = "Density", label_bottom="Coarse Grain Measure Value", label_right="Energy", legend_pos=(1.40,0.90), legend_labels=None): fig = plt.figure(figsize=figsize) #figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k') num_structs = len(args) rows = int(1.5 * math.sqrt(num_structs)) cols = int(math.ceil(num_structs / float(rows))) ls,lb = None,None #figsize(cols * 4, rows * 3) for i, arg in enumerate(args): ax = fig.add_subplot(rows, cols, i+1) try: (ls, lb) = plot_function(arg, ax=ax) except Exception: import traceback print >>sys.stderr, traceback.format_exc() #print >>sys.stderr, "Error:", str(e) continue fig.add_subplot(rows, cols, 1) ax = fig.add_subplot(rows, cols, num_structs) #ls1, lb1 = ax1.get_legend_handles_labels() if label_bottom is not None: fig.text(0.5, 0.00, label_bottom, ha='center', va='center', fontsize=13) if label_left is not None: fig.text(0.00, 0.60, label_left, ha='center', va='center', rotation='vertical', fontsize=13) if label_right is not None: fig.text(1., 0.60, label_right, ha='center', va='center', rotation='vertical', fontsize=13) plt.tight_layout() plt.subplots_adjust(top=1.30) if legend_labels is not None: lb = legend_labels if ls is not None and lb is not None: plt.legend(ls, lb, bbox_to_anchor=legend_pos, loc=2, borderaxespad=0.) if filename is not None: # blah plt.savefig(filename, bbox_inches='tight') return ax
def predict_price(dates,prices,x): dates = np.reshape(dates,len(dates),1) svr_len = SVR(kernel='linear',c=1e3) svr_poly = SVR(kernel='poly',c=1e3,degree=2) svr_len = SVR(kernel='rbf',c=1e3,gamma=0.1) svr_lin.fit(dates,prices) svr_poly.fit(dates,prices) svr_rbf.fit(dates,prices) plt.scatter(dates,prices,color='black', label='Data') plt.plot(dates, svr_rbf.predict(dates), color='red', label='RBF model') plt.plot(dates, svr_lin.predict(dates), color='green', label='Linear model') plt.plot(dates, svr_ply.predict(dates), color='blue', label='Ploynomial model') plt.xlabel('Date') plt.ylabel('Price') plt.title('Support Vector Regration') plt.legend() plt.show() return svr_rbf.predict(x)[0],svr_lin.predict(x)[0], ,svr_poly.predict(x)[0]
def plot_the_loss_curve(epochs, mae_training, mae_validation): """Plot a curve of loss vs. epoch.""" plt.figure() plt.xlabel("Epoch") plt.ylabel("Root Mean Squared Error") plt.plot(epochs[1:], mae_training[1:], label="Training Loss") plt.plot(epochs[1:], mae_validation[1:], label="Validation Loss") plt.legend() # We're not going to plot the first epoch, since the loss on the first epoch # is often substantially greater than the loss for other epochs. merged_mae_lists = mae_training[1:] + mae_validation[1:] highest_loss = max(merged_mae_lists) lowest_loss = min(merged_mae_lists) delta = highest_loss - lowest_loss print(delta) top_of_y_axis = highest_loss + (delta * 0.05) bottom_of_y_axis = lowest_loss - (delta * 0.05) plt.ylim([bottom_of_y_axis, top_of_y_axis]) plt.show()
target_size=(150, 150), batch_size=20, class_mode='binary') validation_generator = test_datagen.flow_from_directory(validation_dir, target_size=(150, 150), batch_size=20, class_mode='binary') model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=2e-5), metrics=['acc']) history = model.fit_generator(train_generator, steps_per_epoch=100, epochs=30, validation_data=validation_generator, validation_steps=50) acc = history.history['acc'] val_acc = history.history['val_acc'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(acc) + 1) plt.plot(epochs, acc, 'bo', label='Training acc') plt.plot(epochs, val_acc, 'b', label='Validation acc') plt.title('Training and validation accuracy') plt.legend() plt.figure() plt.plot(epochs, loss, 'bo', label='Training loss') plt.plot(epochs, val_loss, 'b', label='Validation loss') plt.title('Training and validation loss') plt.legend() plt.show()
import matplot.pyplot as plt from scipy import special import numpy as np ai, aip, bi, bip = special.airy(x) x = linspace(-15, 5, 201) plt.plot(x, ai, 'r', label='Ai(x)') plt.plot(x, bi, 'b--', label='Bi(x)') plt.ylim(-0.5, 1.0) plt.grid plt.legend(loc='upper left') plt.show()
# decision function calculates the raw anomaly score for every point Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) * -1 Z = Z.reshape(xx.shape) # fill blue map colormap from minimum anomaly score to threshold value plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), threshold, 7),cmap=plt.cm.Blues_r) # draw red contour line where anomaly score is equal to thresold a = plt.contour(xx, yy, Z, levels=[threshold],linewidths=2, colors='red') # fill orange contour lines where range of anomaly score is from threshold to maximum anomaly score plt.contourf(xx, yy, Z, levels=[threshold, Z.max()],colors='orange') b = plt.scatter(IX1,IX2, c='white',s=20, edgecolor='k') c = plt.scatter(OX1,OX2, c='black',s=20, edgecolor='k') plt.axis('tight') # loc=2 is used for the top left corner plt.legend( [a.collections[0], b,c], ['learned decision function', 'inliers','outliers'], prop=matplotlib.font_manager.FontProperties(size=20), loc=2) plt.xlim((0, 1)) plt.ylim((0, 1)) plt.title(clf_name) plt.show()