COMPGI21讲解、辅导Python语言程序、讲解Jupyter、Python设计辅导

- 首页 >> Python编程

This is the first assignment for the course Introduction to Machine Learning (COMPGI21). The goal of this assignment is

to learn how to visualize functions using the Python language, along with the numpy and matplotlib packages. This

assignment takes the form of a Jupyter Notebook, a convenient way to combine formatted text, interactive coding and

plotting.

Function visualization in Python (0.4)

Consider the following function:

where

To visualize this function in the domain we will sample its values with a step of 0.01 units, which

gives us a matrix with values:

Question 1 (.0) Try to understand how the visualization function works and in particular how the matplotlib functions

imshow, plot_surface and contour are used. Run the two examples given. Do the next questions using these

examples as a guideline.

Question 2 (.2) Compute this matrix using a nested for-loop iterating over the values in X1 and X2.

Question 3 (.2) Compute this matrix again, this time applying the arithmetic operations directly to the numpy arrays

X1 and X2. Notice that this is both more efficient and requires fewer lines of code.

Question 4 (.0) For classification we care a lot about the zero set of the function, where the function flips from being

positive to being negative. Go back to the cells of questions 2 and 3 and tweak the parameters of the visualization

function to also plot the zero set of .

Visualizing a classifier's decision boundary (0.6)

Building on the previous assignment, consider now the following basic problem discussed in class: you have a two-class

classification problem. The prior probabilities of the two classes are and , while for both

classes the class-conditional probability density function is of the following Gaussian form:

where the parameters for the two classes are considered to be the following:

Question 5 (.2) Define a function that calculates this class-conditional probability density function, and adapt your

code from the previous assignment to compute the isocontours of the distributions of the two classes given, at the

values of and .

Question 6 (.2) Plot the decision boundary for the posterior-based classifier using Bayes' rule:

First, define a function computing the posterior probability for class 1 (reusing code from the previous question). Then, as

in the previous examples, plot the isocontour of this function at , which is where the decision

outcome changes.

Question 7 (.2) Consider now that and repeat. What do you observe?

f(x1, x2; w) = w0 + w1 x1 + w2 x2 + w3x  + 2

π0 = 0.3 π1 = 1π0 = 0.7

P(X = x|C = c) = exp

12πσc1 σc2(x1 μc1)

22(σc1 )2(x2 μc2)

22(σc2 )2C

C= 0 : μ = ?0.2, = 0.8, = 1.0, = 0.2 0

= 1 : μ = 0.7, = ?0.8, = 0.5, = 0.6 1

0.1 0.2

P(C = 1|X = x) = P(X = x|C = 1)P(C = 1)

∑ P(X = x|C = c)P(C = c) c∈{0,1}

P(C = 1|X = x) = 0.5

π0 = 0.1

Code

Imports

In[1]: import os

import time

import matplotlib.pyplot as plt

import numpy as np

import scipy.io as spio

from mpl_toolkits.mplot3d import Axes3D

%matplotlib inline

Visualization function

In[2]: def visualization_func(grid_x, grid_y, function_values, contour_values=[

0.5]):

# Create the matplotlib figure

fig = plt.figure(figsize=plt.figaspect(0.3))

# Add the first subplot, known as an 'Axes' object in matplotlib

ax = fig.add_subplot(1, 3, 1)

# Produce an image plot of the values. By default, values are interp

olated

# between points to produce a smooth visual.

ax.imshow(function_values, extent=[-1, 1, -1, 1], origin='lower')

ax.set_title('Image plot of function values, clipped between [-1,1]'

)


ax = fig.add_subplot(1, 3, 2, projection='3d')

# Produce another plot of these values, this time as a surface in 3D

space.

surf = ax.plot_surface(grid_x, grid_y, function_values, rstride=1, c

stride=1,

linewidth=0, antialiased=False)

ax.set_zlim3d(-1, 1)

ax.set_title('Mesh plot of function values')


ax = fig.add_subplot(1, 3, 3)

# Plot some contour lines of our function. Again, these are automati

cally

# interpolated from the values.

CS = ax.contour(grid_x, grid_y, function_values, contour_values,

cmap=plt.cm.winter)

ax.clabel(CS)

ax.set_title('Isocontours of function')


# Finally, show the figure.

plt.show()

Sample points using meshgrid

In?[3]: num_of_points = 201

# Linearly sample the [-1,1] interval with the given number of points.

# The retstep option returns the step as well as the values.

x1, step = np.linspace(-1.0, 1.0, num=num_of_points, retstep=True)

# Print the step size to confirm it is correct

print('Step size is : {}'.format(step))

x2 = np.linspace(-1.0, 1.0, num=num_of_points)

# Create a rectangular grid out of the x1 and x2 values.

# Each point of this grid is a 2D point with coordinates (X1[i, j], X2[i

, j]).

# See https://stackoverflow.com/a/36014586 for an illustration.

X1, X2 = np.meshgrid(x1, x2, sparse=False)

Example 1

Generate values for f(x1, x2) = 0.1 ? x + ? .5 2

1 √|

 ̄x

 ̄ ̄2 ̄|

In[4]: function_values = 0.1 * X1**2 + np.sqrt(np.abs(X2)) - 0.5

visualization_func(X1, X2, function_values, contour_values=[0.0, 0.4])

Example 2

Generate values for f(x1, x2) = sin(2 ? pi ? x1) ? (x ? ? ) 2

2 x1 x2

In[5]: function_values = np.sin(2*np.pi*X1) * (X2**2 - X1*X2)

visualization_func(X1, X2, function_values, contour_values=[0.0, 0.5])

Step size is : 0.01

Matrix computation

Compute function values using a nested for-loop

In[6]: w = [-1, 0, 0, 4, 2, 0]

rows, cols = X1.shape

function_values = np.zeros((rows, cols))

t = time.process_time()

## TODO (Question 2)

## /TODO

elapsed = time.process_time() - t

print(elapsed, ' seconds have passed using a nested for-loop')


## TODO (Question 4: tweak parameters to plot the zero set)

## /TODO

Compute using numpy operations

0.11579582299999913 seconds have passed using a nested for-loop

In?[7]: t = time.process_time()

## TODO (Question 3)

## /TODO

elapsed = time.process_time() - t

print(elapsed, ' seconds have passed using numpy operations')

## TODO (Question 4: tweak parameters to plot the zero set)

## /TODO

Decision boundary of a classifier

Probability function

In[8]: def probability_func(X1, X2, m_1, m_2, sigma_1, sigma_2):

## TODO (Question 5)

## /TODO


return output

Visualize the distribution of the first class

0.002937408000001085 seconds have passed using numpy operations

In?[9]: m0_1 = -0.2

m0_2 = 0.8

sigma0_1 = 1

sigma0_2 = 0.2

## TODO (Question 5)

## /TODO

Visualize the distribution of the second class

In[10]: m1_1 = 0.7

m1_2 = -0.8

sigma1_1 = 0.5

sigma1_2 = 0.6

## TODO (Question 5)

## /TODO

Decision boundary function

In[11]: def decision_boundary_function(X1, X2, prior0):

## TODO (Question 6)


# /TODO


return posterior1

Visualize the decision boundary for π0 = 0.3

In?[12]: ## TODO (Question 6)

## /TODO

Visualize the decision boundary for π0 = 0.1

In[13]: ## TODO (Question 7)

## /TODO


站长地图