Last active
October 18, 2019 20:22
-
-
Save dangpzanco/2906aa9417450f2ad12448aaea97c910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib # Handle paths (https://pbpython.com/pathlib-intro.html) | |
import numpy as np | |
def run_simulation(n=1000): | |
x = np.random.randn(n) | |
y = np.random.randn(n,n) @ x | |
simulation_results = dict(x=x, y=y) | |
return simulation_results | |
# Define output folder | |
output_folder = pathlib.Path('output') | |
# Create folder if not exists (create parents too) | |
output_folder.mkdir(parents=True, exist_ok=True) | |
# Define output path (simulation filename) | |
output_path = output_folder / 'simulation.npz' | |
completed = output_path.exists() | |
if not completed: | |
simulation_results = run_simulation() | |
np.savez(str(output_path), **simulation_results) | |
else: | |
simulation_results = dict(np.load(str(output_path))) | |
print(simulation_results['x'].mean(), simulation_results['x'].std()) | |
print(simulation_results['y'].mean(), simulation_results['y'].std()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment