markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
As another example, the r.mapcalc wrapper for raster algebra allows using a long expressions. | gscript.mapcalc("elev_strip = if(elevation > 100 && elevation < 125, elevation, null())")
print(gscript.read_command('r.univar', map='elev_strip', flags='g')) | GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb | OSGeo-live/CesiumWidget | apache-2.0 |
The g.region wrapper is a convenient way to retrieve the current region settings (i.e., computational region). It returns a dictionary with values converted to appropriate types (floats and ints). | region = gscript.region()
print region
# cell area in map units (in projected Locations)
region['nsres'] * region['ewres'] | GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb | OSGeo-live/CesiumWidget | apache-2.0 |
We can list data stored in a GRASS GIS location with g.list wrappers. With this function, the map layers are grouped by mapsets (in this example, raster layers): | gscript.list_grouped(['raster']) | GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb | OSGeo-live/CesiumWidget | apache-2.0 |
Here is an example of a different g.list wrapper which structures the output as list of pairs (name, mapset).
We obtain current mapset with g.gisenv wrapper. | current_mapset = gscript.gisenv()['MAPSET']
gscript.list_pairs('raster', mapset=current_mapset) | GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb | OSGeo-live/CesiumWidget | apache-2.0 |
Example with nested json/dict like data, which has been pre-aggregated and pivoted | df2 = df_from_json(data)
df2 = df2.sort('total', ascending=False)
df2 = df2.head(10)
df2 = pd.melt(df2, id_vars=['abbr', 'name'])
scatter5 = Scatter(
df2, x='value', y='name', color='variable', title="x='value', y='name', color='variable'",
xlabel="Medals", ylabel="Top 10 Countries", legend='bottom_right')
sho... | examples/howto/charts/scatter.ipynb | azjps/bokeh | bsd-3-clause |
Use blend operator to "stack" variables | scatter6 = Scatter(flowers, x=blend('petal_length', 'sepal_length', name='length'),
y=blend('petal_width', 'sepal_width', name='width'), color='species',
title='x=petal_length+sepal_length, y=petal_width+sepal_width, color=species',
legend='top_right')
show(scatt... | examples/howto/charts/scatter.ipynb | azjps/bokeh | bsd-3-clause |
Train a MNIST model. | tf.reset_default_graph()
images, one_hot_labels, _ = data_fn(num_epochs=None, shuffle=True, initializable=False)
loss, predictions = model_fn(images, one_hot_labels)
accuracy = tf.reduce_mean(tf.to_float(tf.equal(tf.math.argmax(predictions, axis=1),
tf.math.argmax(one_hot_labels, axis=1)))... | tf/mnist_spectral_density.ipynb | google/spectral-density | apache-2.0 |
Run Lanczos on the MNIST model. | tf.reset_default_graph()
checkpoint_to_load = os.path.join(TRAIN_PATH, 'model.ckpt-10000')
# For Lanczos, the tf.data pipeline should have some very specific characteristics:
# 1. It should stop after a single epoch.
# 2. It should be deterministic (i.e., no data augmentation).
# 3. It should be initializable (we use... | tf/mnist_spectral_density.ipynb | google/spectral-density | apache-2.0 |
Visualize the Hessian eigenvalue density. | # Outputs are saved as numpy saved files. The most interesting ones are
# 'tridiag_1' and 'lanczos_vec_1'.
with open(os.path.join(LANCZOS_PATH, 'tridiag_1'), 'rb') as f:
tridiagonal = np.load(f)
# For legacy reasons, we need to squeeze tridiagonal.
tridiagonal = np.squeeze(tridiagonal)
# Note that the output ... | tf/mnist_spectral_density.ipynb | google/spectral-density | apache-2.0 |
Read in the list of questions/attributes
There were 13 questions | # this csv file has only a single row
questions = []
with open('data/SportsDataset_ListOfAttributes.csv','r') as csvfile:
myreader = csv.reader( csvfile )
for row in myreader:
questions = row
Question2Index = {}
for ind, quest in enumerate( questions ):
Question2Index[quest] = ind
print('Questio... | notebooks/20Q/setup_sportsDataset.ipynb | jamesfolberth/jupyterhub_AWS_deployment | bsd-3-clause |
Read in the training data
The columns of X correspond to questions, and rows correspond to more data. The rows of y are the movie indices. The values of X are 1, -1 or 0 (see YesNoDict for encoding) | YesNoDict = { "Yes": 1, "No": -1, "Unsure": 0, "": 0 }
# Load from the csv file.
# Note: the file only has "1"s, because blanks mean "No"
X = []
with open('data/SportsDataset_DataAttributes.csv','r') as csvfile:
myreader = csv.reader(csvfile)
for row in myreader:
data = [];
for col in row:
... | notebooks/20Q/setup_sportsDataset.ipynb | jamesfolberth/jupyterhub_AWS_deployment | bsd-3-clause |
Your turn: train a decision tree classifier | from sklearn import tree
# the rest is up to you | notebooks/20Q/setup_sportsDataset.ipynb | jamesfolberth/jupyterhub_AWS_deployment | bsd-3-clause |
Use the trained classifier to play a 20 questions game
You may want to use from sklearn.tree import _tree and 'tree.DecisionTreeClassifier' with commands like tree_.children_left[node], tree_.value[node], tree_.feature[node], and `tree_.threshold[node]'. | # up to you | notebooks/20Q/setup_sportsDataset.ipynb | jamesfolberth/jupyterhub_AWS_deployment | bsd-3-clause |
graded = 8/8 | import requests
response = requests.get("http://api.nytimes.com/svc/books/v2/lists.json?list=hardcover-fiction&published-date=2009-05-10&api-key=b577eb5b46ad4bec8ee159c89208e220")
best_seller = response.json()
print(best_seller.keys())
print(type(best_seller))
print(type(best_seller['results']))
print(len(best_sell... | foundations_hw/05/Homework5_Graded.ipynb | mercybenzaquen/foundations-homework | mit |
2) What are all the different book categories the NYT ranked in June 6, 2009? How about June 6, 2015? | import requests
response = requests.get("http://api.nytimes.com/svc/books/v2/lists/names.json?published-date=2009-06-06&api-key=b577eb5b46ad4bec8ee159c89208e220")
best_seller = response.json()
print(best_seller.keys())
print(len(best_seller['results']))
book_categories_2009 = best_seller['results']
for item in b... | foundations_hw/05/Homework5_Graded.ipynb | mercybenzaquen/foundations-homework | mit |
3) Muammar Gaddafi's name can be transliterated many many ways. His last name is often a source of a million and one versions - Gadafi, Gaddafi, Kadafi, and Qaddafi to name a few. How many times has the New York Times referred to him by each of those names?
Tip: Add "Libya" to your search to make sure (-ish) you're tal... | import requests
response = requests.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=Gadafi&fq=Libya&api-key=b577eb5b46ad4bec8ee159c89208e220")
gadafi = response.json()
print(gadafi.keys())
print(gadafi['response'])
print(gadafi['response'].keys())
print(gadafi['response']['docs']) #so no results for GAD... | foundations_hw/05/Homework5_Graded.ipynb | mercybenzaquen/foundations-homework | mit |
4) What's the title of the first story to mention the word 'hipster' in 1995? What's the first paragraph? | import requests
response = requests.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?q=hipster&begin_date=19950101&end_date=19953112&sort=oldest&api-key=b577eb5b46ad4bec8ee159c89208e220")
hipster = response.json()
print(hipster.keys())
print(hipster['response'].keys())
print(hipster['response']['docs'][0... | foundations_hw/05/Homework5_Graded.ipynb | mercybenzaquen/foundations-homework | mit |
5) How many times was gay marriage mentioned in the NYT between 1950-1959, 1960-1969, 1970-1978, 1980-1989, 1990-2099, 2000-2009, and 2010-present? | import requests
response = requests.get('https://api.nytimes.com/svc/search/v2/articlesearch.json?q="gay marriage"&begin_date=19500101&end_date=19593112&api-key=b577eb5b46ad4bec8ee159c89208e220')
marriage_1959 = response.json()
print(marriage_1959.keys())
print(marriage_1959['response'].keys())
print(marriage_1959['re... | foundations_hw/05/Homework5_Graded.ipynb | mercybenzaquen/foundations-homework | mit |
6) What section talks about motorcycles the most?
Tip: You'll be using facets | import requests
response = requests.get("http://api.nytimes.com/svc/search/v2/articlesearch.json?q=motorcycles&facet_field=section_name&api-key=b577eb5b46ad4bec8ee159c89208e220")
motorcycles = response.json()
print(motorcycles.keys())
print(motorcycles['response'].keys())
print(motorcycles['response']['facets']['s... | foundations_hw/05/Homework5_Graded.ipynb | mercybenzaquen/foundations-homework | mit |
7) How many of the last 20 movies reviewed by the NYT were Critics' Picks? How about the last 40? The last 60?
Tip: You really don't want to do this 3 separate times (1-20, 21-40 and 41-60) and add them together. What if, perhaps, you were able to figure out how to combine two lists? Then you could have a 1-20 list, a ... | import requests
response = requests.get('http://api.nytimes.com/svc/movies/v2/reviews/search.json?api-key=b577eb5b46ad4bec8ee159c89208e220')
movies_reviews_20 = response.json()
print(movies_reviews_20.keys())
print(movies_reviews_20['results'][0])
critics_pick = 0
not_a_critics_pick = 0
for item in movies_reviews... | foundations_hw/05/Homework5_Graded.ipynb | mercybenzaquen/foundations-homework | mit |
8) Out of the last 40 movie reviews from the NYT, which critic has written the most reviews? | medium_list = movies_reviews_20['results'] + movies_reviews_40['results']
print(type(medium_list))
print(medium_list[0])
for item in medium_list:
print(item['byline'])
all_critics = []
for item in medium_list:
all_critics.append(item['byline'])
print(all_critics)
unique_medium_list = set(all_critics)
pr... | foundations_hw/05/Homework5_Graded.ipynb | mercybenzaquen/foundations-homework | mit |
If we want to figure out the maximum value, we'll obviously need a loop to check each element of the list (which we know how to do), and a variable to store the maximum. | max_val = 0
for element in x:
# ... now what?
pass | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
We also know we can check relative values, like max_val < element. If this evaluates to True, we know we've found a number in the list that's bigger than our current candidate for maximum value. But how do we execute code until this condition, and this condition alone?
Enter if / elif / else statements! (otherwise k... | x = 5
if x < 5:
print("How did this happen?!") # Spoiler alert: this won't happen.
if x == 5:
print("Working as intended.") | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
In conjunction with if, we also have an else clause that we can use to execute whenever the if statement doesn't: | x = 5
if x < 5:
print("How did this happen?!") # Spoiler alert: this won't happen.
else:
print("Correct.") | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
This is great! We can finally finish computing the maximum element of a list! | x = [51, 65, 56, 19, 11, 49, 81, 59, 45, 73]
max_val = 0
for element in x:
if max_val < element:
max_val = element
print("The maximum element is: {}".format(max_val)) | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
We can test conditions! But what if we have multifaceted decisions to make? Let's look at a classic example: assigning letter grades from numerical grades. | student_grades = {
'Jen': 82,
'Shannon': 75,
'Natasha': 94,
'Benjamin': 48,
} | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
We know the 90-100 range is an "A", 80-89 is a "B", and so on. We can't do just a standard "if / else", since we have more than two possibilities here.
The third and final component of conditionals is the elif statement (short for "else if").
elif allows us to evaluate as many options as we'd like, all within the same ... | for student, grade in student_grades.items():
letter = ''
if grade >= 90:
letter = "A"
elif grade >= 80:
letter = "B"
elif grade >= 70:
letter = "C"
elif grade >= 60:
letter = "D"
else:
letter = "F"
print("{}'s letter grade: {}".format(student, le... | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
Ok, that's neat. But there's still one more edge case: what happens if we want to enforce multiple conditions simultaneously?
To illustrate, let's go back to our example of finding the maximum value in a list, and this time, let's try to find the second-largest value in the list. For simplicity, let's say we've already... | x = [51, 65, 56, 19, 11, 49, 81, 59, 45, 73]
max_val = 81 # We've already found it!
second_largest = 0 | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
Here's the rub: we now have two constraints to enforce--the second largest value needs to be larger than pretty much everything in the list, but also needs to be smaller than the maximum value. Not something we can encode using if / elif / else.
Instead, we'll use two more keywords integral to conditionals: and and or. | for element in x:
if second_largest < element and element < max_val:
second_largest = element
print("The second-largest element is: {}".format(second_largest)) | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
The first condition, second_largest < element, is the same as before: if our current estimate of the second largest element is smaller than the latest element we're looking at, it's definitely a candidate for second-largest.
The second condition, element < max_val, is what ensures we don't just pick the largest... | second_largest = 0
for element in x:
if second_largest < element:
if element < max_val:
second_largest = element
print("The second-largest element is: {}".format(second_largest)) | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
...but your code starts getting a little unwieldy with so many indentations.
You can glue as many comparisons as you want together with and; the whole statement will only be True if every single condition evaluates to True. This is what and means: everything must be True.
The other side of this coin is or. Like and, yo... | numbers = [1, 2, 5, 6, 7, 9, 10]
for num in numbers:
if num == 2 or num == 4 or num == 6 or num == 8 or num == 10:
print("{} is an even number.".format(num)) | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
In this contrived example, I've glued together a bunch of constraints. Obviously, these constraints are mutually exclusive; a number can't be equal to both 2 and 4 at the same time, so num == 2 and num == 4 would never evaluate to True. However, using or, only one of them needs to be True for the statement underneath t... | import random
list_of_numbers = [random.randint(1, 100) for i in range(10)] # Generaets 10 random numbers, between 1 and 100.
if 13 not in list_of_numbers:
print("Aw man, my lucky number isn't here!") | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
Notice a couple things here--
List comprehensions make an appearance! Can you parse it out?
The if statement asks if the number 13 is NOT found in list_of_numbers
When that statement evaluates to True--meaning the number is NOT found--it prints the message.
If you omit the not keyword, then the question becomes: "is ... | import random
list_of_numbers = [random.randint(1, 2) for i in range(10)] # Generaets 10 random numbers, between 1 and 2. Yep.
if 1 in list_of_numbers:
print("Sweet, found a 1!") | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
This works for strings as well: 'some_string' in some_list will return True if that string is indeed found in the list.
Be careful with this. Typing issues can hit you full force here: if you ask if 0 in some_list, and it's a list of floats, then this operation will always evaluate to False.
Similarly, if you ask if "s... | def divide(x, y):
return x / y
divide(11, 0) | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
D'oh! The user fed us a 0 for the denominator and broke our calculator. Meanie-face.
So we know there's a possibility of the user entering a 0. This could be malicious or simply by accident. Since it's only one value that could crash our app, we could in principle have an if statement that checks if the denominator is ... | def divide_safe(x, y):
quotient = 0
try:
quotient = x / y
except ZeroDivisionError:
print("You tried to divide by zero. Why would you do that?!")
return quotient | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
Now if our user tries to be snarky again-- | divide_safe(11, 0) | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
No error, no crash! Just a "helpful" error message.
Like conditionals, you can also create multiple except statements to handle multiple different possible exceptions: | import random # For generating random exceptions.
num = random.randint(0, 1)
try:
if num == 1:
raise NameError("This happens when you use a variable you haven't defined")
else:
raise ValueError("This happens when you try to multiply a string")
except NameError:
print("Caught a NameError!")
... | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
If you download this notebook or run it with mybinder and re-run the above cell, the exception should flip randomly between the two.
Also like conditionals, you can handle multiple errors simultaneously. If, like in the previous example, your code can raise multiple exceptions, but you want to handle them all the same ... | import random # For generating random exceptions.
num = random.randint(0, 1)
try:
if num == 1:
raise NameError("This happens when you use a variable you haven't defined")
else:
raise ValueError("This happens when you try to multiply a string")
except (NameError, ValueError): # MUST have the pa... | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
If you're like me, and you're writing code that you know could raise one of several errors, but are too lazy to look up specifically what errors are possible, you can create a "catch-all" by just not specifying anything: | import random # For generating random exceptions.
num = random.randint(0, 1)
try:
if num == 1:
raise NameError("This happens when you use a variable you haven't defined")
else:
raise ValueError("This happens when you try to multiply a string")
except:
print("I caught something!") | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
Finally--and this is really getting into what's known as control flow (quite literally: "controlling the flow" of your program)--you can tack an else statement onto the very end of your exception-handling block to add some final code to the handler.
Why? This is code that is only executed if NO exception occurs. Let's ... | import random # For generating random exceptions.
num = random.randint(0, 1)
try:
if num == 1:
raise NameError("This happens when you use a variable you haven't defined")
except:
print("I caught something!")
else:
print("HOORAY! Lucky coin flip!") | lectures/L6.ipynb | eds-uga/csci1360e-su16 | mit |
Load data and prep model for estimation | modelname="school_location"
from activitysim.estimation.larch import component_model
model, data = component_model(modelname, return_data=True) | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
Review data loaded from EDB
Next we can review what was read the EDB, including the coefficients, model settings, utilities specification, and chooser and alternative data.
coefficients | data.coefficients | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
alt_values | data.alt_values | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
chooser_data | data.chooser_data | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
landuse | data.landuse | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
spec | data.spec | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
size_spec | data.size_spec | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
Estimate
With the model setup for estimation, the next step is to estimate the model coefficients. Make sure to use a sufficiently large enough household sample and set of zones to avoid an over-specified model, which does not have a numerically stable likelihood maximizing solution. Larch has a built-in estimation m... | model.estimate(method='BHHH', options={'maxiter':1000}) | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
Output Estimation Results | from activitysim.estimation.larch import update_coefficients, update_size_spec
result_dir = data.edb_directory/"estimated" | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
Write updated utility coefficients | update_coefficients(
model, data, result_dir,
output_file=f"{modelname}_coefficients_revised.csv",
); | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
Write updated size coefficients | update_size_spec(
model, data, result_dir,
output_file=f"{modelname}_size_terms.csv",
) | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
Write the model estimation report, including coefficient t-statistic and log likelihood | model.to_xlsx(
result_dir/f"{modelname}_model_estimation.xlsx",
data_statistics=False,
); | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
Next Steps
The final step is to either manually or automatically copy the *_coefficients_revised.csv file and *_size_terms.csv file to the configs folder, rename them to *_coefficients.csv and destination_choice_size_terms.csv, and run ActivitySim in simulation mode. Note that all the location
and desintation choice m... | pd.read_csv(result_dir/f"{modelname}_coefficients_revised.csv")
pd.read_csv(result_dir/f"{modelname}_size_terms.csv") | activitysim/examples/example_estimation/notebooks/02_school_location.ipynb | synthicity/activitysim | agpl-3.0 |
Simple absorbing boundary for 2D acoustic FD modelling
Realistic FD modelling results for surface seismic acquisition geometries require a further modification of the 2D acoustic FD code. Except for the free surface boundary condition on top of the model, we want to suppress the artifical reflections from the other bou... | # Import Libraries
# ----------------
import numpy as np
from numba import jit
import matplotlib
import matplotlib.pyplot as plt
from pylab import rcParams
# Ignore Warning Messages
# -----------------------
import warnings
warnings.filterwarnings("ignore")
from mpl_toolkits.axes_grid1 import make_axes_locatable
# ... | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
In order to modularize the code, we move the 2nd partial derivatives of the wave equation into a function update_d2px_d2pz, so the application of the JIT decorator can be restriced to this function: | @jit(nopython=True) # use JIT for C-performance
def update_d2px_d2pz(p, dx, dz, nx, nz, d2px, d2pz):
for i in range(1, nx - 1):
for j in range(1, nz - 1):
d2px[i,j] = (p[i + 1,j] - 2 * p[i,j] + p[i - 1,j]) / dx**2
d2pz[i,j] = (p[i,j + 1] - 2 * p[... | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
In the FD modelling code FD_2D_acoustic_JIT, a more flexible model definition is introduced by the function model. The block Initalize animation of pressure wavefield before the time loop displays the velocity model and initial pressure wavefield. During the time-loop, the pressure wavefield is updated with ... | # FD_2D_acoustic code with JIT optimization
# -----------------------------------------
def FD_2D_acoustic_JIT(dt,dx,dz,f0):
# define model discretization
# ---------------------------
nx = (int)(xmax/dx) # number of grid points in x-direction
print('nx = ',nx)
nz = (int)(zmax/dz) # n... | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
Homogeneous block model without absorbing boundary frame
As a reference, we first model the homogeneous block model, defined in the function model, without an absorbing boundary frame: | # Homogeneous model
def model(nx,nz,vp,dx,dz):
vp += vp0
return vp | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
After defining the modelling parameters, we can run the modified FD code ... | %matplotlib notebook
dx = 5.0 # grid point distance in x-direction (m)
dz = dx # grid point distance in z-direction (m)
f0 = 100.0 # centre frequency of the source wavelet (Hz)
# calculate dt according to the CFL-criterion
dt = dx / (np.sqrt(2.0) * vp0)
FD_2D_acoustic_JIT(dt,dx,dz,f0) | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
Notice the strong, artifical boundary reflections in the wavefield movie
Simple absorbing Sponge boundary
The simplest, and unfortunately least efficient, absorbing boundary was developed by Cerjan et al. (1985). It is based on the simple idea to damp the pressure wavefields $p^n_{i,j}$ and $p^{n+1}_{i,j}$ in an absorb... | # Define simple absorbing boundary frame based on wavefield damping
# according to Cerjan et al., 1985, Geophysics, 50, 705-708
def absorb(nx,nz):
FW = # thickness of absorbing frame (gridpoints)
a = # damping variation within the frame
coeff = np.zeros(FW)
# define coefficient... | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
This implementation of the Sponge boundary sets a free-surface boundary condition on top of the model, while inciding waves at the other boundaries are absorbed: | # Plot absorbing damping profile
# ------------------------------
fig = plt.figure(figsize=(6,4)) # define figure size
extent = [0.0,xmax,0.0,zmax] # define model extension
# calculate absorbing boundary weighting coefficients
nx = 400
nz = 400
absorb_coeff = absorb(nx,nz)
plt.imshow(absorb_coeff.T)
plt.colorba... | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
The FD code itself requires only some small modifications, we have to add the absorb function to define the amount of damping in the boundary frame and apply the damping function to the pressure wavefields pnew and p | # FD_2D_acoustic code with JIT optimization
# -----------------------------------------
def FD_2D_acoustic_JIT_absorb(dt,dx,dz,f0):
# define model discretization
# ---------------------------
nx = (int)(xmax/dx) # number of grid points in x-direction
print('nx = ',nx)
nz = (int)(zmax/... | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
Let's evaluate the influence of the Sponge boundaries on the artifical boundary reflections: | %matplotlib notebook
dx = 5.0 # grid point distance in x-direction (m)
dz = dx # grid point distance in z-direction (m)
f0 = 100.0 # centre frequency of the source wavelet (Hz)
# calculate dt according to the CFL-criterion
dt = dx / (np.sqrt(2.0) * vp0)
FD_2D_acoustic_JIT_absorb(dt,dx,dz,f0) | 05_2D_acoustic_FD_modelling/lecture_notebooks/4_fdac2d_absorbing_boundary.ipynb | daniel-koehn/Theory-of-seismic-waves-II | gpl-3.0 |
NumPy
Para importar numpy, utilize:
import numpy as np
Você também pode utilizar:
from numpy import * . Isso evitará a utilização de np., mas este comando importará todos os módulos do NumPy.
Para atualizar o NumPy, abra o prompt de comando e digite: pip install numpy -U | # Importando o NumPy
import numpy as np
np.__version__ | Cap08/Notebooks/DSA-Python-Cap08-01-NumPy.ipynb | dsacademybr/PythonFundamentos | gpl-3.0 |
Criando Arrays | # Help
help(np.array)
# Array criado a partir de uma lista:
vetor1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
print(vetor1)
# Um objeto do tipo ndarray é um recipiente multidimensional de itens do mesmo tipo e tamanho.
type(vetor1)
# Usando métodos do array NumPy
vetor1.cumsum()
# Criando uma lista. Perceba como list... | Cap08/Notebooks/DSA-Python-Cap08-01-NumPy.ipynb | dsacademybr/PythonFundamentos | gpl-3.0 |
Funções NumPy | # A função arange cria um vetor contendo uma progressão aritmética a partir de um intervalo - start, stop, step
vetor2 = np.arange(0., 4.5, .5)
print(vetor2)
# Verificando o tipo do objeto
type(vetor2)
# Formato do array
np.shape(vetor2)
print (vetor2.dtype)
x = np.arange(1, 10, 0.25)
print(x)
print(np.zeros(10))... | Cap08/Notebooks/DSA-Python-Cap08-01-NumPy.ipynb | dsacademybr/PythonFundamentos | gpl-3.0 |
Criando Matrizes | # Criando uma matriz
matriz = np.array([[1,2,3],[4,5,6]])
print(matriz)
print(matriz.shape)
# Criando uma matriz 2x3 apenas com números "1"
matriz1 = np.ones((2,3))
print(matriz1)
# Criando uma matriz a partir de uma lista de listas
lista = [[13,81,22], [0, 34, 59], [21, 48, 94]]
# A função matrix cria uma matri... | Cap08/Notebooks/DSA-Python-Cap08-01-NumPy.ipynb | dsacademybr/PythonFundamentos | gpl-3.0 |
Usando o Método random() do NumPy | print(np.random.rand(10))
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib as mat
mat.__version__
print(np.random.rand(10))
plt.show((plt.hist(np.random.rand(1000))))
print(np.random.randn(5,5))
plt.show(plt.hist(np.random.randn(1000)))
imagem = np.random.rand(30, 30)
plt.imshow(imagem, cmap ... | Cap08/Notebooks/DSA-Python-Cap08-01-NumPy.ipynb | dsacademybr/PythonFundamentos | gpl-3.0 |
Operações com datasets | import os
filename = os.path.join('iris.csv')
# No Windows use !more iris.csv. Mac ou Linux use !head iris.csv
!head iris.csv
#!more iris.csv
# Carregando um dataset para dentro de um array
arquivo = np.loadtxt(filename, delimiter=',', usecols=(0,1,2,3), skiprows=1)
print (arquivo)
type(arquivo)
# Gerando um plot a... | Cap08/Notebooks/DSA-Python-Cap08-01-NumPy.ipynb | dsacademybr/PythonFundamentos | gpl-3.0 |
Estatística | # Criando um array
A = np.array([15, 23, 63, 94, 75])
# Em estatística a média é o valor que aponta para onde mais se concentram os dados de uma distribuição.
np.mean(A)
# O desvio padrão mostra o quanto de variação ou "dispersão" existe em
# relação à média (ou valor esperado).
# Um baixo desvio padrão indica que ... | Cap08/Notebooks/DSA-Python-Cap08-01-NumPy.ipynb | dsacademybr/PythonFundamentos | gpl-3.0 |
Outras Operações com Arrays | # Slicing
a = np.diag(np.arange(3))
a
a[1, 1]
a[1]
b = np.arange(10)
b
# [start:end:step]
b[2:9:3]
# Comparação
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
a == b
np.array_equal(a, b)
a.min()
a.max()
# Somando um elemento ao array
np.array([1, 2, 3]) + 1.5
# Usando o método around
a = np.array([1... | Cap08/Notebooks/DSA-Python-Cap08-01-NumPy.ipynb | dsacademybr/PythonFundamentos | gpl-3.0 |
Figure out how to use np.random.choice to simulate 1,000 tosses of a fair coin
np.random uses a "pseudorandom" number generator to simulate choices
String of numbers that has the same statistical properties as random numbers
Numbers are actually generated deterministically
Numbers look random... | numbers = np.random.random(100000)
plt.hist(numbers) | chapters/01_simulation/00_random-sampling.ipynb | harmsm/pythonic-science | unlicense |
But numbers are actually deterministic... | def simple_psuedo_random(current_value,
multiplier=13110243,
divisor=13132):
return current_value*multiplier % divisor
seed = 10218888
out = []
current = seed
for i in range(1000):
current = simple_psuedo_random(current)
out.append(current)
plt.hist(ou... | chapters/01_simulation/00_random-sampling.ipynb | harmsm/pythonic-science | unlicense |
python uses the Mersenne Twister to generate pseudorandom numbers
What does the seed do? | seed = 1021888
out = []
current = seed
for i in range(1000):
current = simple_psuedo_random(current)
out.append(current)
| chapters/01_simulation/00_random-sampling.ipynb | harmsm/pythonic-science | unlicense |
What will we see if I run this cell twice in a row? | s1 = np.random.random(10)
print(s1)
| chapters/01_simulation/00_random-sampling.ipynb | harmsm/pythonic-science | unlicense |
What will we see if I run this cell twice in a row? | np.random.seed(5235412)
s1 = np.random.random(10)
print(s1)
| chapters/01_simulation/00_random-sampling.ipynb | harmsm/pythonic-science | unlicense |
A seed lets you specify which pseudo-random numbers you will use.
If you use the same seed, you will get identical samples.
If you use a different seed, you will get wildly different samples.
matplotlib.pyplot.hist | numbers = np.random.normal(size=10000)
counts, bins, junk = plt.hist(numbers,
range(-10,10))
| chapters/01_simulation/00_random-sampling.ipynb | harmsm/pythonic-science | unlicense |
Basic histogram plotting syntax
python
COUNTS, BIN_EDGES, GRAPHICS_BIT = plt.hist(ARRAY_TO_BIN,BINS_TO_USE)
Figure out how the function works and report back to the class
What the function does
Arguments normal people would care about
What it returns | np.random.normal
np.random.binomial
np.random.uniform
np.random.poisson
np.random.choice
np.random.shuffle | chapters/01_simulation/00_random-sampling.ipynb | harmsm/pythonic-science | unlicense |
Use pandas to read the csv of the monthly-milk-production.csv file and set index_col='Month' | milk = pd.read_csv('monthly-milk-production.csv',index_col='Month') | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Check out the head of the dataframe | milk.head() | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Make the index a time series by using:
milk.index = pd.to_datetime(milk.index) | milk.index = pd.to_datetime(milk.index) | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Plot out the time series data. | milk.plot() | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Train Test Split
Let's attempt to predict a year's worth of data. (12 months or 12 steps into the future)
Create a test train split using indexing (hint: use .head() or tail() or .iloc[]). We don't want a random train test split, we want to specify that the test set is the last 3 months of data is the test set, with... | milk.info()
train_set = milk.head(156)
test_set = milk.tail(12) | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Scale the Data
Use sklearn.preprocessing to scale the data using the MinMaxScaler. Remember to only fit_transform on the training data, then transform the test data. You shouldn't fit on the test data as well, otherwise you are assuming you would know about future behavior! | from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
train_scaled = scaler.fit_transform(train_set)
test_scaled = scaler.transform(test_set) | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Batch Function
We'll need a function that can feed batches of the training data. We'll need to do several things that are listed out as steps in the comments of the function. Remember to reference the previous batch method from the lecture for hints. Try to fill out the function template below, this is a pretty hard s... | def next_batch(training_data,batch_size,steps):
"""
INPUT: Data, Batch Size, Time Steps per batch
OUTPUT: A tuple of y time series results. y[:,:-1] and y[:,1:]
"""
# STEP 1: Use np.random.randint to set a random starting point index for the batch.
# Remember that each batch needs have the ... | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Setting Up The RNN Model
Import TensorFlow | import tensorflow as tf | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
The Constants
Define the constants in a single cell. You'll need the following (in parenthesis are the values I used in my solution, but you can play with some of these):
* Number of Inputs (1)
* Number of Time Steps (12)
* Number of Neurons per Layer (100)
* Number of Outputs (1)
* Learning Rate (0.003)
* Number of ... | # Just one feature, the time series
num_inputs = 1
# Num of steps in each batch
num_time_steps = 12
# 100 neuron layer, play with this
num_neurons = 100
# Just one output, predicted time series
num_outputs = 1
## You can also try increasing iterations, but decreasing learning rate
# learning rate you can play with thi... | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Create Placeholders for X and y. (You can change the variable names if you want). The shape for these placeholders should be [None,num_time_steps-1,num_inputs] and [None, num_time_steps-1, num_outputs] The reason we use num_time_steps-1 is because each of these will be one step shorter than the original time steps size... | X = tf.placeholder(tf.float32, [None, num_time_steps, num_inputs])
y = tf.placeholder(tf.float32, [None, num_time_steps, num_outputs]) | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Now create the RNN Layer, you have complete freedom over this, use tf.contrib.rnn and choose anything you want, OutputProjectionWrappers, BasicRNNCells, BasicLSTMCells, MultiRNNCell, GRUCell etc... Keep in mind not every combination will work well! (If in doubt, the solutions used an Outputprojection Wrapper around a b... | # Also play around with GRUCell
cell = tf.contrib.rnn.OutputProjectionWrapper(
tf.contrib.rnn.BasicLSTMCell(num_units=num_neurons, activation=tf.nn.relu),
output_size=num_outputs) | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Now pass in the cells variable into tf.nn.dynamic_rnn, along with your first placeholder (X) | outputs, states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32) | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Loss Function and Optimizer
Create a Mean Squared Error Loss Function and use it to minimize an AdamOptimizer, remember to pass in your learning rate. | loss = tf.reduce_mean(tf.square(outputs - y)) # MSE
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train = optimizer.minimize(loss) | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Initialize the global variables | init = tf.global_variables_initializer() | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Create an instance of tf.train.Saver() | saver = tf.train.Saver() | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Session
Run a tf.Session that trains on the batches created by your next_batch function. Also add an a loss evaluation for every 100 training iterations. Remember to save your model after you are done training. | gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
sess.run(init)
for iteration in range(num_train_iterations):
X_batch, y_batch = next_batch(train_scaled,batch_size,num_time_steps)
sess.run(tra... | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Predicting Future (Test Data)
Show the test_set (the last 12 months of your original complete data set) | test_set | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Now we want to attempt to predict these 12 months of data, using only the training data we had. To do this we will feed in a seed training_instance of the last 12 months of the training_set of data to predict 12 months into the future. Then we will be able to compare our generated 12 months to our actual true historica... | with tf.Session() as sess:
# Use your Saver instance to restore your saved rnn time series model
saver.restore(sess, "./ex_time_series_model")
# Create a numpy array for your genreative seed from the last 12 months of the
# training set data. Hint: Just use tail(12) and then pass it to an np.arra... | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Show the result of the predictions. | train_seed | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Grab the portion of the results that are the generated values and apply inverse_transform on them to turn them back into milk production value units (lbs per cow). Also reshape the results to be (12,1) so we can easily add them to the test_set dataframe. | results = scaler.inverse_transform(np.array(train_seed[12:]).reshape(12,1)) | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Create a new column on the test_set called "Generated" and set it equal to the generated results. You may get a warning about this, feel free to ignore it. | test_set['Generated'] = results | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
View the test_set dataframe. | test_set | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Plot out the two columns for comparison. | test_set.plot() | 18-05-28-Complete-Guide-to-Tensorflow-for-Deep-Learning-with-Python/04-Recurrent-Neural-Networks/03-Time-Series-Exercise-Solutions-Final.ipynb | arcyfelix/Courses | apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.