スタンフォード大学/CS131/宿題0 PARTⅠ

その買うを、もっとハッピーに。|ハピタス

暇なので、このサイトのStanford Univesity/CS131をやってみようと思う。

スポンサーリンク

git clone

先ずは上述のサイトをgit cloneする。

cd git
/home/workspace/git
!git clone https://github.com/mikucy/CS131.git
Cloning into 'CS131'...
remote: Enumerating objects: 1511, done.
remote: Total 1511 (delta 0), reused 0 (delta 0), pack-reused 1511
Receiving objects: 100% (1511/1511), 80.40 MiB | 1.42 MiB/s, done.
Resolving deltas: 100% (115/115), done.
cd CS131
/home/workspace/git/CS131
ls
README.md     hw1_release/  hw3_release/  hw5_release/  hw7_release/
hw0_release/  hw2_release/  hw4_release/  hw6_release/  hw8_release/

取り敢えず、homework0からスタートすることにする。

cd hw0_release
/home/workspace/git/CS131/hw0_release
ls
hw0.ipynb    image2.jpg*    imageManip.pyc  linalg.pyc
image1.jpg*  imageManip.py  linalg.py       requirements.txt

hw0.ipynbを新しいタブで開いて、これを見ながらチュートリアルを進めていく。

スポンサーリンク

Homework 0

In this homework, we will go through basic linear algebra and image manipulation using python to get everyone on the same page for the prerequisite skills for this class.「この宿題の中で、パイソンを使って線形代数と画像加工の基礎を学ぶ。」

#Imports the print function from newer versions of python
from __future__ import print_function

#Setup

# The Random module for implements pseudo-random number generators
import random 

# Numpy is the main package for scientific computing with Python. 
# This will be one of our most used libraries in this class
import numpy as np

#Imports all the methods in each of the files: linalg.py and imageManip.py
from linalg import *
from imageManip import *

#Matplotlib is a useful plotting library for python 
import matplotlib.pyplot as plt
# This code is to make matplotlib figures appear inline in the
# notebook rather than in a new window.
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
%reload_ext autoreload
スポンサーリンク

Question 1: Linear Algebra Review

Please implement all the required methods in linalg.py.「linalg.pyに必要なメソッドを全て実装してちょ。」

Question 1.1

Define the following using numpy:「numpyで以下を表わせ」

$M = \begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
10 & 11 & 12 \end{bmatrix}$

$a = \begin{bmatrix}
1 & 1 & 0
\end{bmatrix}$

$b = \begin{bmatrix}
-1 \\ 2 \\ 5
\end{bmatrix}$

### YOUR CODE HERE
M = np.arange(1, 13).reshape(4, 3)
a = np.array([1, 1, 0])
b = np.array([[-1], [2], [5]])
### END CODE HERE
print("M = \n", M)
print("a = ", a)
print("b = ", b)
M = 
 [[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
a =  1
b =  [[-1]
 [ 2]
 [ 5]]

Question 1.2

Implement the dot_product method in linalg.py and check that it returns the correct answer for $a^Tb$.「linalg.pyにdot_productメソッドを実装して、$a^Tb$に対して正しい値を返すかチェックする。」

#答えはnumpyのdot関数を使う。
aDotB = dot_product(a, b)
print (aDotB)
2

Question 1.3

Implement the matrix_mult method in linalg.py and use it to compute $(a^T b)Ma$.「matrix_multメソッドをlinalg.pyに実装し、それを使って$(a^T b)Ma$を計算する。」

#以下の答えと同じになる必要がある。
answer = np.dot(a,b)*M.dot(a)
print (answer)
[ 3  9 15 21]
ans = matrix_mult(M, a, b)
print (ans)
[ 3  9 15 21]

Question 1.4

Implement the get_singular_values method. In this method, perform singular value decomposition on the input matrix and return the largest n singular values (n specified in the method calls below).「get_singular_valuesメソッドを実装する。このメソッドでは、入力行列に対して特異値分解を行い、最大n特異値(下のメソッドコールで指定されているn)を返す。」

#この場合のnは、それぞれ1と2
print(get_singular_values(M, 1))
print(get_singular_values(M, 2))
[25.46240744]
[25.46240744  1.29066168]

Question 1.5

Implement the get_eigen_values_and_vectors method. In this method, perform eigen value decomposition on the following matrix and return the largest n eigen values and corresponding eigen vectors (n specified in the method calls below).「get_eigen_values_and_vectorsメソッドを実装する。このメソッドでは、下記の行列に対して固有値分解を行い、最大n固有値(下のメソッド呼び出しで指定されているn)と対応固有ベクトルを返す。」

$$M = \begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \end{bmatrix}
$$

M = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
val, vec = get_eigen_values_and_vectors(M[:,:3], 1)
print("Values = \n", val)
print("Vectors = \n", vec)
val, vec = get_eigen_values_and_vectors(M[:,:3], 2)
print("Values = \n", val)
print("Vectors = \n", vec)
Values = 
 [16.11684397]
Vectors = 
 [[-0.23197069 -0.78583024  0.40824829]]
Values = 
 [16.11684397 -1.11684397]
Vectors = 
 [[-0.23197069 -0.78583024  0.40824829]
 [-0.52532209 -0.08675134 -0.81649658]]

うーん、宿題0にしては結構難しい・・・。今日はこれぐらいにしておこう。

スポンサーリンク
スポンサーリンク