I am working on the file "12d-SpectralTemperatureEstimation.ipynb".
The first task is:
This method is simple and very accurate, but it does require confirmation that the measured scenarios correspond with the model used in the data analysis.  It is not a general solution, but specifically tuned to the equation used.
Python and module versions, and dates
Can you write Python code for it?

# you only need to do this once
#!pip install --upgrade version_information

%load_ext version_information
%version_information numpy, scipy, matplotlib, pyradi


I am working on the file "12g-Plume-texture.ipynb".
The first task is:
A dictionary stores the digitised data. The dictionary keys are the descriptions of the different data set. Each dictionary entry has three entries: (1) a list of the bitmaps for each of the lines, and the sample interval and iso-temperature (in degrees C) for the line, (2) the graph domain and range limits in metres, and (3) the scatter data set as digitised.
The quality of the subsequent grid sampling depends on the fineness of the scattered data. On the other hand, too dense sampling creates too large a data set.  By experiment the optimal number of samples were found, as shown below.
Can you write Python code for it?

#to digitise all the lines in all the graphs, and create one data structure for all data
datasets = {
    'Plume' :[{
    'data/plume-015a.PNG':[25,15],
    'data/plume-015b.PNG':[15,15],
    'data/plume-032.PNG':[15,32],
    'data/plume-060.PNG':[15,60],
    'data/plume-116.PNG':[10,116],
    'data/plume-171.PNG':[5,171],
    'data/plume-282.PNG':[3,282],
    'data/plume-394a.PNG':[3,394],
    'data/plume-394b.PNG':[3,394],
    },[0, 85.3, 0, 3.048] ],
}

for key in datasets.keys():
    datasets[key].append(prepareDataPoints(datasets[key][0],datasets[key][1]))
    print(datasets[key][2].shape)

# to plot the sampled temperature iso lines
def plotScatterDataSet(dataset,key, ikey):
    z = dataset[2]
    p = ryplot.Plotter(ikey,1,1,figsize=(12,3));
    p.plot(1,z[:,0], z[:,1],  xlabel='Distance from tail pipe m', ylabel='Radial distance m',
                 ptitle='Samples: {}, temperature K'.format(key),markers=['.'],linestyle='' );

for ikey,key in enumerate(datasets.keys()):
    plotScatterDataSet(datasets[key],key, ikey)


I am working on the file "12g-Plume-texture.ipynb".
The first task is:
At this point the temperatures are available on a fine regular grid as a function of down stream distance and radial displacement from the centre line.  The next step is to convert this to radiance. All imported and calculated data are stored in the datasets dictionary, for reuse later.
Sensor spectral response
The sensor spectral responses are used to support spectral integration.
Can you write Python code for it?

# to load and plot the sensor responses
sensors = ['Unity3_5.txt']
titles = ['MWIR']
q = ryplot.Plotter(1,1,1,figsize=(12,4))
sensorSpcs = {}
for i, (sensor, title) in enumerate(zip(sensors, titles) ):
    sdata = np.loadtxt('data/{}'.format(sensor),comments='%')
    sensorSpcs[sensor] = [sdata]
    wn = sensorSpcs[sensor][0][:,1]
    taus = sensorSpcs[sensor][0][:,2]
    q.plot(1,wn,taus,xlabel='Wavenumber cm$^{-1}$', ylabel='Response', ptitle='Sensor response',
        pltaxis=[500, 4000, 0, 1.1],label=[title],plotCol=[q.plotCol[i]])


I am working on the file "12g-Plume-texture.ipynb".
The first task is:
Interpolate the scanned temperature profiles
The scattered point digitised data sets must now be interpolated and resampled on a regular grid. This is done using the scipy.interpolate.griddata function.  To create symmetry in the boundary conditions, the scatter data set is mirrored along both axes. Linear interpolation works the best, cubic interpolation results in too many artefacts (possibly because of the sparse input data). After interpolation only the rear half, behinf the tailpipe, is retained.
Can you write Python code for it?

# to interpolate the temperature data to a regular grid
def interpolateDataSet(dataset,key, ikey, numDsamples, numRsamples):
    # get the scatter data set
    z = dataset[2]
    #mirror around the centre line
    zn = z.copy()
    zn[:,1] = -zn[:,1]
    z = np.append(z,zn,axis=0)
    #mirror around the tail pipe
    zn = z.copy()
    zn[:,0] = -zn[:,0]
    z = np.append(z,zn,axis=0)

    #create the regular grid , complex j signifies the number of samples, not step size
    xnew, ynew = np.mgrid[-dataset[1][1]:dataset[1][1]:numDsamples * 1j, -dataset[1][3]:dataset[1][3]:numRsamples * 1j]
    #interpolate
    znew = interpolate.griddata(z[:,0:-1], z[:,-1], (xnew, ynew), method='linear', rescale=True)
    # select only upper-right quadrant, behind tailpipe and above centreline
    select = xnew.__ge__(0) & xnew.__le__(80.) & ynew.__ge__(0)
    #number of selected points along the plume centreline:
    numd = np.sum(select[:, -1])
    #select and reshape back into 2D
    xnew = xnew[select].reshape(numd,-1)
    ynew = ynew[select].reshape(numd,-1)
    znew = znew[select].reshape(numd,-1)

    p = ryplot.Plotter(ikey,1,1,figsize=(12,3));
    p.meshContour(1, xnew, ynew, znew, xlabel='Distance from tail pipe $d$ m', ylabel='Radial distance $r$ m',
                  ptitle='{}, temperature K'.format(key), levels=100, contourLine=False, cbarshow=True);
    # remove the plot frame to not obscure data
    cp = p.getSubPlot(1)
    cp.spines['top'].set_visible(False)
    cp.spines['right'].set_visible(False)
    cp.spines['bottom'].set_visible(False)
    cp.spines['left'].set_visible(False)

    return xnew, ynew, znew

for ikey,key in enumerate(datasets.keys()):
    resolution = 0.1  # metre
    numd = 2 * np.max(datasets[key][2][:,0]) / resolution
    numr = 2 * np.max(datasets[key][2][:,1]) / resolution
    xnew, ynew, znew = interpolateDataSet(datasets[key],key, ikey, numd, numr)
    datasets[key].append([xnew, ynew, znew])

# at this point the data set dictionary has been appended to, and contains the...


I am working on the file "12g-Plume-texture.ipynb".
The first task is:
Plume emissivity
In the code used for emissivity calculation, a dictionary of function names is used to calculate the spectral emissivity.  The dictionary uses the spectral band (NIR,SWIR,MWIR,LWIR) as key and takes wavenumber and temperature as variables.  The code is implemented and used as shown in the following pseudocode:
def emisLWIR(wn, temperature=None):
    .....
    return emiss

def emisMWIR(wn, temperature=None):
    .....
    return emiss

emisFnLU = {'LWIR': emisLWIR, 'MWIR': emisMWIR}

When a spectral emissivity is required it is calculated as follows:
wn = np.linspace(...)
emislw = emisFnLU['LWIR'](wn,300)
emismw = emisFnLU['MWIR'](wn,300)

Plume temperature is only used in the MWIR calculation and ignored in the other spectral bands.
MWIR plume spectral emisivity
The plume spectral emissivity is determined as follows:

A measured emissivity near the tail pipe for a jet engine at 800 K is available (red line in the graph below).  This curve shows the pressure and temperature broadened emissivity over a distance of 100 m through the atmosphere.  From this graph two important extreme values for emissivity can be determined: the emissivity at 800 K, given by the outer shape of the curve and the emissivity at near-atmospheric temperatures given by the atmospheric attenuation (the inner shape).
Determine approximate shapes for the 800 K and 300 K gas masses (see the graph below).
Fit a curve to the two extreme emissivity shapes and determine the centre and width of these shapes.
Given the centre and width at the two extremes calculate the emissivity shape by linear interpolation of the centre and width at the required temperature.  The exact law to be used for this interpolation is not known and a simple linear interpolation method is used.
Given the temperature-derived centre and width, calculate the emissivity at that tempereture and use for radiance calculation.
Can you write Python code for it?

#to digitise emissivity graphs and fit curves to emissivity model

def prepareEmis(dicLines, gscale):
    for i,item in enumerate(dicLines.keys()):

        out = ryutils.extractGraph(item, gscale[0], gscale[1], gscale[2],gscale[3],
            None, False, False, False, step=dicLines[item][0], value=dicLines[item][1] )
        if i==0:
            outA = out
        else:
            outA = np.vstack((outA, out))
    return outA

emissets = {
    300: [
        {'data/emis-MWIR-300K.png':[10,None]},
        [1600., 3400, 0., 1.0], [2335, 80]
    ],
    800: [
        {'data/emis-MWIR-800K.png':[10,None]},
        [1600., 3400, 0., 1.0],  [2308, 191]
    ],
}

p = ryplot.Plotter(1,1,1,figsize=(12,4));
for ikey,key in enumerate(emissets.keys()):
    emissets[key].append(prepareEmis(emissets[key][0],emissets[key][1]))
    p.plot(1,emissets[key][3][:,0],emissets[key][3][:,1],label=['Emissivity {} K eyeball estimate'.format(key)]);

    emisF =ryutils.sfilter(emissets[key][3][:,0],center=emissets[key][2][0],
                           width=emissets[key][2][1], exponent=3, taupass=1, taustop=0.)
    p.plot(1,emissets[key][3][:,0],emisF,
           label=[r'Emissivity {} K fitted: $\tilde{{\nu}}_c$ {} $\Delta\tilde{{\nu}}$ {} cm$^{{-1}}$ '.format(key,emissets[key][2][0],emissets[key][2][1] )]);
emis = np.loadtxt('data/emis-800K-MWIR.txt')
p.plot(1, emis[:,1],emis[:,2],'Emissivity; measured and model','Wavenumber cm$^{-1}$',
       'Emissivity -',label=['Measured value at 800 K, near tailpipe'])  ;

# to calculate spectral emissivity at different temperatures

def emistemp(emissets, wn, temperature):
    wncent = np.interp(temperature, np.asarray([300,800]), np.asarray([emissets[300][2][0],emissets[800][2][0]]))
    wnwid  = np.interp(temperature, np.asarray([300,800]), np.asarray([emissets[300][2][1],emissets[800][2][1]]))
    emisF =ryutils.sfilter(wn,center=wncent,width=wnwid, exponent=3, taupass=1, taustop=0.)
    return emisF, wncent, wnwid

def emisMWIR(wn, temperature):
    emisF,_,_ =...


I am working on the file "12g-Plume-texture.ipynb".
The first task is:
Scan the temperature profile
The temperature profile is given in the form of a few graphs of iso-temperature values, redrawn as shown below.   It is assumed that these values represent temperatures in the plume as would be measured by a thermocouple (gas temperature).  For simplicity it is assumed that the plume is rotationally symmetrical.

Digitising data from a graph is a tiresome and inherently error-prone task. Manual (eyeball) reading the values is not suited for complex graphs, such as spectroscopy data.  For this purpose an alternative method was developed to scan an image of the graph using a short Python script.  The code is quite basic and therefore the procedure requires some manual preparatory work:

Obtain a PNG image (JPEG may have too much noise) of the graph by scanning or screen grab (Snipping tool on Windows).
Correct the image by removing any warping, skewing or rotation.  The end result should present the axes of the graph in a regular rectangle.  This task can be completed by using tools such as Photoshop, PrintShopPro, Gimp, ImageMagick or similar.
Remove all clutter from the image that should not appear in the scan. This clutter includes grid lines and all notation on the graph.  Retain only the graph to be digitised, because the code is looking for non-zero pixels in the image.
Crop the image such that only the data portion of the graph is present in the image, in other words, the bounds of the image should correspond with the minimum and maximum values of the graph.  The image bounds are used to scale the sampled values later.  At this point the image should be sized to hold the graph line, and the $(d,r)$ values of the image edges must be known.
Use the Python digitising function extractGraph to extract the scaled coordinates of the sampled image pixels in a text file.

Instead of processing the graph image on bitmap pixel level, some graphs can be redrawn in a tool such as CorelDraw or Inscape.  Import the image and manually draw the lines with Bezier curves.   Once the line is...
Can you write Python code for it?

def extractGraph(filename, xmin, xmax, ymin, ymax, outfile=None,doPlot=False,\
        xaxisLog=False, yaxisLog=False, step=None, value=None):
    """Scan an image containing graph lines and produce (x,y,value) data.

    This function processes an image, calculate the location of pixels on a
    graph line, and then scale the (r,c) or (x,y) values of pixels with non-zero
    values. The

    Get a bitmap of the graph (scan or screen capture).
    Take care to make the graph x and y axes horizontal/vertical.
    The current version of the software does not work with rotated images.
    Bitmap edit the graph. Clean the graph to the maximum extent possible,
    by removing all the clutter, such that only the line to be scanned is visible.
    Crop only the central block that contains the graph box, by deleting
    the x and y axes notation and other clutter. The size of the cropped image
    must cover the range in x and y values you want to cover in the scan. The
    graph image/box must be cut out such that the x and y axes min and max
    correspond exactly with the edges of the bitmap.
    You must end up with nothing in the image except the line you want
    to digitize.

    The current version only handles single lines on the graph, but it does
    handle vertical and horizontal lines.

    The function can also write out a value associated with the (x,y) coordinates
    of the graph, as the third column. Normally these would have all the same
    value if the line represents an iso value.

    The x,y axes can be lin/lin, lin/log, log/lin or log/log, set the flags.

    Args:
        | filename: name of the image file
        | xmin: the value corresponding to the left side (column=0)
        | xmax: the value corresponding to the right side (column=max)
        | ymin: the value corresponding to the bottom side (row=bottom)
        | ymax: the value corresponding to the top side (row=top)
        | outfile: write the sampled points to this output file
        | doPlot: plot the...


I am working on the file "13 Where are human-adapted mutations found?.ipynb".
The first task is:
Introduction
There are particular polymorphisms that are found that have been experimentally shown (on a limited scale) to confer host adaptation mutations. I want to see whether there is evidence of these polymorphisms flowing into human populations, or if they instead arise independently post-infection.
12 March 2015: For simplicity, I am first starting with the PB2 gene only, and looking only at E627K.
Can you write Python code for it?

# Open the sequences
sequences = SeqIO.to_dict(SeqIO.parse('20150312_PB2_CDS_from_Whole_Genomes.fasta', 'fasta'))
# sequences

data = pd.read_csv('20150312_PB2_CDS_from_Whole_Genomes.csv', parse_dates=['Collection Date'])
data['Strain Name'] = data['Strain Name'].str.split('(').str[0]
data['Sequence Accession'] = data['Sequence Accession'].str.strip('*')
data
accession_strain = dict(zip(data['Sequence Accession'], data['Strain Name']))
accession_strain

G = nx.read_gpickle('20141103 All IRD Final Graph.pkl')
G.nodes()


I am working on the file "13 Where are human-adapted mutations found?.ipynb".
The first task is:
We need a function to figure out what polymorphism exists in a particular position along the PB2 gene. However, this is complicated by the fact that there are PB2 sequences with indels, hence the exact numbering might not be the same. To get around this, I will take a "best match" approach that Justin Z. coded up earlier on. To reduce the search space, I will also tokenize a short region ±40 a.a.
Can you write Python code for it?

def tokenize_string(string, token_length, start_pos, end_pos):
    """
    Takes an input string, and returns a dictionary where:
        - key = position, and
        - value = a tokenized string of length token_length
    """
    assert end_pos > start_pos, "End position must be greater than start position."
    assert (end_pos - start_pos) > token_length, "Token length must be smaller than the position range."

    tokens = dict()

    for i in range(start_pos, end_pos-token_length):
        tokens[string[i:i+token_length]] = i

    return tokens

# tokenize_string(eg_seq, 20, 627-30, 627+30)

# Of the tokenized strings, there will be a few with a "best match" i.e. minimum distance.
# We are only interested in comparing against that one.
def get_best_tokens(tokens, query):
    """
    Takes in:
    - a dictionary containing the tokens of a string & their position.
    - a query string of equal length to the keys in the dictionary.

    Returns:
    - a list of tokens that have the smallest Levenshtein distance to the query string.
    """
    from Levenshtein import distance

    best_tokens = set()
    min_distance = len(query)
    for token, position in tokens.items():
        if distance(token, query) < min_distance:
            min_distance = distance(token, query)
            best_tokens = set()
            best_tokens.add(token)
        if distance(token, query) == min_distance:
            best_tokens.add(token)
        if distance(token, query) > min_distance:
            pass

    return(list(best_tokens))

# Let's then setup a reference string that contains a E at the 627th position in the a.a. sequence.
# - The first letter in the reference string will be the letter E.
# - The length of the string will be 20 a.a. long.
# Looking to the future, we call it a match if at least 17 out of 20 a.a. are identical.

eg_seq = str(sequences['CY005153'].seq)
refstring = eg_seq[626:626+20]

idx = 6720
for i, (accession, seqrecord) in enumerate(sequences.items()):
   ...


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
11.Drucke (print) das Total der Bevölkerungen aller Städte aus
Can you write Python code for it?

z = 0
for x in stadt_dic:
    y = x['Bevölkerung']
    z += y
print(z)


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
12.Rechne den Anteil der Bevölkerung der jeweiligen Städte an der Gesamtbevölkerung der Schweiz aus und "print" die Ergebnisse neben dem Städtenamen
Can you write Python code for it?

ch = 8372000
for x in stadt_dic:
    y = x['Bevölkerung']
    s = x['Stadt']
    print(s , round(y/ch*100, 2), '%')


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
13.Füge noch die Städte Winterthur und St. Gallen hinzu
Can you write Python code for it?

winti = {'Kanton': 'ZH', 'Stadt': 'Winterthur', 'Bevölkerung': 106778}
luzern = {'Kanton': 'LU', 'Stadt': 'Luzern', 'Bevölkerung': 81284}


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
15.Wiederhole den loop von oben, um den Anteil der Bevölkerung an der Gesamtbevölkerung auszurechnen.
Can you write Python code for it?


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
2.Mache dasselbe mit einer Liste aus 100 Elementen und ordne sie der Variabel b zu.
Can you write Python code for it?


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
3.Füge beiden Listen folgenden String an: 'ich bin keine Zahl'
Can you write Python code for it?

a.append('ich bin keine Zahl')
b.append('ich bin keine Zahl')


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
6.Von der list b, zeige nur die letzten zehn Nummern an
Can you write Python code for it?


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
8.Multipliziere jede Nummer in dieser Liste, die kleiner ist als 100, mit 100; wenn die Nummern zwischen 100 und 1000 verwandle sie in eine String, und wenn sie grösser oder gleich 1000 ist, lösche sie.
Can you write Python code for it?

for x in lst:
    if x >= 1000:
        continue
    elif x > 100:
        print(str(x))
    else:
        print(x*100)


I am working on the file "13 lösungen/02 Python I Homework.ipynb".
The first task is:
9.Schreibe eine Dictionary-Liste der fünf grössten Schweizer Städte, ihrer Bevölkerung und dem jeweiligen Kanton.
Can you write Python code for it?

stadt_dic = [{'Kanton': 'GE', 'Stadt': 'Genf', 'Bevölkerung': 194565},
             {'Kanton': 'ZH', 'Stadt': 'Zürich', 'Bevölkerung': 396027},
             {'Kanton': 'BS', 'Stadt': 'Basel', 'Bevölkerung': 175131},
             {'Kanton': 'BE', 'Stadt': 'Bern', 'Bevölkerung': 140634},
             {'Kanton': 'VD', 'Stadt': 'Lausanne', 'Bevölkerung': 135629}
            ]


I am working on the file "13 lösungen/03 Python II Homework.ipynb".
The first task is:
2.Baue einen for-loop, der durch vordefinierte Zahlen-list geht, und mithilfe der eben kreierten eigenen Funktion, alle Resultate verdoppelt ausdruckt.
Can you write Python code for it?

lst = list(range(1,5))

for n in lst:
    print(double(n))


I am working on the file "13 lösungen/03 Python II Homework.ipynb".
The first task is:
3.Entwickle einen Code, der den Nutzer nach der Länge seinem Namen fragt, und ihm dann sagt, wieviele Zeichen sein Name hat.
Can you write Python code for it?

elem = input('Wie heisst Du?')
length = len(elem)
print('Hallo '+ elem+ ','+ ' Dein Name hat '+ str(length)+ ' Zeichen.')


I am working on the file "13 lösungen/03 Python II Homework.ipynb".
The first task is:
5.Wir haben in einem Dictionary mit Massen, die mit ganz unterschiedlichen Formaten daherkommen. Entwickle eine Funktion namens, die diese Formate berücksichtigt, und in Meter umwandelt.
Can you write Python code for it?

#Unsere Formate
var_first = { 'measurement': 3.4, 'scale': 'kilometer' }
var_second = { 'measurement': 9.1, 'scale': 'mile' }
var_third = { 'measurement': 2.0, 'scale': 'meter' }
var_fourth = { 'measurement': 9.0, 'scale': 'inches' }

def m_converter(measurement):
    if measurement['scale'] == 'kilometer':
        return measurement['measurement'] * 1000
    if measurement['scale'] == 'mile':
        return measurement['measurement'] * 1600
    if measurement['scale'] == 'inches':
        return measurement['measurement'] * 0.0254
    else:
        return measurement['measurement']

print(m_converter(var_first))
print(m_converter(var_second))
print(m_converter(var_third))
print(m_converter(var_fourth))


I am working on the file "13--pagerank-partial-solns.ipynb".
The first task is:
Exercise. Complete the PageRank implementation for this dataset.
Can you write Python code for it?

def scale_vector (x, alpha):
    """Scales the dense vector x by a constant alpha."""
    return [x_i*alpha for x_i in x]

def offset_vector (x, c):
    """Adds the scalar value c to every element of a dense vector x."""
    return [x_i+c for x_i in x]

ALPHA = 0.85 # Probability of following some link
MAX_ITERS = 25

# Let X[t] store the dense x(t) vector at time t
X = []

x_0 = dense_vector (n, 1.0/n) # Initial distribution: 1/n at each page
X.append (x_0)

for t in range (1, MAX_ITERS):
    # Complete this implementation

    X.append (...)


I am working on the file "13--pagerank-partial-solns.ipynb".
The first task is:
Exercise. Compute a view called Outdegrees, which contains the following columns:

Id: vertex ID
Degree: the out-degree of this vertex.

To help you test your view, the following snippet includes a second query that selects from your view but adds a Url field and orders the results in descending order of degree. It also prints first few and last few rows of this query, so you can inspect the URLs as a sanity check. (Perhaps it also provides a small bit of entertainment!)
Can you write Python code for it?

# Complete this query:
query = '''
  CREATE VIEW IF NOT EXISTS Outdegrees AS
    SELECT Source AS Id, COUNT(*) AS Degree
      FROM Edges
      GROUP BY Source
'''
c = conn.cursor ()
c.execute (query)

from IPython.display import display

query = '''
  SELECT Outdegrees.Id, Degree, Url
    FROM Outdegrees, Vertices
    WHERE Outdegrees.Id = Vertices.Id
    ORDER BY -Degree
'''
df_outdegrees = pd.read_sql_query (query, conn)
print "==> A few entries with large out-degrees:"
display (df_outdegrees.head ())
print "\n==> A few entries with small out-degrees:"
display (df_outdegrees.tail ())


I am working on the file "13--pagerank-partial-solns.ipynb".
The first task is:
Exercise. Determine which vertices have no incident edges. Store the number of such vertices in a variable, num_solo_vertices.
Can you write Python code for it?

query = '''
  SELECT Id, Url
    FROM Vertices
    WHERE (Id NOT IN (SELECT DISTINCT Source FROM Edges))
          AND (Id NOT IN (SELECT DISTINCT Target FROM Edges))
'''
df_solo_vertices = pd.read_sql_query (query, conn)
print df_solo_vertices.head ()

num_solo_vertices = len (df_solo_vertices)

# Our testing code follows, assuming your `num_solo_vertices` variable:
print ("\n==> %d vertices have no incident edges." % num_solo_vertices)
assert num_solo_vertices == 266


I am working on the file "13--pagerank-partial-solns.ipynb".
The first task is:
Exercise. Implement a function to multiply a sparse matrix by a dense vector, assuming a dense vector defined as follows.
Can you write Python code for it?

def dense_vector (n, init_val=0.0):
    """
    Returns a dense vector of length `n`, with all entries set to
    `init_val`.
    """
    return [init_val] * n

# Implement this routine:
def spmv (n, A, x):
    """Returns a dense vector y of length n, where y = A*x."""
    pass


I am working on the file "13--pagerank-partial-solns.ipynb".
The first task is:
Exercise. Make sure every edge has its end points in the vertex table.
Can you write Python code for it?

query = '''
  SELECT {col} FROM Edges
    WHERE {col} NOT IN (SELECT Id FROM Vertices)
'''

df_s = pd.read_sql_query (query.format (col='Source'), conn)
print (df_s['Source'])

df_t = pd.read_sql_query (query.format (col='Target'), conn)
print (df_t['Target'])

assert df_s['Source'].empty
assert df_t['Target'].empty

print ("==> Verified: All source and target IDs are vertices.")


I am working on the file "13--pagerank-partial-solns.ipynb".
The first task is:
Exercise. Query the database to extract a report of which URLs point to which URLs. Also include the source vertex out-degree and order the rows in descending order by it.
Can you write Python code for it?

query = '''
  SELECT S.Url, T.Url, Out.Degree
    FROM Edges AS E,
         (SELECT Id, Url FROM Vertices) AS S,
         (SELECT Id, Url FROM Vertices) AS T,
         (SELECT Id, Degree FROM Outdegrees) AS Out
    WHERE (E.Source=S.Id) AND (E.Target=T.Id) AND (E.Source=Out.Id)
    ORDER BY -Out.Degree
'''
df_G = pd.read_sql_query (query, conn)

from IPython.display import display
display (df_G.head ())
print ("...")
display (df_G.tail ())


I am working on the file "13--pagerank-partial-solns.ipynb".
The first task is:
Part 2: Implement PageRank
The following exercises will walk you through a possible implementation of PageRank for this dataset.
Exercise. Build a sparse matrix, A_1, that stores $G^TD^{-1}$, where $G^T$ is the transpose of the connectivity matrix $G$, and $D^{-1}$ is the diagonal matrix of inverse out-degrees.
Can you write Python code for it?

from cse6040utils import sparse_matrix

A_1 = sparse_matrix () # Initially all zeros, with no rows or columns

# Insert your code here


I am working on the file "13-Error-Based-Learning/Error_Based_Learning.ipynb".
The first task is:
A standard approach is to select $m$ and $b$ that minimize the mean squared error (MSE) over the available dataset. For a given value of $m$ and $b$, and estimated output $\hat{y}$, the MSE is defined as:
$$MSE(m,b) = \frac{1}{2N} \sum_{i=1}^N \left(\hat{y}i - y_i\right)^2 = \frac{1}{2N} \sum{i=1}^N \left(m x_i + b - y_i\right)^2.$$
Can you write Python code for it?

# the MSE for various values of slope m and y-intercept b:
m = np.linspace(-10, 15, 40);
b = np.linspace(-3000, 3000, 20);
MSEmb = np.zeros((len(m), len(b)));
for ii in range(len(m)):
    for jj in range(len(b)):
        MSEmb[ii,jj] = sum((m[ii]*df.Size + b[jj] - df.Price)**2)/(2*len(df))

plt.pcolor(b, m, MSEmb);


I am working on the file "13-Error-Based-Learning/Error_Based_Learning.ipynb".
The first task is:
Minimizing Error
To minimize the MSE, consider the derivative with respect to each of the model parameters. The minimum MSE occurs where the derivative is zero with respect to both parameters:
$$\frac{\partial MSE}{\partial m} = \frac{1}{N} \sum_{i=1}^N ((m x_i + b) - y_i)x_i = 0  \Rightarrow m = \frac{\sum_{i=1}^N (b - y_i)x_i}{\sum_{i=1}^N x_i^2}$$
$$\frac{\partial MSE}{\partial b} = \frac{1}{N} \sum_{i=1}^N ((m x_i + b) - y_i) = 0 \Rightarrow b = \frac{\sum_{i=1}^N (m x_i - y_i)}{N}$$
This gives you a set of two coupled linear equations, which can be solved exactly... but it is often useful to use a more generally applicable iterative method like gradient descent.
Gradient Descent
Gradient descent is a function-minimization (maximization) method that is used in a wide range of applications.

"Running down (up) hill" along direction of steepest descent (ascent).
Can be slow to converge
Can get stuck in local minima (maxima) (exception: convex problems, which only have one minimum (maximum))

How it works:
* Say we're optimizing $q = (b, m)$. At time step $i$, we have parameters $q_i = (b_i, m_i)$. We update the values using:
$$q_{i+1} = q_i - \alpha \frac{{\rm d}MSE}{{\rm d}q},$$
where $\alpha$ is the learning rate and $\frac{{\rm d}MSE}{{\rm d}q} = \left(\frac{\partial MSE}{\partial b}, \frac{\partial MSE}{\partial m} \right)$.
Notes on choosing the learning rate:
* Choosing the learning rate is not an exact science.
* A small learning rate can be slow to converge.
* A large learning rate can cause you to overshoot the minimum (maximum), and the algorithm could fail to converge.
* If the learning rate is too small or too large, it's often a good idea to choose a learning rate my multiplying by a constant factor (e.g. try 0.00001, 0.01, ..., 1, 10)
* To get the "best of both worlds" (a learning rate that is larger far from the minimum and smaller for finer adjustments as you reach the goal), the learning rate can decay over time with a fixed schedule (e.g. at time step $k$, $\alpha[k] = \alpha_0\frac{c}{c...
Can you write Python code for it?

# set step size:
a = 0.000001
N = len(df)

# set initial conditions
b = 10
m = 0.8

# set initial MSE values
MSE_old = 1.e8
MSE = sum(m*df.Size + b - df.Price)**2/(2*N)

steps = 0
MSE_vals = [MSE]
b_vals = [b]
m_vals = [m]

# run gradient descent:
while (MSE_old-MSE > 0.00001) and (steps < 1e5):
    # compute gradient
    dMSE_db = sum(m*df.Size + b - df.Price)/N
    dMSE_dm = sum((m*df.Size + b - df.Price)*df.Size)/N
    # update parameter values
    m -= a*dMSE_dm
    b -= a*dMSE_db
    # update MSE values
    MSE_old = MSE
    MSE = sum(m*df.Size + b - df.Price)**2/(2*N)
    MSE_vals.append(MSE)
    b_vals.append(b)
    m_vals.append(m)
    steps += 1

# descrease in MSE with incresing step number
plt.plot(MSE_vals);
plt.ylabel("MSE");
plt.xlabel("step");

# path in parameter space
# the MSE for various values of slope m and y-intercept b:
mv = np.linspace(-0.5, 1.5, 40);
bv = np.linspace(0, 15, 20);
MSEmb = np.zeros((len(mv), len(bv)));
for ii in range(len(mv)):
    for jj in range(len(bv)):
        MSEmb[ii,jj] = sum((mv[ii]*df.Size + bv[jj] - df.Price)**2)/(2*len(df))
plt.pcolor(bv, mv, MSEmb);
plt.plot(b_vals, m_vals, 'xg');
plt.plot(6.47, 0.62, 'or');
plt.xlabel("b")
plt.ylabel("m")

plt.plot(df.Size, df.Price, 'ob');
plt.plot(df.Size, m*df.Size+b, 'r');
plt.plot(df.Size, 0.62*df.Size+6.47, '--k');


I am working on the file "13-Error-Based-Learning/Error_Based_Learning.ipynb".
The first task is:
Multilinear Regression
Multilinear regression is the same as linear regression, but now you have more than one input feature. That is, you have a set of input features $x^1, \ldots, x^M$  and input-output data $(x^1_i, \ldots, x^M_i, y_i)$, and you are trying to optimize over parameters $(\beta_0, \beta_1, \ldots, \beta_M)$, where you assume a model of the form:
$$y = \beta_0 + \beta_1 x^1 + \ldots + \beta_M x^M$$
For convenience, it is helpful to define an additional "feature" $x^0$ that is always equal to $1$. Then you can write:
$$y = \beta_0 x^0 + \beta_1 x^1 + \ldots + \beta_M x^M = B^T X,$$
where $B = (\beta_0, \ldots, \beta_M)^T$ and $X = (x^0, \ldots, x^M)^T$.
Just like before, your goal is to minimize the MSE:
$$MSE(B) = \frac{1}{2N} \sum_{i=1}^N \left(\hat{y}i - y_i\right)^2 = \frac{1}{2N} \sum{i=1}^N \left(\beta_0 x^0_i + \ldots + \beta_M x^M_i - y_i\right)^2  = \frac{1}{2N} \sum_{i=1}^N \left(B^TX_i - y_i\right)^2.$$
Then you just use gradient descent like any other time.

It is often better to pre-process the data to get zero-mean features with similar variances (this can help e.g. avoid the problem in our linear regression example, where the gradient along $b$ is much smaller than the gradient along $m$).
Can you write Python code for it?

from sklearn import linear_model
from sklearn.metrics import mean_squared_error, r2_score

# linear regression model from sklearn
lin_mod = linear_model.LinearRegression()

lin_mod.fit(df[['Size','Floor','Rate']], df.Price)

# Fit coefficients:
print('Coefficients: %s, %s'%(lin_mod.intercept_.astype(str), ", ".join(map(str, lin_mod.coef_))))

plt.plot(df.Size, df.Price, 'ob');
plt.plot(df.Size, lin_mod.intercept_ + lin_mod.coef_[0]*df.Size + lin_mod.coef_[1]*df.Floor + lin_mod.coef_[2]*df.Rate, 'r');
plt.plot(df.Size, -0.1513 + 0.6270*df.Size - 0.1781*df.Floor + 0.0714*df.Rate, '--k');
plt.xlabel("Size");
plt.ylabel("Price");

Price_pred = lin_mod.predict(df[['Size','Floor','Rate']])

mean_squared_error(df.Price, Price_pred)

r2_score(df.Price, Price_pred)


I am working on the file "13. Reinforcement learning.ipynb".
The first task is:
Above is an example of a starting environment in our simple game. The agent controls the blue square, and can move up, down, left, or right. The goal is to move to the green square (for +1 reward) and avoid the red square (for -1 reward). The position of the three blocks is randomized every episode.
Can you write Python code for it?

class Qnetwork():
    def __init__(self,h_size):
        #The network recieves a frame from the game, flattened into an array.
        #It then resizes it and processes it through four convolutional layers.
        self.scalarInput =  tf.placeholder(shape=[None,21168],dtype=tf.float32)
        self.imageIn = tf.reshape(self.scalarInput,shape=[-1,84,84,3])
        self.conv1 = slim.conv2d( \
            inputs=self.imageIn,num_outputs=32,kernel_size=[8,8],stride=[4,4],padding='VALID', biases_initializer=None)
        self.conv2 = slim.conv2d( \
            inputs=self.conv1,num_outputs=64,kernel_size=[4,4],stride=[2,2],padding='VALID', biases_initializer=None)
        self.conv3 = slim.conv2d( \
            inputs=self.conv2,num_outputs=64,kernel_size=[3,3],stride=[1,1],padding='VALID', biases_initializer=None)
        self.conv4 = slim.conv2d( \
            inputs=self.conv3,num_outputs=h_size,kernel_size=[7,7],stride=[1,1],padding='VALID', biases_initializer=None)

        #We take the output from the final convolutional layer and split it into separate advantage and value streams.
        self.streamAC,self.streamVC = tf.split(self.conv4,2,3)
        self.streamA = slim.flatten(self.streamAC)
        self.streamV = slim.flatten(self.streamVC)
        xavier_init = tf.contrib.layers.xavier_initializer()
        self.AW = tf.Variable(xavier_init([h_size//2,env.actions]))
        self.VW = tf.Variable(xavier_init([h_size//2,1]))
        self.Advantage = tf.matmul(self.streamA,self.AW)
        self.Value = tf.matmul(self.streamV,self.VW)

        #Then combine them together to get our final Q-values.
        self.Qout = self.Value + tf.subtract(self.Advantage,tf.reduce_mean(self.Advantage,axis=1,keep_dims=True))
        self.predict = tf.argmax(self.Qout,1)

        #Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values.
        self.targetQ = tf.placeholder(shape=[None],dtype=tf.float32)
        self.actions =...


I am working on the file "13. Reinforcement learning.ipynb".
The first task is:
Deep Q-networks
While our ordinary Q-network was able to barely perform as well as the Q-Table in a simple game environment, Deep $Q$-Networks are much more capable. In order to transform an ordinary Q-Network into a DQN we will be making the following improvements:
+ Going from a single-layer network to a multi-layer convolutional network.
+ Implementing Experience Replay, which will allow our network to train itself using stored memories from it’s experience.
+ Utilizing a second “target” network, which we will use to compute target $Q$-values during our updates.
<img src="images/deepq1.png" alt="" style="width: 800px;"/>
See https://jaromiru.com/2016/09/27/lets-make-a-dqn-theory/
Convolutional Layers
Since our agent is going to be learning to play video games, it has to be able to make sense of the game’s screen output in a way that is at least similar to how humans or other intelligent animals are able to. Instead of considering each pixel independently, convolutional layers allow us to consider regions of an image, and maintain spatial relationships between the objects on the screen as we send information up to higher levels of the network.
Experience Replay
The second major addition to make DQNs work is Experience Replay.
The problem with online learning is that the samples arrive in order they are experienced and as such are highly correlated. Because of this, our network will most likely overfit and fail to generalize properly.
The key idea of experience replay is that we store these transitions in our memory and during each learning step, sample a random batch and perform a gradient descend on it.
The Experience Replay buffer stores a fixed number of recent memories, and as new ones come in, old ones are removed. When the time comes to train, we simply draw a uniform batch of random memories from the buffer, and train our network with them.
Separate Target Network
This second network is used to generate the $Q$-target values that will be used to compute the loss for every action during training....
Can you write Python code for it?

from __future__ import division

import gym
import numpy as np
import random
import tensorflow as tf
import tensorflow.contrib.slim as slim
import matplotlib.pyplot as plt
import scipy.misc
import os
%matplotlib inline

import numpy as np
import random
import itertools
import scipy.misc
import matplotlib.pyplot as plt

class gameOb():
    def __init__(self,coordinates,size,intensity,channel,reward,name):
        self.x = coordinates[0]
        self.y = coordinates[1]
        self.size = size
        self.intensity = intensity
        self.channel = channel
        self.reward = reward
        self.name = name

class gameEnv():
    def __init__(self,partial,size):
        self.sizeX = size
        self.sizeY = size
        self.actions = 4
        self.objects = []
        self.partial = partial
        a = self.reset()
        plt.imshow(a,interpolation="nearest")

    def reset(self):
        self.objects = []
        hero = gameOb(self.newPosition(),1,1,2,None,'hero')
        self.objects.append(hero)
        bug = gameOb(self.newPosition(),1,1,1,1,'goal')
        self.objects.append(bug)
        hole = gameOb(self.newPosition(),1,1,0,-1,'fire')
        self.objects.append(hole)
        bug2 = gameOb(self.newPosition(),1,1,1,1,'goal')
        self.objects.append(bug2)
        hole2 = gameOb(self.newPosition(),1,1,0,-1,'fire')
        self.objects.append(hole2)
        bug3 = gameOb(self.newPosition(),1,1,1,1,'goal')
        self.objects.append(bug3)
        bug4 = gameOb(self.newPosition(),1,1,1,1,'goal')
        self.objects.append(bug4)
        state = self.renderEnv()
        self.state = state
        return state

    def moveChar(self,direction):
        # 0 - up, 1 - down, 2 - left, 3 - right
        hero = self.objects[0]
        heroX = hero.x
        heroY = hero.y
        penalize = 0.
        if direction == 0 and hero.y >= 1:
            hero.y -= 1
        if direction == 1 and hero.y <= self.sizeY-2:
            hero.y += 1
        if direction == 2 and hero.x >= 1:
...


I am working on the file "13. Reinforcement learning.ipynb".
The first task is:
FrozenLake-v0 is considered "solved" when the agent obtains an average reward of at least 0.78 over 100 consecutive episodes.
Can you write Python code for it?

print "Score over time: " +  str(sum(rList[-100:])/100)
print "Final Q-Table Values"
print Q


I am working on the file "13. Reinforcement learning.ipynb".
The first task is:
How are we estimating the value of an action?
If by the $t$-th time step action $a$ has been chosen $K_a$ times prior to $t$, yielding rewards
$R_1, R_2, . . . , R_{Ka}$, then its value is estimated to be:
$$
Q_t(a) = \frac{R_1 + R_2 + . . .+ R_{Ka}}{K_a}
$$
A problem with this straightforward implementation is that its memory and computational requirements grow over time without
bound (we have to maintain, for each action $a$, a record of all the rewards that have followed the
selection of that action), but we can derive an incremental formula for computing averages with small, constant computation
required to process each new reward.
\begin{eqnarray}
Q_{k+1} & = & \frac{1}{k+1} \sum_{i=1}^{k+1} R_i \
  & = & \frac{1}{k+1} \left( R_{k+1} + \sum_{i=1}^k R_i
\right) \
  & = & \frac{1}{k+1} \left( R_{k+1} + kQ_k + Q_k - Q_k
\right) \
  & = & \frac{1}{k+1} \left(R_{k+1} + (k+1) Q_k - Q_k
\right) \
  & = & Q_k + \frac{1}{k+1} \left( R_{k+1} - Q_k \right)
\end{eqnarray}
Can you write Python code for it?

def experiment(bandit,Npulls,epsilon):
    history = []
    for i in range(Npulls):
        action = bandit.choose_eps_greedy(epsilon)
        R = bandit.get_reward(action)
        bandit.update_est(action,R)
        history.append(R)
    return np.array(history)


I am working on the file "13. Reinforcement learning.ipynb".
The first task is:
It also begins to progress through the environment for longer than chance aroudn the 1000 mark as well.
Can you write Python code for it?


I am working on the file "13. Reinforcement learning.ipynb".
The first task is:
Let's make three different experiments: $\epsilon = 0$, $\epsilon = 0.1$ and $\epsilon = 0.01$. Data will be averages over 2000 tasks.
Can you write Python code for it?

Nexp = 2000
Npulls = 5000
avg_outcome_eps0p0 = np.zeros(Npulls)
avg_outcome_eps0p01 = np.zeros(Npulls)
avg_outcome_eps0p1 = np.zeros(Npulls)

for i in range(Nexp):
    bandit = Bandit()
    avg_outcome_eps0p0 += experiment(bandit,Npulls,0.0)
    bandit = Bandit()
    avg_outcome_eps0p01 += experiment(bandit,Npulls,0.01)
    bandit = Bandit()
    avg_outcome_eps0p1 += experiment(bandit,Npulls,0.1)

avg_outcome_eps0p0 /= np.float(Nexp)
avg_outcome_eps0p01 /= np.float(Nexp)
avg_outcome_eps0p1 /= np.float(Nexp)

# plot results
import matplotlib.pyplot as plt

plt.plot(avg_outcome_eps0p0,label="eps = 0.0", alpha=0.5)
plt.plot(avg_outcome_eps0p01,label="eps = 0.01", alpha=0.5)
plt.plot(avg_outcome_eps0p1,label="eps = 0.1", alpha=0.5)
plt.ylim(0,2.2)
plt.legend()
plt.gcf().set_size_inches((8,3))
plt.show()


I am working on the file "13. Reinforcement learning.ipynb".
The first task is:
Q-Learning with Neural Networks
Now, you may be thinking: tables are great, but they don’t really scale, do they? While it is easy to have a 16x4 table for a simple grid world, the number of possible states in any modern game or real-world environment is nearly infinitely larger. For most interesting problems, tables simply don’t work.
We instead need some way to take a description of our state, and produce $Q$-values for actions without a table: that is where neural networks come in. By acting as a function approximator, we can take any number of possible states that can be represented as a vector and learn to map them to $Q$-values.
In the case of the FrozenLake example, we will be using a one-layer network which takes the state encoded in a one-hot vector (1x16), and produces a vector of 4 $Q$-values, one for each action.
Such a simple network acts kind of like a glorified table, with the network weights serving as the old cells. The key difference is that we can easily expand the Tensorflow network with added layers, activation functions, and different input types, whereas all that is impossible with a regular table.
<img src="images/pong.jpg" alt="" style="width: 300px;"/>
The method of updating is a little different as well. Instead of directly updating our table, with a network we will be using backpropagation and a loss function. Our loss function will be sum-of-squares loss, where the difference between the current predicted $Q$-values, and the “target” value is computed and the gradients passed through the network. In this case, our $Q_{target}$ for the chosen action is the equivalent to the $Q$-value computed in equation above ($
Q(s,a) + \alpha [r + \gamma max_{a'} Q(s',a') - Q(s,a) ]
$).
$$
Loss = \sum (Q_{target} - Q_{predicted})^2
$$
Can you write Python code for it?

import gym
import numpy as np
import random
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('FrozenLake-v0')

tf.reset_default_graph()

#These lines establish the feed-forward part of the network used to choose actions
inputs1 = tf.placeholder(shape=[1,16],dtype=tf.float32)
W = tf.Variable(tf.random_uniform([16,4],0,0.01))
Qout = tf.matmul(inputs1,W)
predict = tf.argmax(Qout,1)

#Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values.
nextQ = tf.placeholder(shape=[1,4],dtype=tf.float32)
loss = tf.reduce_sum(tf.square(nextQ - Qout))
trainer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
updateModel = trainer.minimize(loss)

init = tf.global_variables_initializer()

# Set learning parameters
y = .99
e = 0.1
num_episodes = 2000

#create lists to contain total rewards and steps per episode
jList = []
rList = []
with tf.Session() as sess:
    sess.run(init)
    for i in range(num_episodes):
        #Reset environment and get first new observation
        s = env.reset()
        rAll = 0
        d = False
        j = 0
        #The Q-Network
        while j < 99:
            j+=1
            #Choose an action by greedily (with e chance of random action) from the Q-network
            a,allQ = sess.run([predict,Qout],
                              feed_dict={inputs1:np.identity(16)[s:s+1]})
            if np.random.rand(1) < e:
                a[0] = env.action_space.sample()
            #Get new state and reward from environment
            s1,r,d,_ = env.step(a[0])
            #Obtain the Q' values by feeding the new state through our network
            Q1 = sess.run(Qout,
                          feed_dict={inputs1:np.identity(16)[s1:s1+1]})
            #Obtain maxQ' and set our target value for chosen action.
            maxQ1 = np.max(Q1)
            targetQ = allQ
            targetQ[0,a[0]] = r + y*maxQ1
            #Train our network using target and predicted Q values
            _,W1 =...


I am working on the file "13. Reinforcement learning.ipynb".
The first task is:
Reinforcement Learning
Reinforcement learning is an area of machine learning inspired by behaviorist psychology, concerned with how software agents ought to take actions in an environment so as to maximize some notion of cumulative reward. (Source: Wikipedia)
Reinforcement learning provides the capacity for us not only to teach an artificial agent how to act, but to allow it to learn through it’s own interactions with an environment. By combining the complex representations that deep neural networks can learn with the goal-driven learning of an RL agent, computers have accomplished some amazing feats, like beating humans at over a dozen Atari games, and defeating the Go world champion.
RL algorithms must enable the agent to learn the correct pairings itself through the use of observations, rewards, and actions.
Typical aspects of a task that make it an RL problem are the following:
1. Different actions yield different rewards. For example, when looking for treasure in a maze, going left may lead to the treasure, whereas going right may lead to a pit of snakes.
2. Rewards are delayed over time. This just means that even if going left in the above example is the right things to do, we may not know it till later in the maze.
3. Reward for an action is conditional on the state of the environment. Continuing the maze example, going left may be ideal at a certain fork in the path, but not at others.
Multi-armed bandit
The simplest reinforcement learning problem is the multi-armed bandit. Essentially, there are $n$-many slot machines, each with a different fixed payout probability.
The goal is to discover the machine with the best payout, and maximize the returned reward by always choosing it.
<img src="images/slot.jpg" alt="" style="width: 400px;"/>
This question has been the subject of active research since the 1950s, and many variations have been studied.
A-B Testing
Traditional A-B testing can be thought of as a special case of the multi-armed bandit problem, in which we choose to pursue a strategy of pure...
Can you write Python code for it?

import numpy as np
import seaborn

class Bandit:
    def __init__(self):
        self.arm_values = np.random.normal(0,1,10)
        self.K = np.zeros(10)
        self.est_values = np.zeros(10)

    def get_reward(self,action):
        noise = np.random.normal(0,1)
        reward = self.arm_values[action] + noise
        return reward

    def choose_eps_greedy(self,epsilon):
        rand_num = np.random.random()
        if epsilon>rand_num:
            return np.random.randint(10)
        else:
            return np.argmax(self.est_values)

    def update_est(self,action,reward):
        self.K[action] += 1
        alpha = 1./self.K[action]
        self.est_values[action] += alpha * (reward - self.est_values[action])


I am working on the file "13. Scikit-Learn, Statsmodel/04. Scikit-Learn 패키지의 샘플 데이터 - classification용.ipynb".
The first task is:
#### fetch_lfw_pairs()

얼굴 이미지 Pair
동일 인물일 수도 있고 아닐 수도 있음
Can you write Python code for it?

from sklearn.datasets import fetch_lfw_pairs
lfw_pairs = fetch_lfw_pairs(resize=0.4)
print(lfw_pairs.DESCR)
print(lfw_pairs.keys())

N=2; M=5;
fig = plt.figure(figsize=(8,5))
plt.subplots_adjust(top=1, bottom=0, hspace=0.01, wspace=0.05)
klist = np.random.choice(range(len(lfw_pairs.data)), M)
for j in range(M):
    k = klist[j]
    ax1 = fig.add_subplot(N, M, j+1)
    ax1.imshow(lfw_pairs.pairs [k][0], cmap=plt.cm.bone);
    ax1.grid(False)
    ax1.xaxis.set_ticks([])
    ax1.yaxis.set_ticks([])
    plt.title(lfw_pairs.target_names[lfw_pairs.target[k]])
    ax2 = fig.add_subplot(N, M, j+1 + M)
    ax2.imshow(lfw_pairs.pairs [k][1], cmap=plt.cm.bone);
    ax2.grid(False)
    ax2.xaxis.set_ticks([])
    ax2.yaxis.set_ticks([])
plt.tight_layout()
plt.show()


I am working on the file "13. Scikit-Learn, Statsmodel/04. Scikit-Learn 패키지의 샘플 데이터 - classification용.ipynb".
The first task is:
Digits Handwriting Image
load_digits()

숫자 필기 이미지
Can you write Python code for it?

from sklearn.datasets import load_digits
digits = load_digits()
print(digits.DESCR)
print(digits.keys())

N=2; M=5;
fig = plt.figure(figsize=(10,5))
plt.subplots_adjust(top=1, bottom=0, hspace=0, wspace=0.05)
for i in range(N):
    for j in range(M):
        k = i*M+j
        ax = fig.add_subplot(N, M, k+1)
        ax.imshow(digits.images[k], cmap=plt.cm.bone, interpolation="none");
        ax.grid(False)
        ax.xaxis.set_ticks([])
        ax.yaxis.set_ticks([])
        plt.title(digits.target_names[k])
plt.tight_layout()
plt.show()


I am working on the file "13. Scikit-Learn, Statsmodel/04. Scikit-Learn 패키지의 샘플 데이터 - classification용.ipynb".
The first task is:
Labeled Faces in the Wild (LFW)
#### fetch_lfw_people()

유명인 얼굴 이미지

Parameters

funneled : boolean, optional, default: True

Download and use the funneled variant of the dataset.
resize : float, optional, default 0.5
Ratio used to resize the each face picture.
min_faces_per_person : int, optional, default None
The extracted dataset will only retain pictures of people that have at least min_faces_per_person different pictures.
color : boolean, optional, default False
Keep the 3 RGB channels instead of averaging them to a single gray level channel. If color is True the shape of the data has one more dimension than than the shape with color = False.
Can you write Python code for it?

from sklearn.datasets import fetch_lfw_people
lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
print(lfw_people.DESCR)
print(lfw_people.keys())

N=2; M=5;
fig = plt.figure(figsize=(8,5))
plt.subplots_adjust(top=1, bottom=0, hspace=0.1, wspace=0.05)
klist = np.random.choice(range(len(lfw_people.data)), N * M)
for i in range(N):
    for j in range(M):
        k = klist[i*M+j]
        ax = fig.add_subplot(N, M, i*M+j+1)
        ax.imshow(lfw_people.images[k], cmap=plt.cm.bone);
        ax.grid(False)
        ax.xaxis.set_ticks([])
        ax.yaxis.set_ticks([])
        plt.title(lfw_people.target_names[lfw_people.target[k]])
plt.tight_layout()
plt.show()


I am working on the file "13. Scikit-Learn, Statsmodel/04. Scikit-Learn 패키지의 샘플 데이터 - classification용.ipynb".
The first task is:
Olivetti faces
fetch_olivetti_faces()

얼굴 인식 이미지
Can you write Python code for it?

from sklearn.datasets import fetch_olivetti_faces
olivetti = fetch_olivetti_faces()
print(olivetti.DESCR)
print(olivetti.keys())

N=2; M=5;
fig = plt.figure(figsize=(8,5))
plt.subplots_adjust(top=1, bottom=0, hspace=0, wspace=0.05)
klist = np.random.choice(range(len(olivetti.data)), N * M)
for i in range(N):
    for j in range(M):
        k = klist[i*M+j]
        ax = fig.add_subplot(N, M, i*M+j+1)
        ax.imshow(olivetti.images[k], cmap=plt.cm.bone);
        ax.grid(False)
        ax.xaxis.set_ticks([])
        ax.yaxis.set_ticks([])
        plt.title(olivetti.target[k])
plt.tight_layout()
plt.show()


I am working on the file "13. Scikit-Learn, Statsmodel/04. Scikit-Learn 패키지의 샘플 데이터 - classification용.ipynb".
The first task is:
Scikit-Learn 패키지의 샘플 데이터 - classification용
Iris Dataset
load_iris()

https://en.wikipedia.org/wiki/Iris_flower_data_set
R.A Fisher의 붓꽃 분류 연구
관찰 자료
꽃받침 길이(Sepal Length)
꽃받침 폭(Sepal Width)
꽃잎 길이(Petal Length)
꽃잎 폭(Petal Width)
종
setosa
versicolor
virginica
Can you write Python code for it?

from sklearn.datasets import load_iris
iris = load_iris()
print(iris.DESCR)

df = pd.DataFrame(iris.data, columns=iris.feature_names)
sy = pd.Series(iris.target, dtype="category")
sy = sy.cat.rename_categories(iris.target_names)
df['species'] = sy
df.tail()

sns.pairplot(df, hue="species")
plt.show()


I am working on the file "13. Scikit-Learn, Statsmodel/04. Scikit-Learn 패키지의 샘플 데이터 - classification용.ipynb".
The first task is:
mldata.org repository
fetch_mldata()

http://mldata.org
public repository for machine learning data, supported by the PASCAL network
홈페이지에서 data name 을 검색 후 key로 이용

MNIST 숫자 필기인식 자료

https://en.wikipedia.org/wiki/MNIST_database
Mixed National Institute of Standards and Technology (MNIST) database
0-9 필기 숫자 이미지
28x28 pixel bounding box
anti-aliased, grayscale levels
60,000 training images and 10,000 testing images
Can you write Python code for it?

from sklearn.datasets.mldata import fetch_mldata
mnist = fetch_mldata('MNIST original')
mnist.keys()

N=2; M=5;
fig = plt.figure(figsize=(8,5))
plt.subplots_adjust(top=1, bottom=0, hspace=0, wspace=0.05)
klist = np.random.choice(range(len(mnist.data)), N * M)
for i in range(N):
    for j in range(M):
        k = klist[i*M+j]
        ax = fig.add_subplot(N, M, i*M+j+1)
        ax.imshow(mnist.data[k].reshape(28, 28), cmap=plt.cm.bone, interpolation="nearest");
        ax.grid(False)
        ax.xaxis.set_ticks([])
        ax.yaxis.set_ticks([])
        plt.title(mnist.target[k])
plt.tight_layout()
plt.show()


I am working on the file "13. Scikit-Learn, Statsmodel/04. Scikit-Learn 패키지의 샘플 데이터 - classification용.ipynb".
The first task is:
뉴스 그룹 텍스트
fetch_20newsgroups(): 20 News Groups text
Can you write Python code for it?

from sklearn.datasets import fetch_20newsgroups
newsgroups = fetch_20newsgroups(subset='all')
print(newsgroups.description)
print(newsgroups.keys())

from pprint import pprint
pprint(list(newsgroups.target_names))

print(newsgroups.data[1])
print("=" * 80)
print(newsgroups.target_names[newsgroups.target[1]])


I am working on the file "13/311 time series homework.ipynb".
The first task is:
According to your selection of data, how many cases were filed in March? How about May?
Can you write Python code for it?


I am working on the file "13/311 time series homework.ipynb".
The first task is:
Graph those same agencies on an annual basis - make it weekly. When do people like to complain? When does the NYPD have an odd number of complaints?
Can you write Python code for it?

ax = df[df['Agency Name'] == 'New York City Police Department'].resample('W')['Agency'].count().plot(figsize=(15,4), linewidth=3)
df[df['Agency Name'] == 'Department of Housing Preservation and Development'].resample('W')['Agency'].count().plot(color='lightblue', linewidth=2)
df[df['Agency Name'] == 'Department of Transportation'].resample('W')['Agency'].count().plot(color='purple', linewidth=2)
df[df['Agency Name'] == 'Department of Health and Mental Hygiene'].resample('W')['Agency'].count().plot(color='grey', linewidth=2)
df[df['Agency Name'] == 'Department of Parks and Recreation'].resample('W')['Agency'].count().plot(color='green', linewidth=2)
ax.set_xticklabels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
ax.set_title('When the Angencies Filed the Reports 2015')
ax.set_xlabel("""
              Red: New York City Police Department
              Blue: Department of Housing Preservation and Development
              Purple: Department of Transportation
              Grey: Department of Health and Mental Hygiene
              Green: Department of Parks and Recreation""")
plt.savefig("When the Angencies Filed the Reports 2015.svg", bbox_inches='tight')

ax = df_NYPD.resample('W')['Agency'].count().plot(kind='bar', figsize=(15,4))
ax.set_xticklabels([''])
ax.set_xlabel('January to December 2015')


I am working on the file "13/311 time series homework.ipynb".
The first task is:
I'd like to see all of the 311 complaints called in on April 1st.

Surprise! We couldn't do this in class, but it was just a limitation of our data set
Can you write Python code for it?


I am working on the file "13/311 time series homework.ipynb".
The first task is:
Looks like midnight is a little bit of an outlier. Why might that be? Take the 5 most common agencies and graph the times they file reports at (all day, not just midnight).
Can you write Python code for it?

df['Agency'].value_counts().head(5)

df['Agency Name'].value_counts().head(5)

df_NYPD = df[df['Agency Name'] == 'New York City Police Department']
df_NYPD.groupby(by=df_NYPD.index.hour)['Unique Key'].count().plot(kind='bar')

df_HPD = df[df['Agency Name'] == 'Department of Housing Preservation and Development']
df_HPD.groupby(by=df_HPD.index.hour)['Unique Key'].count().plot(kind='bar')

df_DOT = df[df['Agency Name'] == 'Department of Transportation']
df_DOT.groupby(by=df_DOT.index.hour)['Unique Key'].count().plot(kind='bar')

df_DPR = df[df['Agency Name'] == 'Department of Parks and Recreation']
df_DPR.groupby(by=df_DPR.index.hour)['Unique Key'].count().plot(kind='bar')

df_DOHMH = df[df['Agency Name'] == 'Department of Health and Mental Hygiene']
df_DOHMH.groupby(by=df_DOHMH.index.hour)['Unique Key'].count().plot(kind='bar')


real_midnight = df[(df.index.hour == 0) & (df.index.minute == 0) & (df.index.second == 0)]
len(real_midnight)

twelve_and_one_sec = df[(df.index.hour == 0) & (df.index.minute == 0) & (df.index.second == 1)]
len(twelve_and_one_sec)

twelve_o_one = df[(df.index.hour == 0) & (df.index.minute == 2) & (df.index.second == 0)]
len(twelve_o_one)

real_midnight['Agency'].value_counts()

df['Agency'].value_counts().head()


I am working on the file "13/311 time series homework.ipynb".
The first task is:
Make a horizontal bar graph of the top 5 most frequent complaint types.
Can you write Python code for it?


ax = df['Complaint Type'].value_counts().sort_values(ascending=True).tail(5).plot(kind='barh', figsize=(6,4), fontsize=9)
ax.set_title("Top 5 Most Frequent 311-Complaints in 2015")
ax.set_xlabel("Complaint Count")
ax.set_ylabel("Complaint Type")
plt.savefig("5 Most Frequent 311 Complaints in 2015.svg", bbox_inches='tight')
#http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html


I am working on the file "13/311 time series homework.ipynb".
The first task is:
Maybe the NYPD deals with different issues at different times? Check the most popular complaints in July and August vs the month of May. Also check the most common complaints for the Housing Preservation Bureau (HPD) in winter vs. summer.
Can you write Python code for it?

df['2015-08']['Complaint Type'].value_counts().head(3)

df['2015-07']['Complaint Type'].value_counts().head(3)

df['2015-05']['Complaint Type'].value_counts().head(3)


I am working on the file "13/311 time series homework.ipynb".
The first task is:
Noise complaints are a big deal. Use .str.contains to select noise complaints, and make an chart of when they show up annually. Then make a chart about when they show up every day (cyclic).
Can you write Python code for it?

df['Noise'] = df['Complaint Type'].str.contains('Noise')
df_noise = df[df['Noise'] == True]

ax = df_noise.resample('D')['Unique Key'].count().plot(kind='bar', figsize=(15,4))
ax.set_xticklabels('')
ax.set_title('Number of Noise Complaints per Day in 2015')
ax.set_ylabel('Noise Complaint Count')
ax.set_xlabel('From January - December 2015 ')

df_noise.groupby(by=df_noise.index.hour)['Unique Key'].count().plot(kind='bar', figsize=(12,6))


is_noise_complaints = df['Complaint Type'].str.contains("Noise")
ax = df[is_noise_complaints].resample('M').count().plot(y='Unique Key')
ax = df[is_noise_complaints].resample('D').count().plot(y='Unique Key')

#Using a noise df group by the hour

#noise_df.groupby(by=noise_df.index.hour)['Unique Key'].count.plot()


I am working on the file "13/311 time series homework.ipynb".
The first task is:
One of the hours has an odd number of complaints. What are the most common complaints at that hour, and what are the most common complaints the hour before and after?
Can you write Python code for it?

df.groupby(by=df.index.hour)['Complaint Type'].value_counts().head(2)

df_complaint_type_count_per_hour = df.groupby(by=df.index.hour)['Complaint Type'].value_counts()

Top_complaints_by_hour = pd.DataFrame(df_complaint_type_count_per_hour)

Top_complaints_by_hour['Complaint Type'][0].head(1)

Top_complaints_by_hour['Complaint Type'][1].head(1)

Top_complaints_by_hour['Complaint Type'][23].head(1)

#More Reading: http://pandas.pydata.org/pandas-docs/version/0.13.1/timeseries.html


I am working on the file "13/311 time series homework.ipynb".
The first task is:
So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.
Can you write Python code for it?

df.groupby(by=df.index.hour == 0)['Unique Key'].count()


#Wrong becuase we don't want every minute of every day
#midnight_df.resample('T')['Unique Key'].count().plot()

midnight_df.groupby(by=midnight_df.index.minute)['Unique Key'].count().plot()


I am working on the file "13/311 time series homework.ipynb".
The first task is:
What hour of the day are the most complaints? Graph a day of complaints.
Can you write Python code for it?

#df['created_datetime'].resample('H').count()

df.groupby(by=df.index.hour).count().plot(y='Unique Key')


I am working on the file "13/311 time series homework.ipynb".
The first task is:
What was the most popular type of complaint on April 1st?
What were the most popular three types of complaint on April 1st
Can you write Python code for it?


I am working on the file "13/311 time series homework.ipynb".
The first task is:
What week of the year has the most reports filed? How many? Graph the weekly complaints.
Can you write Python code for it?

df_week_count = df.resample('W')['Unique Key'].count()

df_week_count = pd.DataFrame(df_week_count)

df_week_count.sort_values(by='Unique Key', ascending=False).head(5)


I am working on the file "13/311 time series homework.ipynb".
The first task is:
Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
Can you write Python code for it?

df_summed_complaints = df['Borough'].value_counts()
summed_complaints = pd.DataFrame(df_summed_complaints)
summed_complaints.reset_index(inplace=True)
summed_complaints.columns = ['Borough', 'Complaint Count']

Borough_head_count = pd.read_csv("NYC_Boroughs.csv")

summed_complaints_merged = summed_complaints.merge(Borough_head_count, left_on='Borough', right_on='borough name')

del summed_complaints_merged['borough name']

summed_complaints_merged

summed_complaints_merged['Per Capita'] = summed_complaints_merged['Total'] / summed_complaints_merged['Complaint Count']

summed_complaints_merged['Per Capita'].sort_values(ascending=False)
summed_complaints_merged[['Borough', 'Per Capita']]

Sorted_complaints = summed_complaints_merged.sort_values(by='Per Capita', ascending=False)

Sorted_complaints


I am working on the file "13/311 time series homework.ipynb".
The first task is:
Which were the top five days of the year for filing complaints? How many on each of those days? Graph it.
Can you write Python code for it?

df_day_count = df.resample('D')['Unique Key'].count()
df_day_count = pd.DataFrame(df_day_count)
ax = df_day_count.sort_values(by='Unique Key', ascending=True).tail(5).plot(kind='barh', legend=False)
ax.set_yticklabels(['10. Oktober', '6. August', '3. August', '25. September', '19. Oktober'])
ax.set_title('Top five complaint days in 2015')
ax.set_ylabel('')
ax.set_xlabel('Complaint Counts')
plt.savefig("Top 5 Complaint Days", bbox_inches='tight')


ax = df['created_datetime'].value_counts().head().plot(kind='barh')
ax.set_ylabel("Top 5 Days For Complaints")


I am working on the file "13/311 time series homework.ipynb".
The first task is:
df_NYPD = df[df['Agency Name'] == 'New York City Police Department']
df_NYPD.groupby(by=df_NYPD.index.hour)['Unique Key'].count().plot(kind='bar')
Dont understand why I can't change the labels here:
Can you write Python code for it?

ax = df[df['Agency Name'] == 'New York City Police Department'].groupby(by=df_NYPD.index.hour)['Unique Key'].count().plot(kind='bar', stacked=True, figsize=(15,6))
df[df['Agency Name'] == 'Department of Housing Preservation and Development'].groupby(by=df_HPD.index.hour)['Unique Key'].count().plot(kind='bar', ax=ax, stacked=True, color='lightblue')
df[df['Agency Name'] == 'Department of Transportation'].groupby(by=df_DOT.index.hour)['Unique Key'].count().plot(kind='bar', ax=ax, stacked=True, color='purple')
df[df['Agency Name'] == 'Department of Health and Mental Hygiene'].groupby(by=df_DOHMH.index.hour)['Unique Key'].count().plot(kind='bar', ax=ax, stacked=True, color='grey')
df[df['Agency Name'] == 'Department of Parks and Recreation'].groupby(by=df_DPR.index.hour)['Unique Key'].count().plot(kind='bar', ax=ax, stacked=True, color='green')
ax.set_title('Time of Day Agencies file complaints')
ax.set_ylabel('Complaint Count')
ax.set_xlabel("""
              Red: New York City Police Department
              Blue: Department of Housing Preservation and Development
              Purple: Department of Transportation
              Grey: Department of Health and Mental Hygiene
              Green: Department of Parks and Recreation""")
plt.savefig("Time of Day Agencies File Complaints.svg", bbox_inches='tight')


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
F1-score
Очевидный недостаток пары метрик precision-recall - в том, что их две: непонятно, как ранжировать алгоритмы. Чтобы этого избежать, используют F1-метрику, которая равна среднему гармоническому precision и recall.
F1-метрика будет равна 1, если и только если precision = 1 и recall = 1 (идеальный алгоритм).
(: Обмануть F1 сложно: если одна из величин маленькая, а другая близка к 1 (по графикам видно, что такое соотношение иногда легко получить), F1 будет далека от 1. F1-метрику сложно оптимизировать, потому что для этого нужно добиваться высокой полноты и точности одновременно.
Например, посчитаем F1 для того же набора векторов, для которого мы строили графики (мы помним, что там одна из кривых быстро выходит в единицу).
Can you write Python code for it?

from sklearn.metrics import f1_score

T = 0.5
print("Разные склонности алгоритмов к ошибкам FP и FN:")
for actual, predicted, descr in zip([actual_1, actual_10, actual_11],
                                    [predicted_1 > T, predicted_10 > T, predicted_11 > T],
                                    ["Typical:", "Avoids FP:", "Avoids FN:"]):
    print(descr, "f1 =", f1_score(actual, predicted))


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
F1-метрика в двух последних случаях, когда одна из парных метрик равна 1, значительно меньше, чем в первом, сбалансированном случае.
<font color="green" size=5>Practical task 2. </font> На precision и recall влияют и характер вектора вероятностей, и установленный порог.
Для тех же пар (actual, predicted), что и в предыдущей задаче, найдем оптимальные пороги, максимизирующие F1_score. Будем рассматривать только пороги вида T = 0.1 * k, k - целое; соответственно, нужно найти три значения k.
Can you write Python code for it?

def find_k(actual, probabilities):
    opt_k = None
    opt_f = None
    for k in range(10):
        T = k * 0.1
        predicted = (probabilities > T)
        f = f1_score(actual, predicted)
        if (opt_f is None) or (f > opt_f):
            opt_f = f
            opt_k = k
    return opt_k

k_1 = find_k(actual_1, predicted_1)
k_10 = find_k(actual_10, predicted_10)
k_11 = find_k(actual_11, predicted_11)

ks = [k_1, k_10, k_11]
many_scatters([actual_1, actual_10, actual_11], [predicted_1, predicted_10, predicted_11],
              np.array(ks)*0.1, ["Typical", "Avoids FP", "Avoids FN"], (1, 3))


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
Алгоритм может быть осторожным и стремиться сильно не отклонять вероятности от 0.5, а может рисковать - делать предсказания близакими к нулю или единице.
Can you write Python code for it?


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
Все три метрики легко различают простые случаи хороших и плохих алгоритмов. Обратим внимание, что метрики имеют область значений  [0, 1], и потому их легко интерпретировать.
Метрикам не важны величины вероятностей, им важно только то, сколько объектов неправильно зашли за установленную границу (в данном случае T = 0.5).
Метрика accuracy дает одинаковый вес ошибкам false positive и false negative, зато пара метрик precision и recall однозначно идентифицирует это различие. Собственно, их для того и используют, чтобы контролировать ошибки FP и FN.
Мы измерили три метрики, фиксировав порог T = 0.5, потому что для почти всех картинок он кажется оптимальным. Давайте посмотрим на последней (самой интересной для этих метрик) группе векторов, как меняются precision и recall при увеличении порога.
Can you write Python code for it?

from sklearn.metrics import precision_recall_curve

precs = []
recs = []
threshs = []
labels = ["Typical", "Avoids FP", "Avoids FN"]
for actual, predicted in zip([actual_1, actual_10, actual_11],
                                    [predicted_1, predicted_10, predicted_11]):
    prec, rec, thresh = precision_recall_curve(actual, predicted)
    precs.append(prec)
    recs.append(rec)
    threshs.append(thresh)
plt.figure(figsize=(15, 5))
for i in range(3):
    ax = plt.subplot(1, 3, i+1)
    plt.plot(threshs[i], precs[i][:-1], label="precision")
    plt.plot(threshs[i], recs[i][:-1], label="recall")
    plt.xlabel("threshold")
    ax.set_title(labels[i])
    plt.legend()


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
Идеальная ситуация: существует порог T, верно разделяющий вероятности, соответствующие двум классам. Пример такой ситуации:
Can you write Python code for it?


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
Мы описали разные характеры векторов вероятностей. Далее мы будем смотреть, как метрики оценивают разные векторы предсказаний, поэтому обязательно выполните ячейки, создающие векторы для визуализации.
Метрики, оценивающие бинарные векторы предсказаний
Есть две типичные ситуации, когда специалисты по машинному обучению начинают изучать характеристики метрик качества:
1. при участии в соревновании или решении прикладной задачи, когда вектор предсказаний оценивается по конкретной метрике, и нужно построить алгоритм, максимизирующий эту метрику.
1. на этапе формализации задачи машинного обучения, когда есть требования прикладной области, и нужно предложить математическую метрику, которая будет соответствовать этим требованиям.
Далее мы вкратце рассмотрим каждую метрику с этих двух позиций.
Precision и recall; accuracy
Для начала разберемся с метриками, оценивающие качество уже после бинаризации по порогу T, то есть сравнивающие два бинарных вектора: actual и predicted.
Две популярные метрики - precision и recall. Первая показывает, как часто алгоритм предсказывает класс 1 и оказывается правым, а вторая - как много объектов класса 1 алгоритм нашел.
Также рассмотрим самую простую и известную метрику - accuracy; она показывает долю правильных ответов.
Выясним преимущества и недостатки этих метрик, попробовав их на разных векторах вероятностей.
Can you write Python code for it?

from sklearn.metrics import precision_score, recall_score, accuracy_score

T = 0.5
print("Алгоритмы, разные по качеству:")
for actual, predicted, descr in zip([actual_0, actual_1, actual_2],
                                    [predicted_0 > T, predicted_1 > T, predicted_2 > T],
                                    ["Perfect:", "Typical:", "Awful:"]):
    print(descr, "precision =", precision_score(actual, predicted), "recall =", \
        recall_score(actual, predicted), ";",\
        "accuracy =", accuracy_score(actual, predicted))
print()
print("Осторожный и рискующий алгоритмы:")
for actual, predicted, descr in zip([actual_1, actual_1r],
                                    [predicted_1 > T, predicted_1r > T],
                                    ["Typical careful:", "Typical risky:"]):
    print(descr, "precision =", precision_score(actual, predicted), "recall =", \
        recall_score(actual, predicted), ";",\
        "accuracy =", accuracy_score(actual, predicted))
print()
print("Разные склонности алгоритмов к ошибкам FP и FN:")
for actual, predicted, descr in zip([actual_10, actual_11],
                                    [predicted_10 > T, predicted_11 > T],
                                    ["Avoids FP:", "Avoids FN:"]):
    print(descr, "precision =", precision_score(actual, predicted), "recall =", \
        recall_score(actual, predicted), ";",\
        "accuracy =", accuracy_score(actual, predicted))


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
При увеличении порога мы делаем меньше ошибок FP и больше ошибок FN, поэтому одна из кривых растет, а вторая  - падает. По такому графику можно подобрать оптимальное значение порога, при котором precision и recall будут приемлемы. Если такого порога не нашлось, нужно обучать другой алгоритм.
Оговоримся, что приемлемые значения precision и recall определяются предметной областью. Например, в задаче определения, болен ли пациент определенной болезнью (0 - здоров, 1 - болен), ошибок false negative стараются избегать, требуя recall около 0.9. Можно сказать человеку, что он болен, и при дальнейшей диагностике выявить ошибку; гораздо хуже пропустить наличие болезни.
<font color="green" size=5>Practical task 1. </font> Фиксируем порог T = 0.65; по графикам можно примерно узнать, чему равны метрики на трех выбранных парах векторов (actual, predicted).
Вычисляем точные precision и recall для этих трех пар векторов.
Can you write Python code for it?

T = 0.65
precisions = {}
recalls = {}
for actual, predicted, name, num in zip([actual_1, actual_10, actual_11],
                             [predicted_1 > T, predicted_10 > T, predicted_11 > T],
                             ['typical:', 'avoids FP:', 'avoids FN:'],
                             [1, 10, 11]):
    precisions[num] = precision_score(actual, predicted)
    recalls[num] = recall_score(actual, predicted)
    print(name, 'precision = ', precisions[num], 'recall = ', recalls[num])


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
Также интервалы могут смещаться. Если алгоритм боится ошибок false positive, то он будет чаще делать предсказания, близкие к нулю.
Аналогично, чтобы избежать ошибок false negative, логично чаще предсказывать большие вероятности.
Can you write Python code for it?


I am working on the file "13/Classif_Metrics_practic.ipynb".
The first task is:
Чем больше объектов в выборке, тем более гладкой выглядит кривая (хотя на самом деле она все равно ступенчатая).
Как и ожидалось, кривые всех идеальных алгоритмов проходят через левый верхний угол. На первом графике также показана типичная ROC-кривая (обычно на практике они не доходят до "идеального" угла).
AUC рискующего алгоритма значительном меньше, чем у осторожного, хотя осторожный и рискущий идеальные алгоритмы не различаются по ROC или AUC. Поэтому стремиться увеличить зазор между интервалами вероятностей классов смысла не имеет.
Наблюдается перекос кривой в случае, когда алгоритму свойственны ошибки FP или FN. Однако по величине AUC это отследить невозможно (кривые могут быть симметричны относительно диагонали (0, 1)-(1, 0)).
После того, как кривая построена, удобно выбирать порог бинаризации, в котором будет достигнут компромисс между FP или FN. Порог соответствует точке на кривой. Если мы хотим избежать ошибок FP, нужно выбирать точку на левой стороне квадрата (как можно выше), если FN - точку на верхней стороне квадрата (как можно левее). Все промежуточные точки будут соответствовать разным пропорциям FP и FN.
<font color="green" size=5>Practical task 4. </font> На каждой кривой найдем точку, которая ближе всего к левому верхнему углу (ближе в смысле обычного евклидова расстояния), этой точке соответствует некоторый порог бинаризации.
Пояснение: функция roc_curve возвращает три значения: FPR (массив абсции точек ROC-кривой), TPR (массив ординат точек ROC-кривой) и thresholds (массив порогов, соответствующих точкам).
Рекомендуем отрисовывать найденную точку на графике с помощью функции plt.scatter.
Can you write Python code for it?

def get_distance(fpr, tpr):
    return ((fpr) ** 2 + (tpr - 1) ** 2) ** 0.5

def get_opt_thresh(actual, predicted, name):
    fprs, tprs, thrs = roc_curve(actual, predicted)

    fpr_opt = None
    tpr_opt = None
    distance_opt = None
    thr_opt = None
    for i in range(len(thrs)):

        dist = get_distance(fprs[i], tprs[i])
        if (distance_opt is None) or (dist < distance_opt):
            distance_opt = dist
            fpr_opt = fprs[i]
            tpr_opt = tprs[i]
            thr_opt = thrs[i]
    plt.figure(figsize=(5, 5))
    plt.plot(fprs, tprs)
    plt.xlabel('fpr')
    plt.ylabel('tpr')
    plt.title(name)
    plt.xlim((0, 1))
    plt.ylim((0, 1))
    plt.scatter([fpr_opt], [tpr_opt], color = 'red')
    plt.show()
    return thr_opt

T_0 = get_opt_thresh(actual_0, predicted_0, 'Perfect')
T_1 = get_opt_thresh(actual_1, predicted_1, 'Typical')
T_2 = get_opt_thresh(actual_2, predicted_2, 'Awful')
T_0r = get_opt_thresh(actual_0r, predicted_0r, 'Ideal Risky')
T_1r = get_opt_thresh(actual_1r, predicted_1r, 'Typical Risky')
T_10 = get_opt_thresh(actual_10, predicted_10, 'Avoids FP')
T_11 = get_opt_thresh(actual_11, predicted_11, 'Avoids FN')


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
First, I made a mistake naming the data set! It's 2015 data, not 2014 data. But yes, still use 311-2014.csv. You can rename it.

Importing and preparing your data
Import your data, but only the first 200,000 rows. You'll also want to change the index to be a datetime based on the Created Date column - you'll want to check if it's already a datetime, and parse it if not.
Can you write Python code for it?

#df = pd.read_csv('311-2010-2016.csv')
# We select a list of columns for a better efficiency
columns_list = ['Unique Key', 'Created Date', 'Closed Date', 'Agency', 'Agency Name',
       'Complaint Type', 'Descriptor', 'Borough']
df = pd.read_csv('311-2015.csv', nrows=200000, usecols= columns_list)

df['Created Date'].describe()

def parse_date(str_date):
    return dateutil.parser.parse(str_date)

df['created_datetime'] = df['Created Date'].apply(parse_date)
df.index = df['created_datetime']
df.head(2)


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
Graph those same agencies on an annual basis - make it weekly. When do people like to complain? When does the NYPD have an odd number of complaints?
Can you write Python code for it?

for agency in common_agencies:
    df_agency = df[df['Agency Name'] == agency]
    wdata = df_agency.groupby([lambda x: x.week]).size()
    wdata.plot(legend=True, figsize=(15,8))
plt.legend(common_agencies)
print('')


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
I'd like to see all of the 311 complaints called in on April 1st.

Surprise! We couldn't do this in class, but it was just a limitation of our data set
Can you write Python code for it?


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
Looks like midnight is a little bit of an outlier. Why might that be? Take the 5 most common agencies and graph the times they file reports at (all day, not just midnight).
Can you write Python code for it?

common_agencies = list(df['Agency Name'].value_counts().head(5).keys())
for agency in common_agencies:
    df_agency = df[df['Agency Name'] == agency]
    hdata = df_agency.groupby([lambda x: x.hour]).size()
    hdata.plot(legend=True, figsize=(12,5))
    plt.xticks(hours, hours_str)
plt.legend(common_agencies)
print('')


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
Maybe the NYPD deals with different issues at different times? Check the most popular complaints in July and August vs the month of May. Also check the most common complaints for the Housing Preservation Bureau (HPD) in winter vs. summer.
Can you write Python code for it?

nypd = common_agencies[0]
hpd = common_agencies[1]

def complaints_for_agency(agency):
    return df[df['Agency Name'] == agency]

print("NYPD July-August:\n" + str(complaints_for_agency(nypd)['2015-07':'2015-08']['Complaint Type'].value_counts().head(10)))
print("\nNYPD May:\n" + str(complaints_for_agency(nypd)['2015-05']['Complaint Type'].value_counts().head(10)))

print("\nHPD Winter:\n" + str(complaints_for_agency(hpd)['2015-06-21':'2015-09-20']['Complaint Type'].value_counts().head(10)))
print("\nHPD Summer:\n" + str(complaints_for_agency(hpd)['2015-01-01':'2015-03-21']['Complaint Type'].value_counts().head(10)))


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
One of the hours has an odd number of complaints. What are the most common complaints at that hour, and what are the most common complaints the hour before and after?
Can you write Python code for it?

# The odd number is midnight (very high peak)

type_per_h = pd.DataFrame(df.groupby([lambda x: x.hour])['Complaint Type'].value_counts())

print("0h, Top Counts of", type_per_h['Complaint Type'][0].head(3))

print("\n23h, Top Counts of", type_per_h['Complaint Type'][23].head(3))

print("\n01h, Top Counts of", type_per_h['Complaint Type'][1].head(3))


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
What hour of the day are the most complaints? Graph a day of complaints.
Can you write Python code for it?

hdata = df.groupby([lambda x: x.hour]).size()
print(hdata.idxmax(), "hour has the most reports filed, with", hdata.max(), "reports filed.")

hdata.plot(title="A day of complaints", figsize=(12,5))
plt.xticks(hours, hours_str)
print('')


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
What was the most popular type of complaint on April 1st?
What were the most popular three types of complaint on April 1st
Can you write Python code for it?

print("The most popular three types of complaint on April 1st were:")
c = 1
for value, count in april_1['Complaint Type'].value_counts().iteritems(): # zip type
    if c > 3:
        break
    print("{}) {} ({} complaints)".format(c, value, count))
    c = c + 1


I am working on the file "13/Homework13_Ronga.ipynb".
The first task is:
What was the most popular type of complaint, and how many times was it filed?
Can you write Python code for it?

top_five_complaints = []
complaints_count = df['Complaint Type'].value_counts().head(5)
for complaint_type, count in complaints_count.items():
    top_five_complaints.append({"type": complaint_type, "count": count})

print("The most popular type of complaint is", top_five_complaints[0]['type'], "and it was filed", top_five_complaints[0]['count'], "times.")


I am working on the file "13/TF-IDF Homework.ipynb".
The first task is:
Doing our analysis
Use the sklearn package and a plain boring CountVectorizer to get a list of all of the tokens used in the speeches. If it won't list them all, that's ok! Make a dataframe with those terms as columns.
Be sure to include English-language stopwords
Can you write Python code for it?

from sklearn.feature_extraction.text import CountVectorizer
count_vectorizer = CountVectorizer(max_features=100, stop_words='english')
from sklearn.feature_extraction.text import TfidfVectorizer
import re
from nltk.stem.porter import PorterStemmer

X = count_vectorizer.fit_transform(speeches_df['content'])

X.toarray()


I am working on the file "13/TF-IDF Homework.ipynb".
The first task is:
If I'm searching for China and trade, what are the top 3 speeches to read according to the CountVectoriser?
Can you write Python code for it?

chinatrade_df = pd.DataFrame([tophundred_df['china'] + tophundred_df['trade']], index=["China + trade"]).T

chinatrade_df['China + trade'].sort_values(ascending=False).head(3)


I am working on the file "13/TF-IDF Homework.ipynb".
The first task is:
In class we had the texts variable. For the homework can just do speeches_df['content'] to get the same sort of list of stuff.
Take a look at the contents of the first 5 speeches
Can you write Python code for it?


I am working on the file "13/TF-IDF Homework.ipynb".
The first task is:
Now what if I'm using a TfidfVectorizer?
Can you write Python code for it?

porter_stemmer = PorterStemmer()

def stemming_tokenizer(str_input):
    words = re.sub(r"[^A-Za-z0-9\-]", " ", str_input).lower().split()
    words = [porter_stemmer.stem(word) for word in words]
    return words

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer(stop_words='english', tokenizer=stemming_tokenizer, use_idf=False, norm='l1', max_features = 100)
X = tfidf_vectorizer.fit_transform(speeches_df['content'])
chinatrade_tfidfpd = pd.DataFrame(X.toarray(), columns=tfidf_vectorizer.get_feature_names())

chinatrade_tfidfpd = pd.DataFrame([tophundred_df['china'] + tophundred_df['trade']], index=["China + trade"]).T

# chinatrade_tfidfpd

chinatrade_tfidfpd['China + trade'].sort_values(ascending=False).head(3)


I am working on the file "13/TF-IDF Homework.ipynb".
The first task is:
So great, we have 702 of them. Now let's import them.
Can you write Python code for it?

speeches = []
for path in paths:
    with open(path) as speech_file:
        speech = {
            'pathname': path,
            'filename': path.split('/')[-1],
            'content': speech_file.read()
        }
    speeches.append(speech)
speeches_df = pd.DataFrame(speeches)
speeches_df.head()


I am working on the file "13/TF-IDF Homework.ipynb".
The first task is:
What is the index of the speech thank is the most thankful, a.k.a. includes the word 'thank' the most times?
Can you write Python code for it?

tophundred_df['thank'].sort_values(ascending=False).head(1) # thank is not in the top 100 words unless you remove stop words


I am working on the file "13/TF-IDF Homework.ipynb".
The first task is:
What's the content of the speeches? Here's a way to get them:
Can you write Python code for it?

# index 0 is the first speech, which was the first one imported.
paths[0]

# Pass that into 'cat' using { } which lets you put variables in shell commands
# that way you can pass the path to cat
!cat {paths[0]}


I am working on the file "13/TF-IDF Homework.ipynb".
The first task is:
Which one do you think works the best?
Harry Potter time
I have a scraped collection of Harry Potter fanfiction at https://github.com/ledeprogram/courses/raw/master/algorithms/data/hp.zip.
I want you to read them in, vectorize them and cluster them. Use this process to find out the two types of Harry Potter fanfiction. What is your hypothesis?
Can you write Python code for it?


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
After the convolution we do a max-pooling which is also described in Tutorial #02.
Can you write Python code for it?

net = tf.layers.max_pooling2d(inputs=net, pool_size=2, strides=2)


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Classification Accuracy
We need to calculate the classification accuracy so we can report progress to the user.
First we create a vector of booleans telling us whether the predicted class equals the true class of each image.
Can you write Python code for it?

correct_prediction = tf.equal(y_pred_cls, y_true_cls)


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Copy some of the data-dimensions for convenience.
Can you write Python code for it?

# The number of pixels in each dimension of an image.
img_size = data.img_size

# The images are stored in one-dimensional arrays of this length.
img_size_flat = data.img_size_flat

# Tuple with height and width of images used to reshape arrays.
img_shape = data.img_shape

# Number of classes, one class for each of 10 digits.
num_classes = data.num_classes

# Number of colour channels for the images: 1 channel for gray-scale.
num_channels = data.num_channels


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Final output layer
Now find the image for the 2nd feature of the final output of the neural network. That is, we want to find an image that makes the neural network classify that image as the digit 2. This is the image that the neural network likes to see the most for the digit 2.
Can you write Python code for it?

image = optimize_image(conv_id=None, feature=2,
                       num_iterations=10, show_progress=True)


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
First Convolutional Layer
These are the input images that maximize the features in the first convolutional layer, so these are the images that it likes to see.
Can you write Python code for it?


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Function used to plot 10 images in a 2x5 grid.
Can you write Python code for it?

def plot_images10(images, smooth=True):
    # Interpolation type.
    if smooth:
        interpolation = 'spline16'
    else:
        interpolation = 'nearest'

    # Create figure with sub-plots.
    fig, axes = plt.subplots(2, 5)

    # Adjust vertical spacing.
    fig.subplots_adjust(hspace=0.1, wspace=0.1)

    # For each entry in the grid.
    for i, ax in enumerate(axes.flat):
        # Get the i'th image and only use the desired pixels.
        img = images[i, :, :]

        # Plot the image.
        ax.imshow(img, interpolation=interpolation, cmap='binary')

        # Remove ticks.
        ax.set_xticks([])
        ax.set_yticks([])

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Function used to plot a single image.
Can you write Python code for it?

def plot_image(image):
    plt.imshow(image, interpolation='nearest', cmap='binary')
    plt.xticks([])
    plt.yticks([])


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Helper-function for finding the input image
This function finds the input image that maximizes a given feature in the network. It essentially just performs optimization with gradient ascent. The image is initialized with small random values and is then iteratively updated using the gradient for the given feature with regard to the image.
Can you write Python code for it?

def optimize_image(conv_id=None, feature=0,
                   num_iterations=30, show_progress=True):
    """
    Find an image that maximizes the feature
    given by the conv_id and feature number.

    Parameters:
    conv_id: Integer identifying the convolutional layer to
             maximize. It is an index into conv_names.
             If None then use the last fully-connected layer
             before the softmax output.
    feature: Index into the layer for the feature to maximize.
    num_iteration: Number of optimization iterations to perform.
    show_progress: Boolean whether to show the progress.
    """

    # Create the loss-function that must be maximized.
    if conv_id is None:
        # If we want to maximize a feature on the last layer,
        # then we use the fully-connected layer prior to the
        # softmax-classifier. The feature no. is the class-number
        # and must be an integer between 1 and 1000.
        # The loss-function is just the value of that feature.
        loss = tf.reduce_mean(logits[:, feature])
    else:
        # If instead we want to maximize a feature of a
        # convolutional layer inside the neural network.

        # Get the name of the convolutional operator.
        conv_name = conv_names[conv_id]

        # Get the default TensorFlow graph.
        graph = tf.get_default_graph()

        # Get a reference to the tensor that is output by the
        # operator. Note that ":0" is added to the name for this.
        tensor = graph.get_tensor_by_name(conv_name + ":0")

        # The loss-function is the average of all the
        # tensor-values for the given feature. This
        # ensures that we generate the whole input image.
        # You can try and modify this so it only uses
        # a part of the tensor.
        loss = tf.reduce_mean(tensor[:,:,:,feature])

    # Get the gradient for the loss-function with regard to
    # the input image. This creates a mathematical
    # function for calculating the gradient.
    gradient =...


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Helper-function for showing the performance
Below is a function for printing the classification accuracy on the test-set.
It takes a while to compute the classification for all the images in the test-set, that's why the results are re-used by calling the above functions directly from this function, so the classifications don't have to be recalculated by each function.
Note that this function can use a lot of computer memory, which is why the test-set is split into smaller batches. If you have little RAM in your computer and it crashes, then you can try and lower the batch-size.
Can you write Python code for it?

# Split the test-set into smaller batches of this size.
test_batch_size = 256

def print_test_accuracy(show_example_errors=False,
                        show_confusion_matrix=False):

    # Number of images in the test-set.
    num_test = data.num_test

    # Allocate an array for the predicted classes which
    # will be calculated in batches and filled into this array.
    cls_pred = np.zeros(shape=num_test, dtype=np.int)

    # Now calculate the predicted classes for the batches.
    # We will just iterate through all the batches.
    # There might be a more clever and Pythonic way of doing this.

    # The starting index for the next batch is denoted i.
    i = 0

    while i < num_test:
        # The ending index for the next batch is denoted j.
        j = min(i + test_batch_size, num_test)

        # Get the images from the test-set between index i and j.
        images = data.x_test[i:j, :]

        # Get the associated labels.
        labels = data.y_test[i:j, :]

        # Create a feed-dict with these images and labels.
        feed_dict = {x: images,
                     y_true: labels}

        # Calculate the predicted class using TensorFlow.
        cls_pred[i:j] = session.run(y_pred_cls, feed_dict=feed_dict)

        # Set the start-index for the next batch to the
        # end-index of the current batch.
        i = j

    # Convenience variable for the true class-numbers of the test-set.
    cls_true = data.y_test_cls

    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    correct_sum = correct.sum()

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
    print(msg.format(acc, correct_sum, num_test))

   ...


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Helper-function to perform optimization iterations
There are 55,000 images in the training-set. It takes a long time to calculate the gradient of the model using all these images. We therefore only use a small batch of images in each iteration of the optimizer.
If your computer crashes or becomes very slow because you run out of RAM, then you may try and lower this number, but you may then need to do more optimization iterations.
Can you write Python code for it?


I am working on the file "13B_Visual_Analysis_MNIST.ipynb".
The first task is:
Helper-function to plot confusion matrix
Can you write Python code for it?

def plot_confusion_matrix(cls_pred):
    # This is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # Get the true classifications for the test-set.
    cls_true = data.test.cls

    # Get the confusion matrix using sklearn.
    cm = confusion_matrix(y_true=cls_true,
                          y_pred=cls_pred)

    # Print the confusion matrix as text.
    print(cm)

    # Plot the confusion matrix as an image.
    plt.matshow(cm)

    # Make various adjustments to the plot.
    plt.colorbar()
    tick_marks = np.arange(num_classes)
    plt.xticks(tick_marks, range(num_classes))
    plt.yticks(tick_marks, range(num_classes))
    plt.xlabel('Predicted')
    plt.ylabel('True')

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()