markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
set from dictionary
Works on dicts too. But will return a set of keys only, not values. | # works on dicts too
s3_repeat_values = set({'k1':'v1',
'k2':'v1',
'k3':'v2'})
s3_repeat_values
type(s3_repeat_values)
# repeating keys
s3_repeat_keys = set({'k1':'v1',
'k1':'v2'})
s3_repeat_keys | python_crash_course/python_cheat_sheet_1.ipynb | AtmaMani/pyChakras | mit |
Note. When you create a dict with duplicate keys, Python just keeps the last occurrence of the kvp. It thinks the kvp needs to be updated to the latest value | d80 = {'k1':'v1', 'k2':'v2', 'k1':'v45'} # k1 is repeated
d80 | python_crash_course/python_cheat_sheet_1.ipynb | AtmaMani/pyChakras | mit |
Loops
for loop | list1 = [1,2,3,4,5,6,7]
for element in list1:
print(element)
for element in list1:
print(element, " squared ", element*element)
for count in range(11,20):
print(str(count)) | python_crash_course/python_cheat_sheet_1.ipynb | AtmaMani/pyChakras | mit |
Comprehensions
Comprehensions are an effective way to loop through sequences
List comprehension
[operation for index in sequence condition] | list2_comp = [e*e for e in list1]
list2_comp
list2_even = [e*e for e in list1 if e%2==0]
list2_even | python_crash_course/python_cheat_sheet_1.ipynb | AtmaMani/pyChakras | mit |
Dictionary comprehension
Same as list comprehension, but instead of lists, it returns a dictionary. You use {} instead of []
{key:value for key, value in dictionary if condition} | d3 = {'day':'Thursday',
'day_of_week':5,
'start_of_week':'Sunday',
'day_of_year':123,
'dod':{'month_of_year':'Feb',
'year':2017},
'list1':[8,7,66]}
# get those kvp whose value is a list
{k:v for k,v in d3.items() if type(v)==list} | python_crash_course/python_cheat_sheet_1.ipynb | AtmaMani/pyChakras | mit |
reverse keys and values? - works only when values are immutable types. hence filter them out | d4 = {k:v for k,v in d3.items() if type(v) not in [list, dict]}
d4
# reverse keys and values
d4_reverse = {v:k for k,v in d4.items()}
d4_reverse | python_crash_course/python_cheat_sheet_1.ipynb | AtmaMani/pyChakras | mit |
What is Numba?
Numba is a just-in-time, type-specializing, function compiler for accelerating numerically-focused Python. That's a long list, so let's break down those terms:
function compiler: Numba compiles Python functions, not entire applications, and not parts of functions. Numba does not replace your Python inte... | def add(x, y):
return x + y # add code here | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Now, test the function, first with two scalar integers: | add(1, 2) # add code here | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
1b) With Numpy, we can use our function to add not just scalars, but vectors as well. Using your favorite array creation routine, create two integer arrays with ten elements each, called a and b, and use your add function to add them. | a = np.arange(0,10) # add code here
b = np.arange(1,11) # add code here
add(a, b) # add code here | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
More commonly, you will use jit as a decorator, by adding @jit to the line above your function definition, but the above version shows you that at heart, @jit is just a python function that takes other functions as its argument!
1c) By default, a Numba function saves the original python version of the function in the ... | print(add(a,b)) #add code here
print(numba_add(a,b)) #add code here
print(numba_add.py_func(a,b)) #add code here | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
What's going on here? %timeit is running our function many times, and then reporting the average time it takes to run. This is generally a better approach than timing a single function execution, because it accounts for random events that may cause any given run to perform poorly.
1d) Compare the time it takes to run y... | %timeit add(1,2) # add code here
%timeit add(a, b) # add code here
%timeit a + b # add code here | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
So, scalars are faster than arrays (makes sense), and python's addition function is better than ours (seems reasonable). Now, let's see how fast our pre-compiled Numba addition function is. | %timeit numba_add(a,b) # add code here | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Hold on - our new pre-compiled function is running even slower than the original python version! What's going on here?
Problem 2 - A Better Numba Function
(This problem borrowed from seibert's 2018 gtc numba tutorial.)
As we saw in the first example, Numba isn't going to speed up everything. Generally, Numba will help ... | @jit # add code here
def hypotenuse(x, y):
x = abs(x);
y = abs(y);
t = min(x, y);
x = max(x, y);
t = t / x;
return x * math.sqrt(1+t*t) | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
2b) Use the %timeit function to determine whether the Numba version of the hyptonenuse function is better than the original Python implementation. | %timeit hypotenuse(3,4) # add code here
%timeit hypotenuse.py_func(3,4) # add code here | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
2c) Numba functions can call other functions, provided they are also Numba functions. Below is a function that loops through two numpy arrays and puts their sum into an output array. Modify the following function to calculate the hypotenuse instead. | @njit # this is an alias for @jit(nopython=True)
def ex_func(x, y, out):
for i in range(x.shape[0]):
out[i] = hypotenuse(x[i], y[i]) # change this line
in1 = np.arange(10, dtype=np.float64)
in2 = 2 * in1 + 1
out = np.empty_like(in1)
print('in1:', in1)
print('in2:', in2)
ex_func(in1, in2, out)
print('out:', ou... | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Problem 3 - Fun with Fractals
Now that we've got the basics of the Numba jit decorator down, let's have a little fun. A classic example problem in parallel programming is the calculation of a fractal, because a large fraction of the work can be done in parallel. Below is some code that calculates whether a number is a ... | @njit # add code here
def julia(x, y, max_iters):
"""
Given the real and imaginary parts of a complex number,
determine if it is a candidate for membership in the Julia
set given a fixed number of iterations.
"""
i = 0
c = complex(-0.8, 0.156)
a = complex(x,y)
for i in range(max_iter... | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
3b) There is more than one type of fractal in the world, however! Below is a function that determines membership in the Mandelbrot set. Modify the function using to take advantage of Numba, then modify the code above to produce a new pretty picture. | @njit #add code here
def mandel(x, y, max_iters):
"""
Given the real and imaginary parts of a complex number,
determine if it is a candidate for membership in the Mandelbrot
set given a fixed number of iterations.
"""
i = 0
c = complex(x,y)
z = 0.0j
for i in range(max_iters):
... | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
4a) Numba has inferred the types for this function based on how we've used it. Try out your numba_add function with two floating point numbers, then re-inspect the types of the Numba function. Are they the same? | #Add code here
numba_add(3.,4.)
numba_add.inspect_types() | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
4b) Try your ufunc out with a new target, 'parallel'. How does the speed compare? What if the array size is much larger? | big_array = np.arange(0,1000000) # add code here
%timeit add_ufunc(big_array, big_array) # add code here
@vectorize(['int64(int64, int64)'], target='parallel')
def add_ufunc(x, y):
return x + y
%timeit add_ufunc(big_array,big_array) | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
5a) Run the direct summation code and determine how long it takes with 10, 100, 1000 particles. Is there a relationship? | %timeit direct_sum(particles) | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Because this is an $\mathcal{O} \left(n^2 \right)$ function, each addition of 10x particles costs 100x more in computational time. Given how expensive it is, let's see if we can improve things a bit with Numba. (This also happens to be a highly parallelizable problem, but we'll get to that tomorrow.)
How do we use Numb... | particle_dtype = np.dtype({'names':['x','y','z','m','phi'],
'formats':[np.double,
np.double,
np.double,
np.double,
np.double]}... | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
5b) Write a jit function create_n_random_particles that takes the arguments n (number of particles), m (mass of every particle) and a domain within which to generate a random number (as in the class above).
It should create an array with n elements and dtype=particle_dtype and then return that array.
For each particl... | @njit
def create_n_random_particles(n, m, domain=1):
'''
Creates `n` particles with mass `m` with random coordinates
between 0 and `domain`
'''
parts = np.zeros((n), dtype=particle_dtype)
#attribute access only in @jitted function
for p in parts:
p.x = np.random.random() * domain
... | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
We don't have a distance method anymore, so we need to write a function to take care of that.
5c) Write a jit function distance to calculate the distance between two particles of dtype particle_dtype. | @njit
def distance(part1, part2):
'''calculate the distance between two particles'''
return ((part1.x - part2.x)**2 +
(part1.y - part2.y)**2 +
(part1.z - part2.z)**2)**.5
distance(particles[0], particles[1])
%%timeit
distance(particles[0], particles[1]) | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
5d) Modify the direct_sum function above to instead work a NumPy array of particles. Loop over each element in the array and calculate its total potential. Time the result and compare it to your previous version of this function. | @njit
def direct_sum(particles):
for i, target in enumerate(particles):
for j, source in enumerate(particles):
if i != j:
r = distance(target, source)
target.phi += source.m / r
return particles
%timeit direct_sum(particles) | Sessions/Session08/Day4/numba_intro_solutions.ipynb | LSSTC-DSFP/LSSTC-DSFP-Sessions | mit |
Load data
First we will load a sample ECoG dataset which we'll use for generating
a 2D snapshot. | mat = loadmat(path_data)
ch_names = mat['ch_names'].tolist()
elec = mat['elec'] # electrode coordinates in meters
# Now we make a montage stating that the sEEG contacts are in head
# coordinate system (although they are in MRI). This is compensated
# by the fact that below we do not specicty a trans file so the Head<-... | 0.23/_downloads/00ac060e49528fd74fda09b97366af98/3d_to_2d.ipynb | mne-tools/mne-tools.github.io | bsd-3-clause |
Reading simulation data | def load_data(filenames, preselection=None):
# not setting treename, it's detected automatically
data = root_numpy.root2array(filenames, selection=preselection)
return pandas.DataFrame(data)
sim_data = load_data(folder + 'PhaseSpaceSimulation.root', preselection=None) | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Looking at data, taking first rows: | sim_data.head() | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Plotting some feature | # hist data will contain all information from histogram
hist_data = hist(sim_data.H1_PX, bins=40, range=[-100000, 100000]) | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Adding interesting features
for each particle we compute it's P, PT and energy (under assumption this is Kaon) | def add_momenta_and_energy(dataframe, prefix, compute_energy=False):
"""Adding P, PT and У of particle with given prefix, say, 'H1_' """
pt_squared = dataframe[prefix + 'PX'] ** 2. + dataframe[prefix + 'PY'] ** 2.
dataframe[prefix + 'PT'] = numpy.sqrt(pt_squared)
p_squared = pt_squared + dataframe[prefi... | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Adding features of $B$
We are able to compute 4-momentum of B, given 4-momenta of produced particles | def add_B_features(data):
for axis in ['PX', 'PY', 'PZ', 'E']:
data['B_' + axis] = data['H1_' + axis] + data['H2_' + axis] + data['H3_' + axis]
add_momenta_and_energy(data, prefix='B_', compute_energy=False)
data['B_M'] = data.eval('(B_E ** 2 - B_PX ** 2 - B_PY ** 2 - B_PZ ** 2) ** 0.5')
add_B_feat... | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
looking at result (with added features) | sim_data.head()
_ = hist(sim_data['B_M'], range=[5260, 5280], bins=100) | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Dalitz plot
computing dalitz variables and checking that no resonances in simulation | def add_dalitz_variables(data):
"""function to add Dalitz variables, names of prudicts are H1, H2, H3"""
for i, j in [(1, 2), (1, 3), (2, 3)]:
momentum = pandas.DataFrame()
for axis in ['E', 'PX', 'PY', 'PZ']:
momentum[axis] = data['H{}_{}'.format(i, axis)] + data['H{}_{}'.format(j, ... | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Working with real data
Preselection | preselection = """
H1_IPChi2 > 1 && H2_IPChi2 > 1 && H3_IPChi2 > 1
&& H1_IPChi2 + H2_IPChi2 + H3_IPChi2 > 500
&& B_VertexChi2 < 12
&& H1_ProbPi < 0.5 && H2_ProbPi < 0.5 && H3_ProbPi < 0.5
&& H1_ProbK > 0.9 && H2_ProbK > 0.9 && H3_ProbK > 0.9
&& !H1_isMuon
&& !H2_isMuon
&& !H3_isMuon
"""
preselection = p... | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
adding features | for prefix in ['H1_', 'H2_', 'H3_']:
# setting Kaon mass:
real_data[prefix + 'M'] = 493
add_momenta_and_energy(real_data, prefix, compute_energy=True)
add_B_features(real_data)
_ = hist(real_data.B_M, bins=50) | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
additional preselection
which uses added features | momentum_preselection = """
(H1_PT > 100) && (H2_PT > 100) && (H3_PT > 100)
&& (H1_PT + H2_PT + H3_PT > 4500)
&& H1_P > 1500 && H2_P > 1500 && H3_P > 1500
&& B_M > 5050 && B_M < 6300
"""
momentum_preselection = momentum_preselection.replace('\n', '').replace('&&', '&')
real_data = real_data.query(momentum_presele... | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Adding Dalitz plot for real data | add_dalitz_variables(real_data)
# check that 2nd and 3rd particle have same sign
numpy.mean(real_data.H2_Charge * real_data.H3_Charge)
scatter(real_data['M_12'], real_data['M_13'], alpha=0.1)
xlabel('M_12'), ylabel('M_13')
show()
# lazy way for plots
real_data.plot('M_12', 'M_13', kind='scatter', alpha=0.1) | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Ordering dalitz variables
let's reorder particles so the first Dalitz variable is always greater | scatter(numpy.maximum(real_data['M_12'], real_data['M_13']),
numpy.minimum(real_data['M_12'], real_data['M_13']),
alpha=0.1)
xlabel('max(M12, M13)'), ylabel('min(M12, M13)')
show() | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Binned dalitz plot
let's plot the same in bins, as physicists like | hist2d(numpy.maximum(real_data['M_12'], real_data['M_13']),
numpy.minimum(real_data['M_12'], real_data['M_13']),
bins=8)
colorbar()
xlabel('max(M12, M13)'), ylabel('min(M12, M13)')
show() | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Looking at local CP-asimmetry
adding one more column | real_data['B_Charge'] = real_data.H1_Charge + real_data.H2_Charge + real_data.H3_Charge
hist(real_data.B_M[real_data.B_Charge == +1].values, bins=30, range=[5050, 5500], alpha=0.5)
hist(real_data.B_M[real_data.B_Charge == -1].values, bins=30, range=[5050, 5500], alpha=0.5)
pass | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Leaving only signal region in mass | signal_charge = real_data.query('B_M > 5200 & B_M < 5320').B_Charge | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
counting number of positively and negatively charged B particles | n_plus = numpy.sum(signal_charge == +1)
n_minus = numpy.sum(signal_charge == -1)
print n_plus, n_minus, n_plus - n_minus
print 'asymmetry = ', (n_plus - n_minus) / float(n_plus + n_minus) | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Estimating significance of deviation (approximately)
we will assume that $N_{+} + N_{-}$, and under null hypothesis each observation contains with $p=0.5$ positive or negative particle.
So, under these assumptions $N_{+}$ is distributed as binomial random variable. | # computing properties of n_plus according to H_0 hypothesis.
n_mean = len(signal_charge) * 0.5
n_std = numpy.sqrt(len(signal_charge) * 0.25)
print 'significance = ', (n_plus - n_mean) / n_std | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Subtracting background
using RooFit to fit mixture of exponential (bkg) and gaussian (signal) distributions.
Based on the fit, we estimate number of events in mass region | # Lots of ROOT imports for fitting and plotting
from rootpy import asrootpy, log
from rootpy.plotting import Hist, Canvas, set_style, get_style
from ROOT import (RooFit, RooRealVar, RooDataHist, RooArgList, RooArgSet,
RooAddPdf, TLatex, RooGaussian, RooExponential )
def compute_n_signal_by_fitting(da... | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Computing asymmetry with subtracted background | print n_positive_signal, n_negative_signal
print (n_positive_signal - n_negative_signal) / (n_positive_signal + n_negative_signal)
n_mean = 0.5 * (n_positive_signal + n_negative_signal)
n_std = numpy.sqrt(0.25 * (n_positive_signal + n_negative_signal))
print (n_positive_signal - n_mean) / n_std | ManchesterTutorial.ipynb | yandexdataschool/manchester-cp-asymmetry-tutorial | cc0-1.0 |
Read the Dakota tabular data file. | dat_file = '../examples/1-rosenbrock/dakota.dat'
data = numpy.loadtxt(dat_file, skiprows=1, unpack=True, usecols=[0,2,3,4])
data | notebooks/1-rosenbrock.ipynb | mdpiper/dakota-tutorial | mit |
Plot the path taken in the vector parameter study. | plot(data[1,], data[2,], 'ro')
xlim((-2, 2))
ylim((-2, 2))
xlabel('$x_1$')
ylabel('$x_2$')
title('Planview of parameter study locations') | notebooks/1-rosenbrock.ipynb | mdpiper/dakota-tutorial | mit |
Plot the values of the Rosenbrock function at the study locations. | plot(data[-1,], 'bo')
xlabel('index')
ylabel('Rosenbrock fuction value')
title('Rosenbrock function values at study locations') | notebooks/1-rosenbrock.ipynb | mdpiper/dakota-tutorial | mit |
What's the minimum value of the function over the study locations? | min(data[-1,:]) | notebooks/1-rosenbrock.ipynb | mdpiper/dakota-tutorial | mit |
Nomenclature
Python has a specific nomenclature for enums.
The class Color is an enumeration (or enum)
The attributes Color.red, Color.green, etc., are enumeration members (or enum members).
The enum members have names and values (the name of Color.red is red, the value of Color.blue is 3, etc.)
Printing and Representi... | print(MyEnum.first)
print(repr(MyEnum.first)) | Enums.ipynb | JohnCrickett/PythonExamples | mit |
The type of an enumeration member is the enumeration it belongs to: | type(MyEnum.first) | Enums.ipynb | JohnCrickett/PythonExamples | mit |
Alternative way to create an Enum
There is an alternative way to create and Enum, that matches Python's NamedTuple:
python
Colour = Enum('Colour', 'red, green')
Try it below: | SecondEnum = Enum('SecondEnum', 'first, second, third')
print(SecondEnum.first) | Enums.ipynb | JohnCrickett/PythonExamples | mit |
Let's declare a function with arguments | def eggs(arg1): # Functions arguments are declared inside the parentheses
print "eggs", arg1
eggs("eggssss") # Function calls specify arguments inside parentheses
def func(arg1, arg2, arg3): # There is no limit of arguments
print "func", arg1, arg2, arg3
func("spam", "eggs", "fooo")
print f... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Let's declare a function with arguments and default values | def my_pow(arg1, arg2=2): # It is possible to define deault values for the arguments, always after arguments without default values
return arg1 ** arg2
print my_pow(3)
def my_func(arg1, arg2=2, arg3=3, arg4=4):
return arg1 ** arg2 + arg3 ** arg4
print my_func(3, arg3=2) # Use keyword arguments to call skip... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Let's use an arbitrary arguments list | def my_func(arg1=1, arg2=2, *args): # This arbitrary list is a (kind-off) tuple of positional arguments
print args
return arg1 + arg2
print my_func(2, 3)
print my_func(2, 3, 5, 7)
spam = (5, 7)
print my_func(2, 3, *spam) # It is possible to unpack a tuple or list as an arbitrary list of arguments | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
The same applies for arbitrary keyword arguments | def my_func(arg1=1, arg2=2, **kwargs): # This arbitrary 'args' list is a (kind-off) tuple of positional arguments
print kwargs
return arg1 + arg2
print my_func(2, 3)
print my_func(2, 3, param3=5, param4=7)
spam = {"param3": 5, "param4": 7}
print my_func(2, 3, **spam) # It is possible to unpack a tuple or l... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Functions are first classed objects | def function_caller(f):
f()
def func_as_arg():
print 'There should be one-- and preferably only one --obvious way to do it.'
function_caller(func_as_arg) # Functions can be passed as arguments | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
REMEMBER:
Functions are declared with the 'def' keyword, its name, parrentheses and a colon
Specify arguments inside the parentheses
Define arguments' default values with an equal, after arguments without def val
Specify arbitrary arguments or keyword arguments with args or *kwargs
Actually only the asterisks matter, ... | class Spam: # 'class' keyword, camel case class name and colon :
pass
spammer = Spam() # Class instantiation: spammer becomes an instance of Spam
print spammer
class Eggs(Spam): # Ancestor superclasses inside parentheses for inheritance
a_class_attr = "class_val" # Class... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Still easy? | egger = Eggs(12.345) # Provide __init__ arguments in the instantiation
print egger
print egger.attr # Retrieve instance attributes with a dot
print egger.a_class_attr # Retrieve class attributes with a dot
print Eggs.a_class_attr
egger.a_class_attr = "new val... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Class attributes can be retrieved directly from the class
Instances only modify class attributes value locally | print Eggs | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Classes are objects too:
Python evaluates its declaration and instantiates a special object
This object is called each time a new class instance is created | egger.method("value1", "value2")
egger.second_method()
print egger.method
print Eggs.method
inst_method = egger.method
inst_method("valueA", "valueB") | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Methods are also attributes (bounded) of classes and instances
Time to talk about new-style classes | class Spam:
def spam_method(self):
print self.__class__ # __class__ is a special attribute containing the class of any object
print type(self)
spammer = Spam()
spammer.spam_method()
print spammer
print type(spammer)
# Why type says it is an 'instance' and not a 'Spam'?
class Spam(object): ... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
New-style classes were introduced in Python 2.2 to unify classes and types
Provide unified object model with a full meta-model (more in the Advanced block)
Other benefits: subclass most built-in types, descriptors (slots, properties, static and class methods)...
By default all classes are old-style until Python 3
In ... | class OldStyleClass():
pass
old_inst = OldStyleClass()
print type(old_inst)
# Let's inherit from an old-style class
class NewStyleSubClass(OldStyleClass, object): # Multiple inheritance
pass
new_inst = NewStyleSubClass()
print type(new_inst) | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Inherit from both old-style classes and 'object' to obtain new-style classes
Let's play a bit with inheritance | class Spam(object):
spam_class_attr = "spam" # Class attributes must have value always (you may use None...)
def spam_method(self):
print "spam_method", self, self.spam_class_attr
print self.__class__
class Eggs(object):
eggs_class_attr = "eggs"
def eggs_m... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
What about static or class methods? | class Spam(object):
def method(self, arg=None):
print "Called 'method' with", self, arg
@classmethod # This is a decorator
def cls_method(cls, arg=None):
print "Called 'cls_method' with", cls, arg
@staticmethod # This... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
REMEMBER:
Classes are declared with the 'class' keyword, its name in camel case and a colon
Specify ancestors superclasses list between parrentheses after the class name
So you must inherit ALWAYS from 'object' to have new-style classes
Use indentation for class body declarations (attributes and methods)
Specify cla... | print "'__name__' value:", __name__ | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
The file name is the module name with the suffix .py appended.
Global variable 'name' contains the name of current module.
Functions and classes also have a variable containing their module name | def func():
print "Called func in", __name__
print "'func.__module__' value:", func.__module__
!cat my_modules.py
!python my_modules.py
import my_modules | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
The module name depends on how the module is being evaluated (imported or executed)
Use if name == "main": to detect when a module (script) is imported or executed | # What will it happen if we import the module again?
import my_modules
### All code is evaluated (executed) only once the first time it is imported
func()
my_modules.func()
from my_modules import func
func()
func()
!rm -rf basic_tmp
!mkdir basic_tmp
!echo 'print "This is the __init__.py", __name__\n' > basic_tmp/_... | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Packages are folders with a init.py file
This init.py is also evaluated, so it may contain code
The init.py is actually the package (check its module name)
The module name depends on the packages path | !python -c "from basic_tmp.my_modules import func;func();print my_modules"
!python -c "from basic_tmp.my_modules import func as the_module;the_module();print the_module.__name__" | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
LESSONS LEARNT:
- Modules are objects too, and their variables, functions and classes are their attributes
- Modules can be imported in different ways:
- import packages.path.to.module.module_name
- from packages.path.to.module import module_name_1, module_name_2
- from packages.path.to.module import (mod... | !rm -rf basic_tmp
!mkdir basic_tmp
!echo 'print "This is the __init__.py", __name__\n' > basic_tmp/__init__.py
!cp my_modules.py basic_tmp
!echo 'from my_modules import func\n' > basic_tmp/__init__.py
!python -c "from basic_tmp import func;func()" | basic/4_Functions_classes_and_modules.ipynb | ealogar/curso-python | apache-2.0 |
Statistical Analysis and Data Exploration
In this first section of the project, you will quickly investigate a few basic statistics about the dataset you are working with. In addition, you'll look at the client's feature set in CLIENT_FEATURES and see how this particular sample relates to the features of the dataset. F... | # Number of houses in the dataset
total_houses = housing_features.shape[0]
# Number of features in the dataset
total_features = housing_features.shape[1]
# Minimum housing value in the dataset
minimum_price = housing_prices.min()
# Maximum housing value in the dataset
maximum_price = housing_prices.max()
# Mean hou... | machine_learning/udacity/project_1/boston_housing_exploration.ipynb | necromuralist/machine_learning_studies | mit |
Question 1
As a reminder, you can view a description of the Boston Housing dataset here, where you can find the different features under Attribute Information. The MEDV attribute relates to the values stored in our housing_prices variable, so we do not consider that a feature of the data.
Of the features available for ... | seaborn.set_style('whitegrid', {'figure.figsize': (10, 8)})
with warnings.catch_warnings():
warnings.simplefilter('ignore')
for column in housing_data.columns:
grid = seaborn.lmplot(column, 'median_value', data=housing_data, size=8)
axe = grid.fig.gca()
title = axe.set_title('{0} vs pric... | machine_learning/udacity/project_1/boston_housing_exploration.ipynb | necromuralist/machine_learning_studies | mit |
CRIM : 11.95, INDUS: 18.1, LSTAT: 12.13
Evaluating Model Performance
In this second section of the project, you will begin to develop the tools necessary for a model to make a prediction. Being able to accurately evaluate each model's performance through the use of these tools helps to greatly reinforce the confidence ... | # Put any import statements you need for this code block here
from sklearn import cross_validation
def shuffle_split_data(X, y):
""" Shuffles and splits data into 70% training and 30% testing subsets,
then returns the training and testing subsets. """
# Shuffle and split the data
X_train, X_test, y... | machine_learning/udacity/project_1/boston_housing_exploration.ipynb | necromuralist/machine_learning_studies | mit |
Question 4
Which performance metric below did you find was most appropriate for predicting housing prices and analyzing the total error. Why?
- Accuracy
- Precision
- Recall
- F1 Score
- Mean Squared Error (MSE)
- Mean Absolute Error (MAE)
Mean Squared Error was the most appropriate performance metric for predicting ho... | # Put any import statements you need for this code block
from sklearn.metrics import make_scorer
from sklearn.grid_search import GridSearchCV
def fit_model(X, y):
""" Tunes a decision tree regressor model using GridSearchCV on the input data X
and target labels y and returns this optimal model. """
#... | machine_learning/udacity/project_1/boston_housing_exploration.ipynb | necromuralist/machine_learning_studies | mit |
Analyzing Model Performance
In this third section of the project, you'll take a look at several models' learning and testing error rates on various subsets of training data. Additionally, you'll investigate one particular algorithm with an increasing max_depth parameter on the full training set to observe how model com... | with warnings.catch_warnings():
warnings.simplefilter('ignore')
learning_curves(X_train, y_train, X_test, y_test) | machine_learning/udacity/project_1/boston_housing_exploration.ipynb | necromuralist/machine_learning_studies | mit |
Question 7
Choose one of the learning curve graphs that are created above. What is the max depth for the chosen model? As the size of the training set increases, what happens to the training error? What happens to the testing error?
Looking at the model with max-depth of 3, as the size of the training set increases, th... | model_complexity(X_train, y_train, X_test, y_test) | machine_learning/udacity/project_1/boston_housing_exploration.ipynb | necromuralist/machine_learning_studies | mit |
Question 9
From the model complexity graph above, describe the training and testing errors as the max depth increases. Based on your interpretation of the graph, which max depth results in a model that best generalizes the dataset? Why?
As max-depth increases the training error improves, while the testing error decrea... | print "Final model optimal parameters:", reg.best_params_
reg.best_score_
models = (fit_model(housing_features, housing_prices) for model in range(1000))
params_scores = [(model.best_params_, model.best_score_) for model in models]
parameters = numpy.array([param_score[0]['max_depth'] for param_score in params_scores... | machine_learning/udacity/project_1/boston_housing_exploration.ipynb | necromuralist/machine_learning_studies | mit |
While a max-depth of 3 was the most common best-parameter, the max-depth of 5 was the median max-depth, had the highest median score, and had the highest overall score, so I will say that the optimal max_depth parameter is 5. This is slightly lower than my guess of 6, but doesn't seem too far off, although a max-depth ... | sale_price = reg.predict(CLIENT_FEATURES)
print "Predicted value of client's home: {0:.3f}".format(sale_price[0]) | machine_learning/udacity/project_1/boston_housing_exploration.ipynb | necromuralist/machine_learning_studies | mit |
We need to import several things from Keras. | from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer, Input
from tensorflow.keras.layers import Reshape, MaxPooling2D
from tensorflow.keras.layers import Conv2D, Dense, Flatten | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
PrettyTensor API
This is how the Convolutional Neural Network was implemented in Tutorial #03 using the PrettyTensor API. It is shown here for easy comparison to the Keras implementation below. | if False:
x_pretty = pt.wrap(x_image)
with pt.defaults_scope(activation_fn=tf.nn.relu):
y_pred, loss = x_pretty.\
conv2d(kernel=5, depth=16, name='layer_conv1').\
max_pool(kernel=2, stride=2).\
conv2d(kernel=5, depth=36, name='layer_conv2').\
max_pool(ker... | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Sequential Model
The Keras API has two modes of constructing Neural Networks. The simplest is the Sequential Model which only allows for the layers to be added in sequence. | # Start construction of the Keras Sequential model.
model = Sequential()
# Add an input layer which is similar to a feed_dict in TensorFlow.
# Note that the input-shape must be a tuple containing the image-size.
model.add(InputLayer(input_shape=(img_size_flat,)))
# The input is a flattened array with 784 elements,
# ... | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Model Compilation
The Neural Network has now been defined and must be finalized by adding a loss-function, optimizer and performance metrics. This is called model "compilation" in Keras.
We can either define the optimizer using a string, or if we want more control of its parameters then we need to instantiate an object... | from tensorflow.keras.optimizers import Adam
optimizer = Adam(lr=1e-3) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
For a classification-problem such as MNIST which has 10 possible classes, we need to use the loss-function called categorical_crossentropy. The performance metric we are interested in is the classification accuracy. | model.compile(optimizer=optimizer,
loss='categorical_crossentropy',
metrics=['accuracy']) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Training
Now that the model has been fully defined with loss-function and optimizer, we can train it. This function takes numpy-arrays and performs the given number of training epochs using the given batch-size. An epoch is one full use of the entire training-set. So for 10 epochs we would iterate randomly over the ent... | model.fit(x=data.x_train,
y=data.y_train,
epochs=1, batch_size=128) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Evaluation
Now that the model has been trained we can test its performance on the test-set. This also uses numpy-arrays as input. | result = model.evaluate(x=data.x_test,
y=data.y_test) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Prediction
We can also predict the classification for new images. We will just use some images from the test-set but you could load your own images into numpy arrays and use those instead. | images = data.x_test[0:9] | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Functional Model
The Keras API can also be used to construct more complicated networks using the Functional Model. This may look a little confusing at first, because each call to the Keras API will create and return an instance that is itself callable. It is not clear whether it is a function or an object - but we can ... | # Create an input layer which is similar to a feed_dict in TensorFlow.
# Note that the input-shape must be a tuple containing the image-size.
inputs = Input(shape=(img_size_flat,))
# Variable used for building the Neural Network.
net = inputs
# The input is an image as a flattened array with 784 elements.
# But the c... | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Model Compilation
We have now defined the architecture of the model with its input and output. We now have to create a Keras model and compile it with a loss-function and optimizer, so it is ready for training. | from tensorflow.python.keras.models import Model | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Create a new instance of the Keras Functional Model. We give it the inputs and outputs of the Convolutional Neural Network that we constructed above. | model2 = Model(inputs=inputs, outputs=outputs) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Compile the Keras model using the RMSprop optimizer and with a loss-function for multiple categories. The only performance metric we are interested in is the classification accuracy, but you could use a list of metrics here. | model2.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy']) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Training
The model has now been defined and compiled so it can be trained using the same fit() function as used in the Sequential Model above. This also takes numpy-arrays as input. | model2.fit(x=data.x_train,
y=data.y_train,
epochs=1, batch_size=128) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Evaluation
Once the model has been trained we can evaluate its performance on the test-set. This is the same syntax as for the Sequential Model. | result = model2.evaluate(x=data.x_test,
y=data.y_test) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
The result is a list of values, containing the loss-value and all the metrics we defined when we compiled the model. Note that 'accuracy' is now called 'acc' which is a small inconsistency. | for name, value in zip(model2.metrics_names, result):
print(name, value) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
We can also print the classification accuracy as a percentage: | print("{0}: {1:.2%}".format(model2.metrics_names[1], result[1])) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Examples of Mis-Classified Images
We can plot some examples of mis-classified images from the test-set.
First we get the predicted classes for all the images in the test-set: | y_pred = model2.predict(x=data.x_test) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Save & Load Model
NOTE: You need to install h5py for this to work!
Tutorial #04 was about saving and restoring the weights of a model using native TensorFlow code. It was an absolutely horrible API! Fortunately, Keras makes this very easy.
This is the file-path where we want to save the Keras model. | path_model = 'model.keras' | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Saving a Keras model with the trained weights is then just a single function call, as it should be. | model2.save(path_model) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Delete the model from memory so we are sure it is no longer used. | del model2 | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
We need to import this Keras function for loading the model. | from tensorflow.python.keras.models import load_model | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Loading the model is then just a single function-call, as it should be. | model3 = load_model(path_model) | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
We can then use the model again e.g. to make predictions. We get the first 9 images from the test-set and their true class-numbers. | images = data.x_test[0:9]
cls_true = data.y_test_cls[0:9] | 03C_Keras_API.ipynb | Hvass-Labs/TensorFlow-Tutorials | mit |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.