def show_history(history): sns.set_style("darkgrid") sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 1.6}) sns.mpl.rc("figure", figsize=(16, 5)) f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot(history.losses, color="#47d024") ax1.plot(history.accuracy, color="#0672b6") ax1.legend(["loss", "accuracy"]) ax1.set_title("Batch validation") ax2.plot(history.val_losses, color="#47d024") ax2.plot(history.val_accuracy, color="#0672b6") ax2.legend(["loss", "accuracy"]) ax2.set_title("Epoch validation") plt.show()
@copyright CL all rights reserved @created Thu Dec 23 2018 15:10 GMT-0800 (PST) @last-modified Wed Feb 27 2019 19:20 GMT-0800 (PST) .:.:.::.:.:.:.:.::.:.:.:.:.::.:.:.:.:.::.:.:.:.:.::.:.:''' from tabulate import tabulate import matplotlib import os import datetime from matplotlib import pyplot as plt from mpl_finance import candlestick_ohlc import matplotlib.dates as mdates import numpy as np import seaborn.apionly as sns sns.set_style("white") sns.set_context("poster") import pandas as pd import mpld3 class VisualChart: ''' plot visualization ''' def price_volumn(self, kline_data, save_figure): ''' price_volumn: plot price and volumn Param: kline_data (obj, pandas_dataframe) ''' fig_file = os.path.join(
import matplotlib matplotlib.use('Agg') from matplotlib import pyplot as plt from matplotlib.ticker import Formatter from matplotlib import patheffects as PathEffects from scipy.spatial import Delaunay #import seaborn as sns import seaborn.apionly as sns sns.set_style('ticks') sns.set_context('talk') #sns.set_style({'font.family':'Times New Roman'}) rc_params = { 'backend': 'ps', 'axes.labelsize': 15, 'axes.titlesize': 15, 'font.size': 13, 'legend.fontsize': 15, 'xtick.labelsize': 12, 'ytick.labelsize': 12, #'text.usetex': True, 'font.family': 'Times New Roman' } #, #'font.sans-serif': ['Bitstream Vera Sans']}#,
# wherever the files are located # - there should be a pics subdirectory for picture generation homepath = '/home/gswarrin/research/gerrymander/' ################################################## # imports import pystan import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib import seaborn.apionly as sns sns.set_context('notebook') from matplotlib.colors import LinearSegmentedColormap import scipy.stats as stats # Index of state in this list corresponds to the convention many of our data files use. # '00' is added at he beginning to make this correspondence correct. stlist = ['00','AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','KY',\ 'LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH',\ 'OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'] GLOBAL_MIN_YEAR = 1971 ################################################## load(homepath + 'util.py') load(homepath + 'metrics.py') # functions for computing metrics (e.g., declination, EG) load(homepath + 'read_data.py') # functions for reading in all data load(homepath + 'model.py') # statistical model for imputing values load(homepath + 'classes.py') # various classes for storing information
import pandas as pd import ConfigParser import lab import lab.classes.exceptions as exc import lab.plotting as plotting import warnings warnings.filterwarnings("ignore", category=DeprecationWarning) import seaborn.apionly as sns sns.set_style("ticks") sns.set_context( rc={ 'lines.linewidth': 1, 'axes.titlesize': 7, 'axes.labelsize': 'medium', 'xtick.labelsize': 'medium', 'ytick.labelsize': 'medium' }) import matplotlib as mpl # mpl.rcParams['font.sans-serif'].append(u'DejaVu Sans') mpl.rcParams.update({ 'lines.linewidth': 1, 'axes.titlesize': 9, 'axes.labelsize': 9, 'xtick.labelsize': 7, 'ytick.labelsize': 7, 'mathtext.fontset': 'dejavuserif', 'font.size': 9, 'pdf.fonttype': 42,
def plot_pca_pairs(clf_pca, x_train, y=None, n_components=3, diag='kde', cmap=None, figsize=(10, 10)): """ Create pairwise plots of principal components from x data. Colors the components according to the `y` values. Parameters ---------- clf_pca : sklearn.decomposition.PCA A fitted scikit-learn PCA model. x_train : numpy.ndarray Training data used to fit `clf_pca`, either scaled or un-scaled, depending on how `clf_pca` was fit. y : numpy.ndarray Target training values, of shape = [n_samples]. n_components: int Desired number of principal components to plot. Defaults to 3. diag : str Type of plot to display on the diagonals. Default is 'kde'. * 'kde': density curves * 'hist': histograms cmap : str A string representation of a Seaborn color map. See available maps: https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes. figsize : tuple A tuple indicating the size of the plot to be created, with format (x-axis, y-axis). Defaults to (10, 10). Returns ------- matplotlib.figure.Figure The Figure instance. """ if y is not None: assert y.shape[0] == x_train.shape[0], ( "Dimensions of y {0} do not match dimensions of x_train {1}". format(y.shape[0], x_train.shape[0])) # Obtain the projections of x_train x_projection = clf_pca.transform(x_train) # Create a data frame to hold the projections of n_components PCs col_names = ["PC{0}".format(i + 1) for i in range(n_components)] df = pd.DataFrame(x_projection[:, 0:n_components], columns=col_names) # Generate the plot cmap = "Greys" if cmap is None else cmap color = "#55A969" if y is None else y sns.set_style("white", {"axes.linewidth": "0.8", "image.cmap": cmap}) sns.set_context("notebook") try: # Create figure instance with subplot and populate the subplot with # the scatter matrix. You need to do this so you can access the figure # properties later to increase distance between subplots. If you don't, # Pandas will create its own figure with a tight layout. fig = plt.figure(figsize=figsize) ax = fig.add_subplot(1, 1, 1) from pandas.tools.plotting import scatter_matrix axes = scatter_matrix(df, ax=ax, alpha=0.7, figsize=figsize, diagonal=diag, marker='o', c=color, density_kwds={'c': '#6283B9'}, hist_kwds={ 'facecolor': '#5A76A4', 'edgecolor': '#3D3D3D' }) # Increase space between subplots fig.subplots_adjust(hspace=0.1, wspace=0.1) # Loop through subplots and remove top and right axes axes_unwound = np.ravel(axes) for i in range(axes_unwound.shape[0]): ax = axes_unwound[i] ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) plt.show() except: raise # Re-raise the exception else: sns.reset_orig() return fig finally: sns.reset_orig()
def plot_residuals(clf, X, y, r_type='standardized', figsize=(10, 8)): """Plot residuals of a linear model. Parameters ---------- clf : sklearn.linear_model A scikit-learn linear model classifier with a `predict()` method. X : numpy.ndarray Training data used to fit the classifier. y : numpy.ndarray Target training values, of shape = [n_samples]. r_type : str Type of residuals to return: 'raw', 'standardized', 'studentized'. Defaults to 'standardized'. * 'raw' will return the raw residuals. * 'standardized' will return the standardized residuals, also known as internally studentized residuals, which is calculated as the residuals divided by the square root of MSE (or the STD of the residuals). * 'studentized' will return the externally studentized residuals, which is calculated as the raw residuals divided by sqrt(LOO-MSE * (1 - leverage_score)). figsize : tuple A tuple indicating the size of the plot to be created, with format (x-axis, y-axis). Defaults to (10, 8). Returns ------- matplotlib.figure.Figure The Figure instance. """ # Ensure we only plot residuals using classifiers we have tested assert isinstance(clf, _utils.supported_linear_models), ( "Classifiers of type {0} not currently supported.".format(type(clf))) # Get residuals or standardized residuals resids = stats.residuals(clf, X, y, r_type) predictions = clf.predict(X) # Prepare plot labels to use, depending on which type of residuals used y_label = { 'raw': 'Residuals', 'standardized': 'Standardized Residuals', 'studentized': 'Studentized Residuals' } # Set plot style sns.set_style("whitegrid") sns.set_context("talk") # Increase font size on plot # Generate residual plot try: fig = plt.figure('residuals', figsize=figsize) plt.scatter(predictions, resids, s=14, c='gray', alpha=0.7) plt.hlines(y=0, xmin=predictions.min(), xmax=predictions.max(), linestyle='dotted') plt.title("Residuals Plot") plt.xlabel("Predictions") plt.ylabel(y_label[r_type]) plt.show() except: raise # Re-raise the exception finally: sns.reset_orig() # Always reset back to default matplotlib styles return fig