def plot_test_vs_validation_set(output_name, C, sigma, M, xlim=None, ylim=None, xticks=None, yticks=None): """ Plot a single figure which compares the expected validation and generalization errors for various numbers of training samples (n), using either m=1 or m=n validation samples. """ import matplotlib.pyplot as plt plt.ioff() plt.style.use('./latex-paper.mplstyle') plt.figure() ax = plt.axes() ax.yaxis.grid(True) d = pickler.load(f'categorical_K2_C{C}_sigma{sigma:.2f}_M{M}'.replace('.','_')) x_values = [x.n_train for x in d.xs] plt.plot(x_values, d.results[:,0], 'C0-', label=r'$\mathrm{e}_{\mathrm{val}}(m=n)$') plt.plot(x_values, d.results[:,1], 'C1-', label=r'$\mathrm{e}_{\mathrm{gen}}(m=n)$') d = pickler.load(f'categorical_LOO_C{C}_sigma{sigma:.2f}_M{M}'.replace('.','_')) x_values = [x.n_train for x in d.xs] plt.plot(x_values, d.results[:,0], 'C0--', label=r'$\mathrm{e}_{\mathrm{val}}(m=1)$') plt.plot(x_values, d.results[:,1], 'C1--', label=r'$\mathrm{e}_{\mathrm{gen}}(m=1)$') plt.xlabel('n') plt.ylabel('MSE') if xlim is not None: plt.xlim(xlim) if ylim is not None: plt.ylim(ylim) if xticks is not None: plt.xticks(xticks) if yticks is not None: plt.yticks(yticks) plt.legend(loc='best') simulations_framework.save_figure(output_name + f'_reps{d.n_repetitions}')
def plot_test_vs_validation_set(subdir, filename_prefix, M, normalize, xlim=None, ylim=None, xticks=None, yticks=None): """ Plot a single figure which compares the expected validation and generalization errors for various numbers of training samples (n), using either m=1 or m=n validation samples. """ import matplotlib.pyplot as plt plt.ioff() plt.style.use('./latex-paper.mplstyle') plt.figure() ax = plt.axes() ax.yaxis.grid(True) d = pickler.load(f'{filename_prefix}_K2_M{M}_normalize{normalize}') x_values = [x.n_train for x in d.xs] plt.plot(x_values, d.results[:, 0], 'C0-', linewidth=1.5, label=r'$\mathrm{e}_{\mathrm{val}}(m=n)$') plt.plot(x_values, d.results[:, 1], 'C1-', linewidth=1.5, label=r'$\mathrm{e}_{\mathrm{gen}}(m=n)$') d = pickler.load(f'{filename_prefix}_LOO_M{M}_normalize{normalize}') x_values = [x.n_train for x in d.xs] plt.plot(x_values, d.results[:, 0], 'C0--', linewidth=1.5, label=r'$\mathrm{e}_{\mathrm{val}}(m=1)$') plt.plot(x_values, d.results[:, 1], 'C1--', linewidth=1.5, label=r'$\mathrm{e}_{\mathrm{gen}}(m=1)$') plt.xlabel('$n$') plt.ylabel('MSE') plt.xlim(xlim if xlim is not None else [min(x_values), max(x_values)]) if ylim is not None: plt.ylim(ylim) if xticks is not None: plt.xticks(xticks) if yticks is not None: plt.yticks(yticks) #ax.set_yscale('log') plt.legend(loc='best') output_name = f'{filename_prefix}_M{M}_normalize{normalize}_reps{d.n_repetitions}' save_figure(output_name, subdir=subdir)
def __init__(self,name,func,need=[],needpos=[],params=None): self.name=name self.func=func self.need=need self.needpos=needpos self.params=params self.arr=pickler.load(self.name) self.arr2=pickler.load(self.name+"_2")
def plot_test_vs_validation_set(filename_prefix, D, df, K_strong_columns, strong_column_multiplier, K, noise_multiplier, xlim=None, ylim=None, xticks=None, yticks=None): """ Plot a single figure which compares the expected validation and generalization errors for various numbers of training samples (n), using either m=1 or m=n validation samples. """ import matplotlib.pyplot as plt plt.ioff() plt.style.use('./latex-paper.mplstyle') plt.figure() ax = plt.axes() ax.yaxis.grid(True) d = pickler.load(f'variable_selected_linear_regression_K2_D{D}_df{df}_Kstrong{K_strong_columns}_multiplier{strong_column_multiplier}_K{K}_noisemul{noise_multiplier:.2f}') x_values = [x.n_train for x in d.xs] plt.plot(x_values, d.results[:,0], 'C0-', linewidth=1.5, label=r'$\mathrm{e}_{\mathrm{val}}(m=n)$') plt.plot(x_values, d.results[:,1], 'C1-', linewidth=1.5, label=r'$\mathrm{e}_{\mathrm{gen}}(m=n)$') d = pickler.load(f'variable_selected_linear_regression_LOO_D{D}_df{df}_Kstrong{K_strong_columns}_multiplier{strong_column_multiplier}_K{K}_noisemul{noise_multiplier:.2f}') x_values = [x.n_train for x in d.xs] plt.plot(x_values, d.results[:,0], 'C0--', linewidth=1.5, label=r'$\mathrm{e}_{\mathrm{val}}(m=1)$') plt.plot(x_values, d.results[:,1], 'C1--', linewidth=1.5, label=r'$\mathrm{e}_{\mathrm{gen}}(m=1)$') d = pickler.load(f'variable_selected_linear_regression_null_model_D{D}_df{df}_Kstrong{K_strong_columns}_multiplier{strong_column_multiplier}_noisemul{noise_multiplier:.2f}') null_mse = d.results[0][0] plt.plot([min(x_values), max(x_values)], [null_mse, null_mse], 'k:', linewidth=1.0, label='Null model') plt.xlabel('$n$') plt.ylabel('MSE') plt.xlim(xlim if xlim is not None else [min(x_values), max(x_values)]) if ylim is not None: plt.ylim(ylim) if xticks is not None: plt.xticks(xticks) if yticks is not None: plt.yticks(yticks) plt.legend(loc='best') output_name = f'{filename_prefix}_D{D}_df{df}_Kstrong{K_strong_columns}_multiplier{strong_column_multiplier}_K{K}_noisemul{noise_multiplier:.2f}_reps{d.n_repetitions}' save_figure(output_name, f'noise_{noise_multiplier:.2f}')
def plot_alphas_correct_vs_incorrect(p, df, sigma, with_intercept, i, bins='auto'): plt.figure() results = pickler.load( f'normalized_lasso_cv_pipeline_p{p}_df{df}_sigma{sigma}_intercept{with_intercept}_10FOLDCV' ).results[i] assert results.ndim == 2 correct_alphas = results[:, 1] incorrect_alphas = results[:, 3] plt.hist([correct_alphas, incorrect_alphas], label=['correct', 'incorrect'], bins=bins) plt.legend()
def read_results_with_confintervals(p, df, sigma, with_intercept): d = pickler.load( f'normalized_lasso_cv_pipeline_p{p}_df{df}_sigma{sigma}_intercept{with_intercept}_10FOLDCV' ) assert d.results.shape == (len(d.xs), d.n_repetitions, 5) null_model_mse = np.mean(d.results[:, :, 4]) correct_mses = np.apply_along_axis(mean_confidence_interval, axis=1, arr=d.results[:, :, 0]) assert correct_mses.shape == (len(d.xs), 2) incorrect_mses = np.apply_along_axis(mean_confidence_interval, axis=1, arr=d.results[:, :, 2]) assert incorrect_mses.shape == (len(d.xs), 2) n_range = d.xs return (d.xs, null_model_mse, correct_mses, incorrect_mses, d.n_repetitions)
r = requests.get(graph_url) print(r.status_code) print(r.json()) if r.status_code == 200: result_json = json.loads(r.text) engagement_json = result_json.get('engagement') # {'engagement': {u'comment_count': 0, u'comment_plugin_count': 0, u'share_count': 0, u'reaction_count': 0} print(engagement_json) return engagement_json else: return None base_url = 'https://www.stugknuten.com' cottages = pickler.load('stugor_vastkusten.pickle', []) result = [] for cottage in cottages: cottage_href = cottage.get('href') engagement = get_engagement(cottage_href) if engagement: cottage['engagement'] = engagement print(cottage) result.append(cottage) time.sleep(FIVE_SECONDS) else: print('NO ENGAGEMENT! ERROR! ABORT ABORT!') break pickler.save('likes_vastkusten.pickle', result)
def __init__(self): self.messages = pickler.load("messages")
import pickler import pprint import json urls = pickler.load('likes_vastkusten.pickle', []) # {'engagement': {u'comment_count': 0, u'comment_plugin_count': 0, u'share_count': 0, u'reaction_count': 0} sorted_urls = sorted(urls, key=lambda x: x.get('engagement').get('reaction_count'), reverse=True) def pretty_print(): pp = pprint.PrettyPrinter(indent=4) for sorted_url in sorted_urls: pp.pprint(sorted_url) def to_json(): with open('likes_vastkusten.json', 'w') as outfile: print('dumping ....') json.dump(sorted_urls, outfile, indent=4, sort_keys=True) print('done') to_json()
def unpickle_model(model): path = settings.models_path print("loading model from: " + path + model.id + ".pickle") loaded_model = pickler.load(path + model.id + ".pickle") return loaded_model