markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
Scraping the Government Response | url='https://publications.parliament.uk/pa/cm201617/cmselect/cmwomeq/963/96302.htm'
#Inconsistency across different reports in terms of presentation, linking to evidence | notebooks/Committee Reports.ipynb | psychemedia/parlihacks | mit |
<p style="text-align: right; direction: rtl; float: right;">
ืืฉืืจื ืืจืืฉืื ื ืืื ืืืืืืฉ ืคื: ืืฉืืจื ืื ืื ืื ื ืืืงืฉืื ืงืื ืืืืฉืชืืฉ (ืืช ืืฉื ืฉืื), ืืฉืืืจืื ืืช ืืงืื ืฉืืืื ืืืฉืชื ื ืืฉื <code>name</code>.<br>
ืืจืืข ืฉืคืืืชืื ืืืืขื ืึพ<code dir="ltr">input()</code>, ืืื ืขืืฆืจืช ืื ืคืขืืื, ืขื ืฉืชืงืื ืงืื ืืืืฉืชืืฉ.<br>
ืืืืจ ืืื ืืื "ืืืืืคื" ืืช <c... | name = "Moishalah"
message = "Hello, " + name + "!"
print(message) | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="align: right; direction: rtl; float: right;">ืชืจืืื</p>
<p style="text-align: right; direction: rtl; float: right;">
ืืชืื ืงืื ืืืืงืฉ ืืงืื ืฉืืืฉื ื ืชืื ืื: ืฉื ืคืจืื, ืฉื ืืฉืคืื ืืชืืจืื ืืืื.<br>
ืืงืื ืืฆืื ืืืฉืชืืฉ ืืจืื ืืืืื.<br>
ืืืืืื, ืขืืืจ ืื ืชืื ืื <code>Israel</code>, <code>Cohen</code>, <code>22/07/1992</code>, ืืื ื... | type(5)
type(1.5)
type('Hello') | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right;">
ืื ืืชื ืืจืืืฉืื ืฉืืืฉ ืืกืคืงืชื ืืฉืืื, ืฉืืื ืืื ืืืฆืืฅ <a href="3_Types.ipynb">ืืคืจืง 3</a>, ืฉืืืื ืขื ืืืคืืกืื.
</p>
<p style="text-align: right; direction: rtl; float: right;">
ืื ื ืฉืชืขืฉืข ืืขื ืขื <code dir="ltr">input()</code>, ื ืืื ืืืจ ืืืื ืฉืืคืขืืื ืืงืื ืืืืฉืชืืฉ ืื ืื... | moshe_apples = input("How many apples does Moshe have? ")
orly_apples = input("How many apples does Orly have? ")
apples_together = moshe_apples + orly_apples
print("Together, they have " + apples_together + " apples!") | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
<div class="align-center" style="display: flex; text-align: right; direction: rtl; clear: both;">
<div style="display: flex; width: 10%; float: right; clear: both;">
<img src="images/exercise.svg" style="height: 50px !important;" alt="ืชืจืืื">
</div>
<div style="width: 70%">
<p style="text-ali... | moshe_apples = input("How many apples does Moshe have? ")
moshe_apples = moshe_apples + 1 # Give Moshe a single apple
print(moshe_apples) | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="align: right; direction: rtl; float: right;">ืืืจืช ืืืคืืกืื (Casting)</p>
<p style="text-align: right; direction: rtl; float: right;">
ืฉืคืื ื ืืืืจ ืืื ืืงืขืจื ืขื 5 ืงืืืืืช ืงืจื. ืืื ืืฉ ืื ืขืืฉืื?<br>
ืงืฉื ืื ื ืืืื ืืขื ืืช ืขื ืืฉืืื ืืืืื ืฉืืื ืื ืืกืืช ืืืืคื ืืจืืข ืืืขืจืืช ืืืจืื ืืกืืืื ืฉืื ืื. ืืืืชื ืกืืื ืืืืืง ืืคืืืชืื ืง... | color = input("What is your favorite color? ")
print("The type of the input " + color + " is...")
type(color)
age = input("What is your age? ")
print("The type of the input " + age + " is...")
type(age) | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right;">
ืืืืืจ, ืื ืขืื ืืงืื ืฉืื ื ืืื ืืกืื ืืืจืืืช, ืคืขืืืืช ืืื ืืืืืจ ืฉืื ืขื ืืกืคืจ ืืืืฉืื.<br>
ืืื ื ืฆืืจื ืืืืื ืฉืฉื ืืื ืืืื ืืืืชื ืกืื ืขื ืืื ืืืจื ืฉื ืืื ืืขืจืืื ืืกืื ืืื ืืกืื ืืืจ.<br>
ืชืืืื ืืคืืืช ืขืจื ืืกืื ืืืคืืก ืืืจ ื ืงืจื <dfn>ืืืจืช ืืืคืืกืื</dfn>, ืื <dfn>Casting</dfn> / <dfn>... | moshe_apples = input("How many apples does Moshe have? ")
moshe_apples = moshe_apples + 1 # Give Moshe a single apple
print(moshe_apples) | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right;">
ื ืจืื ืฉืืงืื ืื ืืขืืื, ืืืืื ืฉืืื ืืคืฉืจืืช ืืืืจ ืืื ืืืจืืืช (ืืกืคืจ ืืชืคืืืื ืฉื ืืฉื ืืืงืื ืฉื ืืืฉืชืืฉ) ืืืื ืืกืคืจ (ืึพ1 ืฉืื ืื ื ืจืืฆืื ืืืืกืืฃ).<br>
ืืืืื ืฉืืืืจื ืืื ืืืืกืืฃ ืชืคืื 1 ืืืกืคืจ ืืกืืื ืฉื ืชืคืืืื, ื ืืืจ ืืืืืจ ืืช <code>moshe_apples</code> ืืืกืคืจ ืฉืื (<code>int</code>) ืื... | moshe_apples = input("How many apples does Moshe have? ")
moshe_apples = int(moshe_apples) # <--- Casting
moshe_apples = moshe_apples + 1
print(moshe_apples) | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right;">
ืืืื ืืืฃ, ืืืจื ื ืืช ืืกืคืจ ืืชืคืืืื ืฉื ืืฉื ืืขืจื ืืืืคืืก ืฉืื (ืฉืืจื 2), ืืขืืฉืื ืืงืื ืขืืื!<br>
ืฉืืื ืื ืฉืขืืฉืื ืื ื ืจืฆื ืืืืคืืก ืืช ืืกืคืจ ืืชืคืืืื ืืฆื ืืฉืคื ืฉืืืืจ "ืืืฉื ืืฉ X ืชืคืืืื", ืื ืื ื ืขืืืืื ืืืืชืงื ืืืขืื.<br>
ืืืฉืคื ืฉืื ืื ื ืจืืฆืื ืืืืคืืก ืืื <code>str</code>, ืืืกืคืจ ืืชืคืืื... | moshe_apples = input("How many apples does Moshe have? ")
moshe_apples = int(moshe_apples) # <--- Casting
moshe_apples = moshe_apples + 1
print("Moshe has " + moshe_apples + " apples") | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
<p style="text-align: right; direction: rtl; float: right;">
ืคืืืชืื ืืชืจืืขื ืืคื ืื ื ืฉืืฉ ืคื ืืขืื: ืืฉืืจื ืืืืจืื ื, ืืื ืื ืืฆืืืื ืืืืจ ืืช ืืกืคืจ ืืชืคืืืื ืขื ืืืืจืืืืช ืื ืืฆืืืช ืืฆืืืื.<br>
ืื ืืคืชืจืื?<br>
ืื ืืืจืชื ืืืืืจ ืืช ืืกืคืจ ืืชืคืืืื ืฉื ืืฉื ืืืืจืืืช, ืื ืืื ืืขืืื. ื ืขืฉื ืืช ืื ืืื:
</p> | moshe_apples = input("How many apples does Moshe have? ")
moshe_apples = int(moshe_apples) # <--- Casting to int
moshe_apples = moshe_apples + 1
moshe_apples = str(moshe_apples) # <--- Casting to str
print("Moshe has " + moshe_apples + " apples") | week01/5_Input_and_Casting.ipynb | PythonFreeCourse/Notebooks | mit |
Data
First we load and pre-process data. | # load data
data_path = os.path.abspath(
os.path.join(
os.path.pardir,
'utilities_and_data',
'diabetes.csv'
)
)
data = pd.read_csv(data_path)
# print some basic info()
data.info()
# preview some first rows
data.head()
# some summary
data.describe() | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
Preprocess data | # modify the data column names slightly for easier typing
# rename DiabetesPedigreeFunction to dpf
data.rename(columns={'DiabetesPedigreeFunction': 'dpf'}, inplace=True)
# make lower
data.rename(columns=lambda old_name: old_name.lower(), inplace=True)
# removing those observation rows with 0 in selected variables
norm... | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
Stan model code for logistic regression
Logistic regression with Student's $t$ prior as discussed above. | with open('logistic_t.stan') as file:
print(file.read())
model = stan_utility.compile_model('logistic_t.stan') | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
Set priors and sample from the posterior
Here we'll use a Student t prior with 7 degrees of freedom and a scale of 2.5, which, as discussed above, is a reasonable default prior when coefficients should be close to zero but have some chance of being large. PyStan returns the posterior distribution for the parameters de... | data1 = dict(
n=n,
d=p,
X=X,
y=y,
p_alpha_df=7,
p_alpha_loc=0,
p_alpha_scale=2.5,
p_beta_df=7,
p_beta_loc=0,
p_beta_scale=2.5
)
fit1 = model.sampling(data=data1, seed=74749)
samples1 = fit1.extract(permuted=True) | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
Inspect the resulting posterior
Check n_effs and Rhats | # print summary of selected variables
# use pandas data frame for layout
summary = fit1.summary(pars=['alpha', 'beta'])
pd.DataFrame(
summary['summary'],
index=summary['summary_rownames'],
columns=summary['summary_colnames']
) | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
n_effs are high and Rhats<1.1, which is good.
Next we check divergences, E-BMFI and treedepth exceedences as explained in Robust Statistical Workflow with PyStan Case Study by Michael Betancourt. | stan_utility.check_treedepth(fit1)
stan_utility.check_energy(fit1)
stan_utility.check_div(fit1) | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
Everything is fine based on these diagnostics and we can proceed with our analysis.
Visualise the marginal posterior distributions of each parameter | # plot histograms
fig, axes = plot_tools.hist_multi_sharex(
[samples1['alpha']] + [sample for sample in samples1['beta'].T],
rowlabels=['intercept'] + list(X.columns),
n_bins=60,
x_lines=0,
figsize=(7, 10)
) | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
We can use Pareto smoothed importance sampling leave-one-out cross-validation to estimate the predictive performance. | loo1, loos1, ks1 = psis.psisloo(samples1['log_lik'])
loo1_se = np.sqrt(np.var(loos1, ddof=1)*n)
print('elpd_loo: {:.4} (SE {:.3})'.format(loo1, loo1_se))
# check the number of large (> 0.5) Pareto k estimates
np.sum(ks1 > 0.5) | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
Alternative horseshoe prior on weights
In this example, with $n \gg p$ the difference is small, and thus we donโt expect much difference with a different prior and horseshoe prior is usually more useful for $n<p$.
The global scale parameter for horseshoe prior is chosen as recommended by Juho Piironen and Aki Vehtari (... | with open('logistic_hs.stan') as file:
print(file.read())
model = stan_utility.compile_model('logistic_hs.stan')
p0 = 2 # prior guess for the number of relevant variables
tau0 = p0 / (p - p0) * 1 / np.sqrt(n)
data2 = dict(
n=n,
d=p,
X=X,
y=y,
p_alpha_df=7,
p_alpha_loc=0,
p_alpha_scale=... | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
We see that the horseshoe prior has shrunk the posterior distribution of irrelevant features closer to zero, without affecting the posterior distribution of the relevant features. | # print summary of selected variables
# use pandas data frame for layout
summary = fit2.summary(pars=['alpha', 'beta'])
pd.DataFrame(
summary['summary'],
index=summary['summary_rownames'],
columns=summary['summary_colnames']
)
# plot histograms
fig, axes = plot_tools.hist_multi_sharex(
[samples2['alpha... | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
We compute LOO also for the model with Horseshoe prior. Expected log predictive density is higher, but not significantly. This is not surprising as this is a easy data with $n \gg p$. | loo2, loos2, ks2 = psis.psisloo(samples2['log_lik'])
loo2_se = np.sqrt(np.var(loos2, ddof=1)*n)
print('elpd_loo: {:.4} (SE {:.3})'.format(loo2, loo2_se))
# check the number of large (> 0.5) Pareto k estimates
np.sum(ks2 > 0.5)
elpd_diff = loos2 - loos1
elpd_diff_se = np.sqrt(np.var(elpd_diff, ddof=1)*n)
elpd_diff = n... | demos_pystan/diabetes.ipynb | tsivula/becs-114.1311 | gpl-3.0 |
Numerically Integrating Newton's Second Law
There are many times in physics when you want to know the solution to a differential equation that you can't solve analytically. This comes up in fields from ranging from astrophysics to biophysics to particle physics. Once you change from finding exact solutions to numerical... | # Input Values
time = 40.
delta_t = .1
time_steps = int(time/delta_t)
# Create arrays for storing variables
x = np.zeros(time_steps)
v = np.zeros(time_steps)
t = np.zeros(time_steps)
# Set initial values to "stretched"
x[0] = 1.
v[0] = 0.
t[0] = 0.
omega = .75
# Create function to calculate acceleration
def accel(x,ome... | integration/Numerical Integration.ipynb | JesseLivezey/science-programming | mit |
Plots | plt.plot(t,x,'ro',label='x(t)')
plot(t,v,'bo',label='v(t)')
legend()
plt.ylim((-1.5,1.5))
plt.title('Numerical x(t) and v(t)')
plt.figure()
plt.plot(t,xExact,'ro',label='x(t)')
plot(t,vExact,'bo',label='v(t)')
legend()
plt.ylim((-1.5,1.5))
plt.title('Exact x(t) and v(t)') | integration/Numerical Integration.ipynb | JesseLivezey/science-programming | mit |
Try a few different values of delta_t. What happens as you make delta_t larger?
One subtle problem with the method we are using above is that it may not be conserving energy. You can see this happening as the amplitude grows over time. Let's try creating a quick "hack" to fix this.
Copy the position and velocity code f... | # Code goes here: | integration/Numerical Integration.ipynb | JesseLivezey/science-programming | mit |
What about friction?
How could you incorporate a drag force into this program? You can assume the drag force is proportional to velocity:
$$F_\text{drag} = -b v$$
Copy your code from above and add in a drag term. Do the resulting plots make sense? | # Code goes here:
# Plots go here: | integration/Numerical Integration.ipynb | JesseLivezey/science-programming | mit |
Load the run metadata. You can also just look through the directory, but this index file is convenient if (as we do here) you only want to download some of the files. | run_info = pd.read_csv(os.path.join(scratch_dir, 'index.tsv'), sep='\t')
# Filter to SQuAD 2.0 runs from either 2M MultiBERTs or the original BERT checkpoint ("public").
mask = run_info.task == "v2.0"
mask &= (run_info.n_steps == "2M") | (run_info.release == 'public')
run_info = run_info[mask]
run_info
# Download all ... | language/multiberts/multi_vs_original.ipynb | google-research/language | apache-2.0 |
Now we should have everything in our scratch directory, and can load individual predictions.
SQuAD has a monolithic eval script that isn't easily compatible with a bootstrap procedure (among other things, it parses a lot of JSON, and you don't want to do that in the inner loop!). Ultimately, though, it relies on comput... | # Import the SQuAD 2.0 eval script; we'll use some functions from this below.
import sys
sys.path.append(scratch_dir)
import evaluate_squad2 as squad_eval
# Load dataset
with open(os.path.join(scratch_dir, 'dev-v2.0.json')) as fd:
dataset = json.load(fd)['data'] | language/multiberts/multi_vs_original.ipynb | google-research/language | apache-2.0 |
The official script supports thresholding for no-answer, but the default settings ignore this and treat only predictions of emptystring ("") as no-answer. So, we can score on exact_raw and f1_raw directly. | exact_scores = {} # filename -> qid -> score
f1_scores = {} # filename -> qid -> score
for fname in tqdm(run_info.file):
with open(os.path.join(scratch_dir, fname)) as fd:
preds = json.load(fd)
exact_raw, f1_raw = squad_eval.get_raw_scores(dataset, preds)
exact_scores[fname] = exact_raw
... | language/multiberts/multi_vs_original.ipynb | google-research/language | apache-2.0 |
Run multibootstrap
base (L) is the original BERT checkpoint, expt (L') is MultiBERTs with 2M steps. Since we pre-computed the pointwise exact match and F1 scores for each run and each example, we can just pass dummy labels and use a simple average over predictions as our scoring function. | import multibootstrap
num_bootstrap_samples = 1000
selected_runs = run_info.copy()
selected_runs['seed'] = selected_runs['pretrain_id']
selected_runs['intervention'] = (selected_runs['release'] == 'multiberts')
# Dummy labels
dummy_labels = np.zeros_like(exact_scores[0]) # [num_examples]
score_fn = lambda y_true, y... | language/multiberts/multi_vs_original.ipynb | google-research/language | apache-2.0 |
TacticToolkit Introduction
TacticToolkit is a codebase to assist with machine learning and natural language processing. We build on top of sklearn, tensorflow, keras, nltk, spaCy and other popular libraries. The TacticToolkit will help throughout; from data acquisition to preprocessing to training to inference.
| Mo... | # until we can install, add parent dir to path so ttk is found
import sys
sys.path.insert(0, '..')
# basic imports
import pandas as pd
import numpy as np
import re
import matplotlib
%matplotlib inline
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)
import matplotlib.pyplot as plt | examples/2017-09-11_TacticToolkit_Intro.ipynb | tacticsiege/TacticToolkit | mit |
Let's start with some text
The ttk.text module includes classes and functions to make working with text easier. These are meant to supplement existing nltk and spaCy text processing, and often work in conjunction with these libraries. Below is an overview of some of the major components. We'll explore these objects w... | # simple text normalization
# apply individually
# apply to sentences
# simple text tokenization
# harder text tokenization
# sentence tokenization
# paragraph tokenization | examples/2017-09-11_TacticToolkit_Intro.ipynb | tacticsiege/TacticToolkit | mit |
Corpii? Corpuses? Corpora!
The ttk.corpus module builds on the nltk.corpus model, adding new corpus readers and corpus processing objects. It also includes loading functions for the corpora included with ttk, which will download the content from github as needed.
We'll use the Dated Headline corpus included with ttk. ... | from ttk.corpus import load_headline_corpus
# load the dated corpus.
# This will attempt to download the corpus from github if it is not present locally.
corpus = load_headline_corpus(verbose=True)
# inspect categories
print (len(corpus.categories()), 'categories')
for cat in corpus.categories():
print (cat)
# ... | examples/2017-09-11_TacticToolkit_Intro.ipynb | tacticsiege/TacticToolkit | mit |
Examining the survival statistics, a large majority of males did not survive the ship sinking. However, a majority of females did survive the ship sinking. Let's build on our previous prediction: If a passenger was female, then we will predict that they survived. Otherwise, we will predict the passenger did not survive... | def predictions_1(data):
""" Model with one feature:
- Predict a passenger survived if they are female. """
predictions = []
for _, passenger in data.iterrows():
# Remove the 'pass' statement below
# and write your prediction conditions here
predictions.ap... | P0:_Titanic_Survival/Titanic_Survival_Exploration.ipynb | parambharat/ML-Programs | mit |
Examining the survival statistics, the majority of males younger then 10 survived the ship sinking, whereas most males age 10 or older did not survive the ship sinking. Let's continue to build on our previous prediction: If a passenger was female, then we will predict they survive. If a passenger was male and younger t... | def predictions_2(data):
""" Model with two features:
- Predict a passenger survived if they are female.
- Predict a passenger survived if they are male and younger than 10. """
predictions = []
for _, passenger in data.iterrows():
# Remove the 'pass' statement... | P0:_Titanic_Survival/Titanic_Survival_Exploration.ipynb | parambharat/ML-Programs | mit |
After exploring the survival statistics visualization, fill in the missing code below so that the function will make your prediction.
Make sure to keep track of the various features and conditions you tried before arriving at your final prediction model.
Hint: You can start your implementation of this function using th... | def predictions_3(data):
""" Model with multiple features. Makes a prediction with an accuracy of at least 80%. """
predictions = []
for _, passenger in data.iterrows():
# Remove the 'pass' statement below
# and write your prediction conditions here
if passenger["Pclas... | P0:_Titanic_Survival/Titanic_Survival_Exploration.ipynb | parambharat/ML-Programs | mit |
ะกะพััะธัะพะฒะบะฐ ะฒ ะพะฑัะฐัะฝะพะผ ะฟะพััะดะบะต
ะะปั ัะพััะธัะพะฒะบะธ ะฒ ะพะฑัะฐัะฝะพะผ ะฟะพััะดะบะต ะผะพะถะฝะพ ัะบะฐะทะฐัั ะฟะฐัะฐะผะตัั reverse. | a = [5, 3, -2, 9, 1]
a.sort(reverse=True)
print(a) | crash-course/builtin-sort.ipynb | citxx/sis-python | mit |
ะกะพััะธัะพะฒะบะฐ ะฟะพ ะบะปััั
ะกะพััะธัะพะฒะบะฐ ะฟะพ ะบะปััั ะฟะพะทะฒะพะปัะตั ะพััะพััะธัะพะฒะฐัั ัะฟะธัะพะบ ะฝะต ะฟะพ ะทะฝะฐัะตะฝะธั ัะฐะผะพะณะพ ัะปะตะผะตะฝัะฐ, ะฐ ะฟะพ ัะตะผั-ัะพ ะดััะณะพะผั. | # ะะฑััะฝะพ ัััะพะบะธ ัะพััะธัััััั ะฒ ะฐะปัะฐะฒะธัะฝะพะผ ะฟะพััะดะบะต
a = ["bee", "all", "accessibility", "zen", "treasure"]
a.sort()
print(a)
# ะ ะธัะฟะพะปัะทัั ัะพััะธัะพะฒะบั ะฟะพ ะบะปััั ะผะพะถะฝะพ ัะพััะธัะพะฒะฐัั, ะฝะฐะฟัะธะผะตั, ะฟะพ ะดะปะธะฝะต
a = ["bee", "all", "accessibility", "zen", "treasure"]
a.sort(key=len)
print(a) | crash-course/builtin-sort.ipynb | citxx/sis-python | mit |
ะ ะบะฐัะตััะฒะต ะฟะฐัะฐะผะตััะฐ key ะผะพะถะฝะพ ัะบะฐะทัะฒะฐัั ะฝะต ัะพะปัะบะพ ะฒัััะพะตะฝะฝัะต ััะฝะบัะธะธ, ะฝะพ ะธ ัะฐะผะพััะพััะตะปัะฝะพ ะพะฟัะตะดะตะปัะฝะฝัะต. ะขะฐะบะฐั ััะฝะบัะธั ะดะพะปะถะฝะฐ ะฟัะธะฝะธะผะฐัั ะพะดะธะฝ ะฐัะณัะผะตะฝั, ัะปะตะผะตะฝั ัะฟะธัะบะฐ, ะธ ะฒะพะทัะฐัะฐัั ะทะฝะฐัะตะฝะธะต, ะฟะพ ะบะพัะพัะพะผั ะฝะฐะดะพ ัะพััะธัะพะฒะฐัั. | # ะกะพััะธััะตะผ ะฟะพ ะพััะฐัะบั ะพั ะดะตะปะตะฝะธั ะฝะฐ 10
def mod(x):
return x % 10
a = [1, 15, 143, 8, 0, 5, 17, 48]
a.sort(key=mod)
print(a)
# ะะฑััะฝะพ ัะฟะธัะบะธ ัะพััะธัััััั ัะฝะฐัะฐะปะฐ ะฟะพ ะฟะตัะฒะพะผั ัะปะตะผะตะฝัั, ะฟะพัะพะผ ะฟะพ ะฒัะพัะพะผั ะธ ัะฐะบ ะดะฐะปะตะต
a = [[4, 3], [1, 5], [2, 15], [1, 6], [2, 9], [4, 1]]
a.sort()
print(a)
# ะ ัะฐะบ ะผะพะถะฝะพ ะพััะพััะธัะพะฒะฐัั ัะฝ... | crash-course/builtin-sort.ipynb | citxx/sis-python | mit |
Pressure Drop calculations
Collecting K-values of fittings, connections, etc... | """
""" | Archive/Analysis/pressure-reservoir-notebook.ipynb | psas/liquid-engine-test-stand | gpl-2.0 |
Template representation variant 1
bundles for each workflow step
(characterized by output, activity, and agent with relationships)
every activity uses information from a global provenance log file (used relationship)
and every activity updates parts of a global provenance log file (was generated by relationship)
... | # generate prov_template options and print provn representation
gen_bundles(workflow_dict,prov_doc01)
print(prov_doc01.get_provn())
%matplotlib inline
prov_doc01.plot()
prov_doc01.serialize('data-ingest1.rdf',format='rdf') | prov_templates/Data_ingest_use_case_templates.ipynb | stephank16/enes_graph_use_case | gpl-3.0 |
Template representation variant 2:
workflow steps without bundles
workflow steps are chained (output is input to next step) | nodes = in_bundles(workflow_dict,prov_doc2)
chain_bundles(nodes)
print(prov_doc02.get_provn())
%matplotlib inline
prov_doc02.plot()
from prov.dot import prov_to_dot
dot = prov_to_dot(prov_doc02)
prov_doc02.serialize('ingest-prov-version2.rdf',format='rdf')
dot.write_png('ingest-prov-version2.png') | prov_templates/Data_ingest_use_case_templates.ipynb | stephank16/enes_graph_use_case | gpl-3.0 |
Template representation variant 3:
workflow steps without bundles
workflow steps are chained (output is input to next step)
global workflow representation generation added | gnodes = in_bundles(workflow_dict,prov_doc3)
chain_hist_bundles(gnodes,prov_doc3)
print(prov_doc03.get_provn())
dot = prov_to_dot(prov_doc03)
dot.write_png('ingest-prov-version3.png')
%matplotlib inline
prov_doc03.plot()
prov_doc03.serialize('data-ingest3.rdf',format='rdf')
# ------------------ to be removed -------... | prov_templates/Data_ingest_use_case_templates.ipynb | stephank16/enes_graph_use_case | gpl-3.0 |
The BERT tokenizer
To fine tune a pre-trained model you need to be sure that you're using exactly the same tokenization, vocabulary, and index mapping as you used during training.
The BERT tokenizer used in this tutorial is written in pure Python (It's not built out of TensorFlow ops). So you can't just plug it into yo... | # Set up tokenizer to generate Tensorflow dataset
tokenizer = # TODO 1: Your code goes here
print("Vocab size:", len(tokenizer.vocab)) | courses/machine_learning/deepdive2/text_classification/labs/fine_tune_bert.ipynb | GoogleCloudPlatform/training-data-analyst | apache-2.0 |
Each subset of the data has been converted to a dictionary of features, and a set of labels. Each feature in the input dictionary has the same shape, and the number of labels should match: | # Print the key value and shapes
for key, value in glue_train.items():
# TODO 2: Your code goes here
print(f'glue_train_labels shape: {glue_train_labels.shape}') | courses/machine_learning/deepdive2/text_classification/labs/fine_tune_bert.ipynb | GoogleCloudPlatform/training-data-analyst | apache-2.0 |
Note: The pretrained TransformerEncoder is also available on TensorFlow Hub. See the Hub appendix for details.
Set up the optimizer
BERT adopts the Adam optimizer with weight decay (aka "AdamW").
It also employs a learning rate schedule that firstly warms up from 0 and then decays to 0. | # Set up epochs and steps
epochs = 3
batch_size = 32
eval_batch_size = 32
train_data_size = len(glue_train_labels)
steps_per_epoch = int(train_data_size / batch_size)
num_train_steps = steps_per_epoch * epochs
warmup_steps = int(epochs * train_data_size * 0.1 / batch_size)
# creates an optimizer with learning rate sc... | courses/machine_learning/deepdive2/text_classification/labs/fine_tune_bert.ipynb | GoogleCloudPlatform/training-data-analyst | apache-2.0 |
To see an example of how to customize the optimizer and it's schedule, see the Optimizer schedule appendix.
Train the model
The metric is accuracy and we use sparse categorical cross-entropy as loss. | metrics = [tf.keras.metrics.SparseCategoricalAccuracy('accuracy', dtype=tf.float32)]
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
bert_classifier.compile(
optimizer=optimizer,
loss=loss,
metrics=metrics)
# Train the model
bert_classifier.fit(# TODO 4: Your code goes here) | courses/machine_learning/deepdive2/text_classification/labs/fine_tune_bert.ipynb | GoogleCloudPlatform/training-data-analyst | apache-2.0 |
Then apply the transformation to generate new TFRecord files. | # Set up output of training and evaluation Tensorflow dataset
train_data_output_path="./mrpc_train.tf_record"
eval_data_output_path="./mrpc_eval.tf_record"
max_seq_length = 128
batch_size = 32
eval_batch_size = 32
# Generate and save training data into a tf record file
input_meta_data = (# TODO 5: Your code goes here... | courses/machine_learning/deepdive2/text_classification/labs/fine_tune_bert.ipynb | GoogleCloudPlatform/training-data-analyst | apache-2.0 |
Measuring chromatin fluorescence
Goal: we want to quantify the amount of a particular protein (red fluorescence) localized on the centromeres (green) versus the rest of the chromosome (blue).
The main challenge here is the uneven illumination, which makes isolating the chromosomes a struggle. | import numpy as np
from matplotlib import cm, pyplot as plt
import skdemo
plt.rcParams['image.cmap'] = 'cubehelix'
plt.rcParams['image.interpolation'] = 'none'
from skimage import io
image = io.imread('images/chromosomes.tif')
skdemo.imshow_with_histogram(image) | scikit_image/lectures/solutions/adv0_chromosomes.ipynb | M-R-Houghton/euroscipy_2015 | mit |
But getting the chromosomes is not so easy: | chromosomes_binary = chromosomes > threshold_otsu(chromosomes)
skdemo.imshow_all(chromosomes, chromosomes_binary) | scikit_image/lectures/solutions/adv0_chromosomes.ipynb | M-R-Houghton/euroscipy_2015 | mit |
Not only is the uneven illumination a problem, but there seem to be some artifacts due to the illumination pattern!
Exercise: Can you think of a way to fix this?
(Hint: in addition to everything you've learned so far, check out skimage.morphology.remove_small_objects) | from skimage.morphology import (opening, selem,
remove_small_objects)
d = selem.diamond(radius=4)
chr0 = opening(chromosomes_adapt, d)
chr1 = remove_small_objects(chr0.astype(bool), 256)
images = [chromosomes, chromosomes_adapt, chr0, chr1]
titles = ['original', 'adaptive threshold',
... | scikit_image/lectures/solutions/adv0_chromosomes.ipynb | M-R-Houghton/euroscipy_2015 | mit |
Now that we have the centromeres and the chromosomes, it's time to do the science: get the distribution of intensities in the red channel using both centromere and chromosome locations. | # Replace "None" below with the right expressions!
centromere_intensities = protein[centromeres_binary]
chromosome_intensities = protein[chr1]
all_intensities = np.concatenate((centromere_intensities,
chromosome_intensities))
minint = np.min(all_intensities)
maxint = np.max(all_intensi... | scikit_image/lectures/solutions/adv0_chromosomes.ipynb | M-R-Houghton/euroscipy_2015 | mit |
Restore BF Reviews and Ratings | cmd = "SELECT review_rating, review_text FROM bf_reviews"
bfdf = pd.read_sql_query(cmd, engine)
print(len(bfdf))
bfdf.head(5) | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Now limit the reviews used in training to only reviews with more than 350 characters. | bfdfl = bfdf[bfdf['review_text'].str.len() > 350].copy()
len(bfdfl) | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Create Training and Testing Data | train_data = bfdfl['review_text'].values[:750]
y_train = bfdfl['review_rating'].values[:750]
t0 = time()
vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
stop_words='english')
X_train = vectorizer.fit_transform(train_data)
duration = time() - t0
print('vectorized in {:.2f} seco... | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Now Test Several Classifiers | def benchmark(clf, pos_label=None):
print('_' * 80)
print("Training: ")
print(clf)
t0 = time()
clf.fit(X_train, y_train)
train_time = time() - t0
print("train time: %0.3fs" % train_time)
t0 = time()
pred = clf.predict(X_test)
test_time = time() - t0
print("test time: %0.3fs... | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Plot Results | indices = np.arange(len(results))
results = [[x[i] for x in results] for i in range(5)]
font = {'family' : 'normal',
'weight' : 'bold',
'size' : 16}
plt.rc('font', **font)
plt.rcParams['figure.figsize'] = 12.94, 8
clf_names, score, training_time, test_time, pred = results
training_time = np.array(t... | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Now Plot The Predicted Rating as a Function of the Given Rating for the BF Test Data | fig, ax = plt.subplots(1, 1)
ax.plot(y_test + 0.1*np.random.random(len(y_test)) - 0.05, pred[0] + 0.1*np.random.random(len(y_test)) - 0.05, '.')
ax.set_xlim([0, 6])
ax.set_ylim([0, 6])
ax.set_xlabel('Given Rating')
ax.set_ylabel('Predicted Rating')
ms = np.zeros((5, 5))
for row in range(5):
for col in range(5):
... | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
confusion matrix | from sklearn import metrics
def plot_confusion_matrix(y_pred, y, normalize=False, cmap=plt.cm.binary):
cm = metrics.confusion_matrix(y, y_pred)
cm = np.flipud(cm)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.imshow(cm, cmap=cmap, interpolation='nearest')
plt.col... | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Which features/words have the highest weight towards rating 1?
Unnormalized centroid | wgtarr = cens[4,:]
ratwords = np.argsort(wgtarr).tolist()[::-1]
for i in range(20):
print(wgtarr[ratwords[i]], words[ratwords[i]], ratwords[i])
cens[:, 1148] | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Normalized centroid
First compute the total for each feature across all ratings (1 to 5) | cen_tot = np.sum(cens, axis=0)
cen_tot.shape
wgtarr = cens[4,:]/cen_tot
words[np.argsort(wgtarr)[0]]
ratwords = np.argsort(wgtarr).tolist()[::-1]
for i in range(20):
print(wgtarr[ratwords[i]], words[ratwords[i]]) | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Expanding the Model to 3-grams | t0 = time()
vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
stop_words='english', ngram_range=(1, 3))
X_train = vectorizer.fit_transform(train_data)
duration = time() - t0
print('vectorized in {:.2f} seconds.'.format(duration))
print(X_train.shape)
t0 = time()
X_test = vectoriz... | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Expanding the Model to 2-grams | t0 = time()
vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
stop_words='english', ngram_range=(1, 2))
X_train = vectorizer.fit_transform(train_data)
duration = time() - t0
print('vectorized in {:.2f} seconds.'.format(duration))
print(X_train.shape)
t0 = time()
X_test = vectoriz... | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Conclusions
The 1-gram model worked just as well as the 3-gram model. To reduce complexity, I will therefore use the 1-gram model. Out of the models tested, the NearestCentroid performed the best, so I will use that for classification. | engine = cadb.connect_aws_db(write_unicode=True)
city = 'palo_alto'
cmd = 'select h.hotel_id, h.business_id, count(*) as count from '
cmd += 'ta_reviews r inner join ta_hotels h on r.business_id = '
cmd += 'h.business_id where h.hotel_city = "'
cmd += (' ').join(city.split('_'))+'" '
cmd += 'GROUP BY r.business_id'
c... | code/test_rating_classifiers.ipynb | mattgiguere/doglodge | mit |
Step 1: define a molecule
Here, we use LiH in sto3g basis with PySCF driver as an example.
The molecule object records the information from the PySCF driver. | # using driver to get fermionic Hamiltonian
# PySCF example
cfg_mgr = ConfigurationManager()
pyscf_cfg = OrderedDict([('atom', 'Li .0 .0 .0; H .0 .0 1.6'),
('unit', 'Angstrom'),
('charge', 0),
('spin', 0),
('basis', ... | qiskit/aqua/chemistry/advanced_howto.ipynb | antoniomezzacapo/qiskit-tutorial | apache-2.0 |
Step 2: Prepare qubit Hamiltonian
Here, we setup the to-be-frozen and to-be-removed orbitals to reduce the problem size when we mapping to qubit Hamiltonian. Furthermore, we define the mapping type for qubit Hamiltonian.
For the particular parity mapping, we can further reduce the problem size. | # please be aware that the idx here with respective to original idx
freeze_list = [0]
remove_list = [-3, -2] # negative number denotes the reverse order
map_type = 'parity'
h1 = molecule._one_body_integrals
h2 = molecule._two_body_integrals
nuclear_repulsion_energy = molecule._nuclear_repulsion_energy
num_particles =... | qiskit/aqua/chemistry/advanced_howto.ipynb | antoniomezzacapo/qiskit-tutorial | apache-2.0 |
We use the classical eigen decomposition to get the smallest eigenvalue as a reference. | # Using exact eigensolver to get the smallest eigenvalue
exact_eigensolver = get_algorithm_instance('ExactEigensolver')
exact_eigensolver.init_args(qubitOp, k=1)
ret = exact_eigensolver.run()
print('The computed energy is: {:.12f}'.format(ret['eigvals'][0].real))
print('The total ground state energy is: {:.12f}'.format... | qiskit/aqua/chemistry/advanced_howto.ipynb | antoniomezzacapo/qiskit-tutorial | apache-2.0 |
Step 3: Initiate and config dynamically-loaded instances
To run VQE with UCCSD variational form, we require
- VQE algorithm
- Classical Optimizer
- UCCSD variational form
- Prepare the initial state into HartreeFock state
[Optional] Setup token to run the experiment on a real device
If you would like to run the experie... | from qiskit import IBMQ
IBMQ.load_accounts()
# setup COBYLA optimizer
max_eval = 200
cobyla = get_optimizer_instance('COBYLA')
cobyla.set_options(maxiter=max_eval)
# setup HartreeFock state
HF_state = get_initial_state_instance('HartreeFock')
HF_state.init_args(qubitOp.num_qubits, num_spin_orbitals, map_type,
... | qiskit/aqua/chemistry/advanced_howto.ipynb | antoniomezzacapo/qiskit-tutorial | apache-2.0 |
Step 4: Run algorithm and retrieve the results
The smallest eigenvalue is stored in the first entry of the eigvals key. | results = vqe_algorithm.run()
print('The computed ground state energy is: {:.12f}'.format(results['eigvals'][0]))
print('The total ground state energy is: {:.12f}'.format(results['eigvals'][0] + energy_shift + nuclear_repulsion_energy))
print("Parameters: {}".format(results['opt_params'])) | qiskit/aqua/chemistry/advanced_howto.ipynb | antoniomezzacapo/qiskit-tutorial | apache-2.0 |
Radius of cell growing for last $t$ minutes: $1.05^t \times 10^{-3}cm$. Thus for a cell to form a detectable cluster it should grow for atleast: $\frac{log(10)}{log(1.05}=47.19$ minutes
And the total time for grow is $60$ minutes, so that MAX wait time before growth starts is $60-47.19=12.81$ minutes | delta = (1-exp(-12.81/20))
print(delta) | 2015_Fall/MATH-578B/Homework4/Homework4.ipynb | saketkc/hatex | mit |
$\delta = p(\text{wait time $\leq$ 12.81 minutes}) = 1-\exp(-12.81/20) = 0.473$
So now we need these criteria for calling a ball white:
- It gets mutated (PPP)
- It has a waiting time of <12.81 minutes
So we have a new $PPP(\rho \mu \delta)$ and
$\frac{17}{\rho \times A} = \exp(-\rho \mu \delta A) \times \frac{(\rho ... | from sympy import solve, Eq, symbols
from mpmath import log as mpl
from math import factorial
mu= symbols('mu')
lhs = log(17/100.0,e)
rhs = Eq(-rho*mu*delta*A + 17*(rho*mu*delta*A-1) -mpl(factorial(17)))
s = solve(rhs, mu)
print (s) | 2015_Fall/MATH-578B/Homework4/Homework4.ipynb | saketkc/hatex | mit |
Thus, approximated $\mu = 0.0667$ for the given dataset!
Problem 2 | %pylab inline
import matplotlib.pyplot as plt
N = 10**6
N_t = 10**6
mu = 10**-6
s = 0.001
generations = 8000
mu_t = N_t*mu
from scipy.stats import poisson, binom
import numpy as np
def run_generations(distribution):
mutations = []
all_mutations = []
for t in range(generations):
# of mutations in ... | 2015_Fall/MATH-578B/Homework4/Homework4.ipynb | saketkc/hatex | mit |
Poisson | pylab.rcParams['figure.figsize'] = (16.0, 12.0)
all_mutations = run_generations('poisson')
plt.plot(range(1,generations+1),[np.mean(x) for x in all_mutations])
plt.title('Average distinct mutation per generations')
plt.plot(range(1,generations+1),[np.sum(x) for x in all_mutations])
plt.title('Total mutation per genera... | 2015_Fall/MATH-578B/Homework4/Homework4.ipynb | saketkc/hatex | mit |
Poisson Results | mu = 10**-6
s = 0.001
N= 10**6
theoretical_tot_mut = mu*N/s
print(theoretical_tot_mut)
print ('Average total mutations per generation: {}'.format(np.mean([np.sum(x) for x in all_mutations])))
print ('Average distinct mutations per generation: {}'.format(np.mean([len(x) for x in all_mutations])))
print ('Theoretical to... | 2015_Fall/MATH-578B/Homework4/Homework4.ipynb | saketkc/hatex | mit |
Binomial | pylab.rcParams['figure.figsize'] = (16.0, 12.0)
all_mutations = run_generations('binomial')
plt.plot(range(1,generations+1),[np.mean(x) for x in all_mutations])
plt.title('Average distinct mutation per generations')
plt.plot(range(1,generations+1),[np.sum(x) for x in all_mutations])
plt.title('Total mutation per gener... | 2015_Fall/MATH-578B/Homework4/Homework4.ipynb | saketkc/hatex | mit |
Binomial results | mu = 10**-6
s=0.001
N= 10**6
theoretical_tot_mut = mu*N/s
print(theoretical_tot_mut)
print ('Average total mutations per generation: {}'.format(np.mean([np.sum(x) for x in all_mutations])))
print ('Average distinct mutations per generation: {}'.format(np.mean([len(x) for x in all_mutations])))
print ('Theoretical tota... | 2015_Fall/MATH-578B/Homework4/Homework4.ipynb | saketkc/hatex | mit |
Using if-elif for discrete classification
This problem will use elif statements, the useful sibling of if and else, it basically extends your if statements to test multiple situations.
In our current situation, where each star will have exactly one spectral type, elif will really come through to make our if statements ... | # Fill in the parentheses. Don't forget indentation!
n = random_number(50,250) # this should be given!
if ( n <= 100 ):
print(n,'is less than or equal to 100.')
elif (100 < n <= 150):
print(n,'is between 100 and 150.')
else:
print(n, 'is greater than or equal to 150.') | notebooks/Lectures2021/Lecture1/L1_challenge_problem_stars_instructor.ipynb | astroumd/GradMap | gpl-3.0 |
Test your statement a few times so that you see if it works for various numbers.
Every time you run the cell, a new random number will be chosen, but you can also set it to make sure that the code works correctly.
Just comment (#) before the random_number() function.
Make sure to also test the boundary numbers, as they... | i = 0
end = len(temp)
# Define your loop here
while i < end:
if temp[i] < 3700:
print('Star',temp[i],'K is type', 'M')
elif 3700 <= temp[i] < 5200:
print('Star',temp[i],'K is type', 'K')
elif 5200 <= temp[i] < 6000:
print('Star',temp[i],'K is type', 'G')
elif 6000 <= temp[i] < 7... | notebooks/Lectures2021/Lecture1/L1_challenge_problem_stars_instructor.ipynb | astroumd/GradMap | gpl-3.0 |
Scrape data
The file wowwiki_pages_current.xml is a database dump of the WoW Wikia, containing the current version of every page on the Wikia. This amounts to ~500 MB of uncompressed text! To process this, we will use the lxml library. A typical page we are interested in looks something like this:
```xml
<page>
<titl... | MAXPRINT = 100
MAXPROCESS = 1e7
numprocessed = 0
names = []
levels = []
healths = []
itertree = etree.iterparse('wowwiki_pages_current.xml')
for event, element in itertree:
if numprocessed > MAXPROCESS:
raise Exception('Maximum number of records processed')
break
numprocessed += 1
# if... | WoW_NPC_analysis.ipynb | SnoopJeDi/WoWNPCs | mit |
Data cleaning and exploratory analysis
Now we have a set of lists (names, levels, healths) that contain information about every NPC we were able to find. We can look at this data using the numpy library to find out what the data look like, using the techniques of exploratory data analysis. | # convert the lists we've built into numpy arrays. `None` entries will be mapped to NaNs
names = np.array(names)
lvls = np.array(levels, dtype=np.float64)
hps = np.array(healths, dtype=np.float64)
num_NPCs = len(names)
min_lvl, max_lvl = lvls.min(), lvls.max()
min_hp, max_hp = hps.min(), hps.max()
print(
"Number ... | WoW_NPC_analysis.ipynb | SnoopJeDi/WoWNPCs | mit |
We can see we've successfully extracted over 12,000 NPCs, but there are some NaNs in the levels! Let's look at these... | names[np.isnan(lvls)][:5] | WoW_NPC_analysis.ipynb | SnoopJeDi/WoWNPCs | mit |
Looking at the page for Vol'jin, we see that his level is encoded on the page as ?? Boss, which can't be converted to an integer. World of Warcraft has a lot of these, and for a more detailed analysis, we could isolate these entries by assigning them a special value during parsing, but in this first pass at the data, w... | idx = np.isfinite(lvls)
num_NPCs = len(names[idx])
min_lvl, max_lvl = lvls[idx].min(), lvls[idx].max()
min_hp, max_hp = hps[idx].min(), hps[idx].max()
print(
"Number of entries: {}\n"
"Min/max level: {}, {}\n"
"Min/max health: {}, {}".format(num_NPCs, min_lvl, max_lvl, min_hp, max_hp)
) | WoW_NPC_analysis.ipynb | SnoopJeDi/WoWNPCs | mit |
Ah, there we go! Knowing that the maximum player level (as of this writing) in WoW is 100, we can surmise that there are still some NPCs in this dataset that represent very-high-level bosses/etc., which may skew the statistics of hitpoints. We can set our cutoff at this point to only consider NPCs that are directly com... | LEVEL_CUTOFF = 100
hps = np.log10(healths)
lvls = np.array(levels)
idx = np.isfinite(lvls)
lvls = lvls[idx]
hps = hps[idx]
print("Number of NPCs with level > 100: %d" % (lvls > 100).sum())
idx = (lvls <= LEVEL_CUTOFF)
print("Number of NPCs with finite level < 100: %d\n" % (idx.sum()))
lvls = lvls[idx]
hps = hps[id... | WoW_NPC_analysis.ipynb | SnoopJeDi/WoWNPCs | mit |
Visualizing the data
We could continue to explore these data using text printouts of statistical information (mean, median, etc.), but with a dataset this large, visualization becomes a very powerful tool. We will use the matplotlib library (and the seaborn library that wraps it) to generate a hexplot (and marginal dis... | ax = sns.jointplot(lvls, hps, stat_func=None, kind='hex',
xlim=(0, LEVEL_CUTOFF), bins='log', color='r', cmap='inferno_r'
)
ax.fig.suptitle('HP vs Lvl of WoW NPCS (N={N})'.format(N=lvls.size), y=1.02)
ax.ax_joint.set_xlabel('NPC level')
ax.ax_joint.set_ylabel(r'$log_{10}(\mathrm... | WoW_NPC_analysis.ipynb | SnoopJeDi/WoWNPCs | mit |
In this notebook, we introduce some basic uses of the agavepy Python library for interacting with the Agave Platform science-as-a-service APIs. The examples primarily draw from the apps service, but the concepts introduced are broadly applicable to all Agave services. In subsequent notebooks, we'll take deeper dives in... | from agavepy.agave import Agave
import json | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
Before we can interact with Agave, we need to instantiate a client. Typically, we would use the constructor and pass in our credentials (OAuth client key and secret as well as our username and password) together with configuration data for our "tenant", the organization within Agave we wish to interact with. | agave_cache_dir = os.environ.get('AGAVE_CACHE_DIR')
ag_token_cache = json.loads(open(agave_cache_dir + '/current').read())
print (ag_token_cache)
AGAVE_APP_NAME="funwave-tvd-nectar" + os.environ['AGAVE_USERNAME']
ag = Agave(token=ag_token_cache['access_token'], refresh_token=ag_token_cache['refresh_token'], api_key=a... | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
The agavepy library's Agave class also provides a restore() method for reconstituting previous OAuth sessions. Previous sessions are read from and written to a cache file, /etc/.agpy, so that OAuth sessions persist across iPython sessions. When you authenticated to JupyterHub, the OAuth login was written to the .agpy f... | ag.token.token_info
ag.token.refresh()
ag.token.token_info | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
This shows us both the access and refresh tokens being used. We can also see the end user profile associated with these tokens: | ag.profiles.get() | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
Finally, we can inspect the ag object directly for attributes like api_key, api_secret, api_server, etc. | print ag.api_key, ag.api_secret, ag.api_server | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
We are now ready to interact with Agave services using our agavepy client. We can take a quick look at the available top-level methods of our client: | dir(ag) | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
We see there is a top-level method for each of the core science APIs in agave. We will focus on the apps service since it is of broad interest, but much of what we illustrate is generally applicable to all Agave core science APIs.
We can browse a specific collection using the list() method. For example, let's see what... | ag.apps.list() | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
What we see in the output above is a python list representing the JSON object returned from Agave's apps service. It is a list of objects, each of which representing a single app. Let's capture the first app object and inspect it. To do that we can use normal Python list notation: | app = ag.apps.list()[0]
print type(app); app | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
We see that the app object is of type agavepy.agave.AttrDict. That's a Python dictionary with some customizations to provide convenience features such as using dot notation for keys/attributes. For example, we see that the app object has an 'id' key. We can access it directly using dot notation: | app.id | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
Equivalently, we can use normal Python dictionary syntax: | app['id'] | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
In Agave, the app id is the unique identifier for the application. We'll come back to that in a minute. For now, just know that this example is very typical of responses from agavepy: in general the JSON response object is represented by lists of AttrDicts.
Stepping back for a second, let's explore the apps collection ... | dir(ag.apps) | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
Also notice that we have tab-completion on these operations. So, if we start typing "ag.apps.l" and then hit tab, Jupyter provides a select box of operations beginning with "l". Try putting the following cell in focus and then hitting the tab key (but don't actually hit enter or try to execute the cell; otherwise you'l... | ag.apps.l | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
If we would like to get details about a specific object for which we know the unique id, in general we use the get method, passing in the id for the object. Here, we will use an app id we found from the ag.apps.list command. | ag.apps.get(appId=app.id) | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
Whoa, that's a lot of information. We aren't going to give a comprehensive introduction to Agave applications in this notebook. Instead we refer you to the official Agave app tutorial on the website: http://agaveapi.co/documentation/tutorials/app-management-tutorial/
However, we will point out a couple of important poi... | full_app = ag.apps.get(appId=app.id) | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
Complex sub-objects of the application such as application inputs and parameters come back as top level attributes. and are represented as lists. The individual elements of the list are represented as AttrDicts. We can see this by exploring our full_app's inputs: | print type(full_app.inputs); print type(full_app.inputs[0]); full_app.inputs[0] | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
Then, if we want the input id, we can use dot notation or dictionary notation just as before: | full_app.inputs[0].id | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
You now have the ability to fully explore individual Agave objects returned from agavepy, but what about searching for objects? The Agave platform provides a powerful search feature across most services, and agavepy supports that as well.
Every retrieval operation in agavepy (for example, apps.list) supports a "search... | ag.apps.list(search={'id': app.id}) | content/notebooks/Python SDK.ipynb | agaveapi/SC17-container-tutorial | bsd-3-clause |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.