<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 OrderExtend example with boat image
This note shows how the OrderExtend algorithm performs the matrix completion task on a partially observed image data.
First, read boat image file and display.
Step1:  Prepare the model.
We assume 30% of matrix is observed, and set the rank to 50.
Initial model is running with 100 iterations.
Step2:  Now we set the iteration number to 1000, and rerun the model.
Step3:  The model cannot solve any dimension of $x$-axis except the dimensions used in initialization, because the model decides that the any $r \times r$ submatrix of $y$ is highly unstable to solve following linear systems, even with the stabilize routine. In other words, the local condition number of initial matrix is greater than the threshold $\theta$. Now let's increase the threshold to 3.
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from model import OrderExtend
img = ndimage.imread('images/boat.jpeg', flatten=True)
img /= np.max(img) #normalize image [0,1]
plt.imshow(img, cmap = cm.Greys_r)
Explanation: OrderExtend example with boat image
This note shows how the OrderExtend algorithm performs the matrix completion task on a partially observed image data.
First, read boat image file and display.
End of explanation
nx,ny = img.shape
r = 50
p = 0.3
max_iter = 100
theta = 1
# mask of the observed pixels (randomly chosen)
sigma = np.random.binomial(1, p, size=img.shape)
model = OrderExtend(img, sigma, r, theta=theta)
order = model.init()
r_img, x, y = model.fit(order, max_iter=max_iter)
plt.imshow(r_img, cmap = cm.Greys_r)
print(np.max(r_img),np.min(r_img))
Explanation: Prepare the model.
We assume 30% of matrix is observed, and set the rank to 50.
Initial model is running with 100 iterations.
End of explanation
r_img, x, y = model.fit(order, max_iter=1000)
plt.imshow(r_img, cmap = cm.Greys_r)
print(np.max(r_img),np.min(r_img))
Explanation: Now we set the iteration number to 1000, and rerun the model.
End of explanation
#set the threshold to 3
theta = 3
nx,ny = img.shape
r = 50
p = 0.3
max_iter = 5000
# mask of the observed pixels (randomly chosen)
sigma = np.random.binomial(1, p, size=img.shape)
# initialize another model with new theta
model = OrderExtend(img, sigma, r, theta)
order = model.init()
r_img, x, y = model.fit(order, max_iter=max_iter)
plt.imshow(r_img, cmap = cm.Greys_r)
print(np.max(r_img), np.min(r_img))
Explanation: The model cannot solve any dimension of $x$-axis except the dimensions used in initialization, because the model decides that the any $r \times r$ submatrix of $y$ is highly unstable to solve following linear systems, even with the stabilize routine. In other words, the local condition number of initial matrix is greater than the threshold $\theta$. Now let's increase the threshold to 3.
End of explanation
<END_TASK>