<jupyter_start><jupyter_text>3.2 좌표와 변환 - 랭크<jupyter_code>X1 = np.array([[1, 3], [2, 4]])
np.linalg.matrix_rank(X1)
X2 = np.array([[1, 3, 5], [2, 3, 7]])
np.linalg.matrix_rank(X2)<jupyter_output><empty_output><jupyter_text>연습문제 3.2.5<jupyter_code>X1 = np.array([[1], [1]])
num1=X1@X1.T
np.linalg.matrix_rank(num1)
X1 = np.array([[1,1], [1,-1]])
num2=X1@X1.T
np.linalg.matrix_rank(num2)<jupyter_output><empty_output><jupyter_text>이미지변환 3.2.73<jupyter_code>import scipy as sp
import scipy.misc
import scipy.ndimage
import matplotlib.pylab as plt
import matplotlib as mpl
red = {"facecolor": "red"}
green = {"facecolor": "green"}
blue = {"facecolor": "blue"}
f = sp.misc.face(gray=True)

e1 = np.array([0, 1]) # matplotlib의 이미지 좌표규약으로 부호변경 
e2 = np.array([1, 0])
E = np.vstack([e1, e2]).T
g1 = np.array([1, 1]) / np.sqrt(2)
g2 = np.array([-1, 1]) / np.sqrt(2)
A = np.vstack([g1, g2]).T
gc1 = E @ g1
gc2 = E @ g2

plt.subplot(121)
plt.imshow(f, cmap=mpl.cm.bone, alpha=0.9)
plt.annotate('', xy=500*e1, xytext=(0,0), arrowprops=green)
plt.annotate('$e_1$', xy=500*e1, xytext=500*e1 + [-100,0])
plt.annotate('', xy=500*e2, xytext=(0,0), arrowprops=green)
plt.annotate('$e_2$', xy=500*e2, xytext=500*e2 + [0, -50])
plt.annotate('', xy=500*gc1, xytext=(0, 0), arrowprops=red)
plt.annotate('$g_1$', xy=500*gc1, xytext=500*gc1 + [50, -50])
plt.annotate('', xy=500*gc2, xytext=(0, 0), arrowprops=red)
plt.annotate('$g_2$', xy=500*gc2, xytext=500*gc2 + [50, 0])
plt.axis("off")
plt.xlim(-200, 1000)
plt.ylim(800, -500)
plt.title("좌표변환전")

f1 = sp.ndimage.affine_transform(f, A)

plt.subplot(122)
plt.imshow(f1, cmap=mpl.cm.bone, alpha=0.8)
plt.annotate('', xy=500*e1, xytext=(0,0), arrowprops=red)
plt.annotate('$g_1$', xy=500*e1, xytext=500*e1 + [-100,0])
plt.annotate('', xy=500*e2, xytext=(0,0), arrowprops=red)
plt.annotate('$g_2$', xy=500*e2, xytext=500*e2 + [0, -50])
plt.axis("off")
plt.xlim(-200, 1000)
plt.ylim(800, -500)
plt.title("좌표변환후")
plt.show()<jupyter_output><empty_output><jupyter_text>연습 문제 3.2.9<jupyter_code>f = sp.misc.face(gray=True)

e1 = np.array([0, 1]) # matplotlib의 이미지 좌표규약으로 부호변경 
e2 = np.array([1, 0])
E = np.vstack([e1, e2]).T
g1 = np.array([1, 0.75])
g2 = np.array([-1, 0.75])
A = np.vstack([g1, g2]).T
gc1 = E @ g1
gc2 = E @ g2

plt.subplot(121)
plt.imshow(f, cmap=mpl.cm.bone, alpha=0.9)
plt.annotate('', xy=500*e1, xytext=(0,0), arrowprops=green)
plt.annotate('$e_1$', xy=500*e1, xytext=500*e1 + [-100,0])
plt.annotate('', xy=500*e2, xytext=(0,0), arrowprops=green)
plt.annotate('$e_2$', xy=500*e2, xytext=500*e2 + [0, -50])
plt.annotate('', xy=500*gc1, xytext=(0, 0), arrowprops=red)
plt.annotate('$g_1$', xy=500*gc1, xytext=500*gc1 + [50, -50])
plt.annotate('', xy=500*gc2, xytext=(0, 0), arrowprops=red)
plt.annotate('$g_2$', xy=500*gc2, xytext=500*gc2 + [50, 0])
plt.axis("off")
plt.xlim(-200, 1000)
plt.ylim(800, -500)
plt.title("좌표변환전")

f1 = sp.ndimage.affine_transform(f, A)

plt.subplot(122)
plt.imshow(f1, cmap=mpl.cm.bone, alpha=0.8)
plt.annotate('', xy=500*e1, xytext=(0,0), arrowprops=red)
plt.annotate('$g_1$', xy=500*e1, xytext=500*e1 + [-100,0])
plt.annotate('', xy=500*e2, xytext=(0,0), arrowprops=red)
plt.annotate('$g_2$', xy=500*e2, xytext=500*e2 + [0, -50])
plt.axis("off")
plt.xlim(-200, 1000)
plt.ylim(800, -500)
plt.title("좌표변환후")
plt.show()
A = np.array([[1, -2], [2, -3]])
w1, V1 = np.linalg.eig(A)

print(w1)
print(V1)
# 원래는 종복 고윳값이라 값이 하나가 나와야하는데 부동소수점 문제로 인해
# 차수만큼 다나옴
# v1은 열별로 아이겐 벡터를 봐야함
B = np.array([[2, 3], [2, 1]])
w2, V2 = np.linalg.eig(B)

print(w2)
print(V2)
C = np.array([[0, -1], [1, 0]])
w3, V3 = np.linalg.eig(C)

print(w3)
print(V3)<jupyter_output>[0.+1.j 0.-1.j]
[[0.70710678+0.j         0.70710678-0.j        ]
 [0.        -0.70710678j 0.        +0.70710678j]]<jupyter_text>연습문제 3.3.5<jupyter_code>E = np.array([[2, 3], [2, 1]])
w3, V3 = np.linalg.eig(E)

print(w3)
print(V3)
F = np.array([[1, 1], [0, 1]])
w3, V3 = np.linalg.eig(F)

print(w3)
print(V3)<jupyter_output>[1. 1.]
[[ 1.00000000e+00 -1.00000000e+00]
 [ 0.00000000e+00  2.22044605e-16]]<jupyter_text>예제 3.3.57<jupyter_code>B = np.array([[2, 3], [2, 1]])
w2, V2 = np.linalg.eig(B)

print(w2)
print(V2)
V2_inv = np.linalg.inv(V2)
V2_inv
# 아이겐벡터행렬 고윳값행렬 아이겐벡터역행렬 순으로 곱
# 원래의 B가 나온것을 확인가능
V2 @ np.diag(w2) @ V2_inv<jupyter_output><empty_output><jupyter_text>예제 3.3.73<jupyter_code>A = np.array([[60., 30., 20.],
              [30., 20., 15.],
              [20., 15., 12.]])

w, V = np.linalg.eig(A)
w1, w2, w3 = w
v1 = V[:, 0:1]
v2 = V[:, 1:2]
v3 = V[:, 2:3]
A1 = v1 @ v1.T
A2 = v2 @ v2.T
A3 = v3 @ v3.T
w
w1*A1
w2*A2
w3 * A3
w1 * A1 + w2 * A2 + w3 * A3<jupyter_output><empty_output><jupyter_text>연습문제 3.3.9<jupyter_code>#  1)
from sklearn.datasets import load_iris 
iris = load_iris() 
X = iris.data
cov_iris= X.T@X
w, V = np.linalg.eig(cov_iris)
w
#  2)
from sklearn.datasets import load_boston 
boston = load_boston() 
X = boston.data 
cov_boston=X.T@X
w, V = np.linalg.eig(cov_boston)
w<jupyter_output><empty_output><jupyter_text>파이썬을 사용한 특이분해-특이분해는 파이썬으로 해야함<jupyter_code>from numpy.linalg import svd

A = np.array([[3, -1], [1, 3], [1, 1]])
U, S, VT = svd(A)
U
S
np.diag(S,1)[:,1:]
VT
U @ np.diag(S,1)[:,1:] @ VT
# 축소형
U2, S2, VT2 = svd(A,full_matrices=False)
U2
S2
VT2
U2 @ np.diag(S2) @VT2
# 연습문제 3.4.1
B = np.array([[3, 2, 2], [2,3,-2]])
U, S, VT = svd(B)
U
S
VT
U @ np.diag(S, -1)[1:, :]  @VT
np.diag(S,-1)
U2, S2, VT2 = svd(B,full_matrices=False)
U2
S2
VT2
U2 @ np.diag(S2) @ VT2
# 연습 문제 3.4.2
B = np.array([[3, 2, 2], [2,3,-2]])
C = np.array([[2,4],[1,3],[0,0],[0,0]])
B, C
U, S, VT = svd(B,full_matrices=False)
U,np.diag(S)
B@VT.T
(np.diag(S)@ U.T).T<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.23745997865528282, 'fraction_numerical': 0.11650658128779794, 'mean_word_length': 4.737755102040817, 'pattern_counts': {'":': 3, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '415741', 'license': 'MIT', 'n_tokens_mistral': 3494, 'n_tokens_neox': 3362, 'n_words': 535, 'path': 'math/Day_3.ipynb', 'repo_name': 'Seoheesu1/TIL', 'size': '5622'}