update week 35
This commit is contained in:
@@ -1326,7 +1326,7 @@ Xscaled = scaler.transform(X)
|
||||
display(XPandas-Xscaled)
|
||||
</pre></div>
|
||||
<p>
|
||||
Small exercise: perform the standars scaling by including the standard deviation.
|
||||
Small exercise: perform the standard scaling by including the standard deviation.
|
||||
</section>
|
||||
|
||||
|
||||
@@ -1394,13 +1394,13 @@ TrainError = np.zeros(maxdegree)
|
||||
polydegree = np.zeros(maxdegree)
|
||||
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=<span style="color: #B452CD">0.2</span>)
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
scaler.fit(x_train)
|
||||
x_train_scaled = scaler.transform(x_train)
|
||||
x_test_scaled = scaler.transform(x_test)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> degree <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(maxdegree):
|
||||
model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=<span style="color: #8B008B; font-weight: bold">False</span>))
|
||||
clf = model.fit(x_train_scale,y_train)
|
||||
clf = model.fit(x_train_scaled,y_train)
|
||||
y_fit = clf.predict(x_train_scaled)
|
||||
y_pred = clf.predict(x_test_scaled)
|
||||
polydegree[degree] = degree
|
||||
@@ -1719,7 +1719,7 @@ As an example, the above defective matrix can be decomposed as
|
||||
|
||||
<p> <br>
|
||||
$$
|
||||
\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 2& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
|
||||
\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 4& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
|
||||
$$
|
||||
<p> <br>
|
||||
|
||||
@@ -1788,31 +1788,28 @@ In general the economy-size SVD leads to less FLOPS and still conserving the des
|
||||
<span style="color: #CD5555"> SVD is numerically more stable than the inversion algorithms provided by</span>
|
||||
<span style="color: #CD5555"> numpy and scipy.linalg at the cost of being slower.</span>
|
||||
<span style="color: #CD5555"> '''</span>
|
||||
U, s, VT = np.linalg.svd(A)
|
||||
<span style="color: #228B22"># print('test U')</span>
|
||||
<span style="color: #228B22"># print( (np.transpose(U) @ U - U @np.transpose(U)))</span>
|
||||
<span style="color: #228B22"># print('test VT')</span>
|
||||
<span style="color: #228B22"># print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))</span>
|
||||
U, S, VT = np.linalg.svd(A)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'test U'</span>)
|
||||
<span style="color: #658b00">print</span>( (np.transpose(U) @ U - U <span style="color: #707a7c">@np</span>.transpose(U)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'test VT'</span>)
|
||||
<span style="color: #658b00">print</span>( (np.transpose(VT) @ VT - VT <span style="color: #707a7c">@np</span>.transpose(VT)))
|
||||
<span style="color: #658b00">print</span>(U)
|
||||
<span style="color: #658b00">print</span>(s)
|
||||
<span style="color: #658b00">print</span>(S)
|
||||
<span style="color: #658b00">print</span>(VT)
|
||||
|
||||
D = np.zeros((<span style="color: #658b00">len</span>(U),<span style="color: #658b00">len</span>(VT)))
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>,<span style="color: #658b00">len</span>(VT)):
|
||||
D[i,i]=s[i]
|
||||
UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> np.matmul(V,np.matmul(invD,UT))
|
||||
D[i,i]=S[i]
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> U @ D @ VT
|
||||
|
||||
|
||||
X = np.array([ [<span style="color: #B452CD">1.0</span>, -<span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">2.0</span>], [<span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">0.0</span>, <span style="color: #B452CD">1.0</span>], [<span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">2.0</span>, -<span style="color: #B452CD">1.0</span>], [<span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">0.0</span>] ])
|
||||
X = np.array([ [<span style="color: #B452CD">1.0</span>,-<span style="color: #B452CD">1.0</span>], [<span style="color: #B452CD">1.0</span>,-<span style="color: #B452CD">1.0</span>]])
|
||||
<span style="color: #658b00">print</span>(X)
|
||||
A = np.transpose(X) @ X
|
||||
<span style="color: #658b00">print</span>(A)
|
||||
<span style="color: #228B22"># Brute force inversion of super-collinear matrix</span>
|
||||
<span style="color: #228B22">#B = np.linalg.inv(A)</span>
|
||||
<span style="color: #228B22">#print(B)</span>
|
||||
C = SVDinv(A)
|
||||
<span style="color: #658b00">print</span>(C)
|
||||
<span style="color: #228B22"># Print the difference between the original matrix and the SVD one</span>
|
||||
<span style="color: #658b00">print</span>(C-A)
|
||||
</pre></div>
|
||||
<p>
|
||||
The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first
|
||||
|
||||
@@ -1421,7 +1421,7 @@ Xscaled = scaler.transform(X)
|
||||
display(XPandas-Xscaled)
|
||||
</pre></div>
|
||||
<p>
|
||||
Small exercise: perform the standars scaling by including the standard deviation.
|
||||
Small exercise: perform the standard scaling by including the standard deviation.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -1487,13 +1487,13 @@ TrainError = np.zeros(maxdegree)
|
||||
polydegree = np.zeros(maxdegree)
|
||||
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=<span style="color: #B452CD">0.2</span>)
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
scaler.fit(x_train)
|
||||
x_train_scaled = scaler.transform(x_train)
|
||||
x_test_scaled = scaler.transform(x_test)
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> degree <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(maxdegree):
|
||||
model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=<span style="color: #8B008B; font-weight: bold">False</span>))
|
||||
clf = model.fit(x_train_scale,y_train)
|
||||
clf = model.fit(x_train_scaled,y_train)
|
||||
y_fit = clf.predict(x_train_scaled)
|
||||
y_pred = clf.predict(x_test_scaled)
|
||||
polydegree[degree] = degree
|
||||
@@ -1794,7 +1794,7 @@ $$
|
||||
As an example, the above defective matrix can be decomposed as
|
||||
|
||||
$$
|
||||
\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 2& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
|
||||
\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 4& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
|
||||
$$
|
||||
|
||||
<p>
|
||||
@@ -1862,31 +1862,28 @@ In general the economy-size SVD leads to less FLOPS and still conserving the des
|
||||
<span style="color: #CD5555"> SVD is numerically more stable than the inversion algorithms provided by</span>
|
||||
<span style="color: #CD5555"> numpy and scipy.linalg at the cost of being slower.</span>
|
||||
<span style="color: #CD5555"> '''</span>
|
||||
U, s, VT = np.linalg.svd(A)
|
||||
<span style="color: #228B22"># print('test U')</span>
|
||||
<span style="color: #228B22"># print( (np.transpose(U) @ U - U @np.transpose(U)))</span>
|
||||
<span style="color: #228B22"># print('test VT')</span>
|
||||
<span style="color: #228B22"># print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))</span>
|
||||
U, S, VT = np.linalg.svd(A)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'test U'</span>)
|
||||
<span style="color: #658b00">print</span>( (np.transpose(U) @ U - U <span style="color: #707a7c">@np</span>.transpose(U)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">'test VT'</span>)
|
||||
<span style="color: #658b00">print</span>( (np.transpose(VT) @ VT - VT <span style="color: #707a7c">@np</span>.transpose(VT)))
|
||||
<span style="color: #658b00">print</span>(U)
|
||||
<span style="color: #658b00">print</span>(s)
|
||||
<span style="color: #658b00">print</span>(S)
|
||||
<span style="color: #658b00">print</span>(VT)
|
||||
|
||||
D = np.zeros((<span style="color: #658b00">len</span>(U),<span style="color: #658b00">len</span>(VT)))
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">0</span>,<span style="color: #658b00">len</span>(VT)):
|
||||
D[i,i]=s[i]
|
||||
UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> np.matmul(V,np.matmul(invD,UT))
|
||||
D[i,i]=S[i]
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> U @ D @ VT
|
||||
|
||||
|
||||
X = np.array([ [<span style="color: #B452CD">1.0</span>, -<span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">2.0</span>], [<span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">0.0</span>, <span style="color: #B452CD">1.0</span>], [<span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">2.0</span>, -<span style="color: #B452CD">1.0</span>], [<span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">1.0</span>, <span style="color: #B452CD">0.0</span>] ])
|
||||
X = np.array([ [<span style="color: #B452CD">1.0</span>,-<span style="color: #B452CD">1.0</span>], [<span style="color: #B452CD">1.0</span>,-<span style="color: #B452CD">1.0</span>]])
|
||||
<span style="color: #658b00">print</span>(X)
|
||||
A = np.transpose(X) @ X
|
||||
<span style="color: #658b00">print</span>(A)
|
||||
<span style="color: #228B22"># Brute force inversion of super-collinear matrix</span>
|
||||
<span style="color: #228B22">#B = np.linalg.inv(A)</span>
|
||||
<span style="color: #228B22">#print(B)</span>
|
||||
C = SVDinv(A)
|
||||
<span style="color: #658b00">print</span>(C)
|
||||
<span style="color: #228B22"># Print the difference between the original matrix and the SVD one</span>
|
||||
<span style="color: #658b00">print</span>(C-A)
|
||||
</pre></div>
|
||||
<p>
|
||||
The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first
|
||||
|
||||
@@ -1426,7 +1426,7 @@ Xscaled <span style="color: #666666">=</span> scaler<span style="color: #666666"
|
||||
display(XPandas<span style="color: #666666">-</span>Xscaled)
|
||||
</pre></div>
|
||||
<p>
|
||||
Small exercise: perform the standars scaling by including the standard deviation.
|
||||
Small exercise: perform the standard scaling by including the standard deviation.
|
||||
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -1492,13 +1492,13 @@ TrainError <span style="color: #666666">=</span> np<span style="color: #666666">
|
||||
polydegree <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(maxdegree)
|
||||
x_train, x_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(x, y, test_size<span style="color: #666666">=0.2</span>)
|
||||
scaler <span style="color: #666666">=</span> StandardScaler()
|
||||
scaler<span style="color: #666666">.</span>fit(X_train)
|
||||
scaler<span style="color: #666666">.</span>fit(x_train)
|
||||
x_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(x_train)
|
||||
x_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(x_test)
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> degree <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(maxdegree):
|
||||
model <span style="color: #666666">=</span> make_pipeline(PolynomialFeatures(degree<span style="color: #666666">=</span>degree), LinearRegression(fit_intercept<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">False</span>))
|
||||
clf <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(x_train_scale,y_train)
|
||||
clf <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(x_train_scaled,y_train)
|
||||
y_fit <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(x_train_scaled)
|
||||
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(x_test_scaled)
|
||||
polydegree[degree] <span style="color: #666666">=</span> degree
|
||||
@@ -1799,7 +1799,7 @@ $$
|
||||
As an example, the above defective matrix can be decomposed as
|
||||
|
||||
$$
|
||||
\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 2& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
|
||||
\boldsymbol{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 4& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\boldsymbol{U}\boldsymbol{\Sigma}\boldsymbol{V}^T,
|
||||
$$
|
||||
|
||||
<p>
|
||||
@@ -1867,31 +1867,28 @@ In general the economy-size SVD leads to less FLOPS and still conserving the des
|
||||
<span style="color: #BA2121; font-style: italic"> SVD is numerically more stable than the inversion algorithms provided by</span>
|
||||
<span style="color: #BA2121; font-style: italic"> numpy and scipy.linalg at the cost of being slower.</span>
|
||||
<span style="color: #BA2121; font-style: italic"> '''</span>
|
||||
U, s, VT <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>svd(A)
|
||||
<span style="color: #408080; font-style: italic"># print('test U')</span>
|
||||
<span style="color: #408080; font-style: italic"># print( (np.transpose(U) @ U - U @np.transpose(U)))</span>
|
||||
<span style="color: #408080; font-style: italic"># print('test VT')</span>
|
||||
<span style="color: #408080; font-style: italic"># print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))</span>
|
||||
U, S, VT <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>svd(A)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'test U'</span>)
|
||||
<span style="color: #008000">print</span>( (np<span style="color: #666666">.</span>transpose(U) <span style="color: #666666">@</span> U <span style="color: #666666">-</span> U <span style="color: #AA22FF">@np</span><span style="color: #666666">.</span>transpose(U)))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">'test VT'</span>)
|
||||
<span style="color: #008000">print</span>( (np<span style="color: #666666">.</span>transpose(VT) <span style="color: #666666">@</span> VT <span style="color: #666666">-</span> VT <span style="color: #AA22FF">@np</span><span style="color: #666666">.</span>transpose(VT)))
|
||||
<span style="color: #008000">print</span>(U)
|
||||
<span style="color: #008000">print</span>(s)
|
||||
<span style="color: #008000">print</span>(S)
|
||||
<span style="color: #008000">print</span>(VT)
|
||||
|
||||
D <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros((<span style="color: #008000">len</span>(U),<span style="color: #008000">len</span>(VT)))
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">0</span>,<span style="color: #008000">len</span>(VT)):
|
||||
D[i,i]<span style="color: #666666">=</span>s[i]
|
||||
UT <span style="color: #666666">=</span> np<span style="color: #666666">.</span>transpose(U); V <span style="color: #666666">=</span> np<span style="color: #666666">.</span>transpose(VT); invD <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>inv(D)
|
||||
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>matmul(V,np<span style="color: #666666">.</span>matmul(invD,UT))
|
||||
D[i,i]<span style="color: #666666">=</span>S[i]
|
||||
<span style="color: #008000; font-weight: bold">return</span> U <span style="color: #666666">@</span> D <span style="color: #666666">@</span> VT
|
||||
|
||||
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([ [<span style="color: #666666">1.0</span>, <span style="color: #666666">-1.0</span>, <span style="color: #666666">2.0</span>], [<span style="color: #666666">1.0</span>, <span style="color: #666666">0.0</span>, <span style="color: #666666">1.0</span>], [<span style="color: #666666">1.0</span>, <span style="color: #666666">2.0</span>, <span style="color: #666666">-1.0</span>], [<span style="color: #666666">1.0</span>, <span style="color: #666666">1.0</span>, <span style="color: #666666">0.0</span>] ])
|
||||
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([ [<span style="color: #666666">1.0</span>,<span style="color: #666666">-1.0</span>], [<span style="color: #666666">1.0</span>,<span style="color: #666666">-1.0</span>]])
|
||||
<span style="color: #008000">print</span>(X)
|
||||
A <span style="color: #666666">=</span> np<span style="color: #666666">.</span>transpose(X) <span style="color: #666666">@</span> X
|
||||
<span style="color: #008000">print</span>(A)
|
||||
<span style="color: #408080; font-style: italic"># Brute force inversion of super-collinear matrix</span>
|
||||
<span style="color: #408080; font-style: italic">#B = np.linalg.inv(A)</span>
|
||||
<span style="color: #408080; font-style: italic">#print(B)</span>
|
||||
C <span style="color: #666666">=</span> SVDinv(A)
|
||||
<span style="color: #008000">print</span>(C)
|
||||
<span style="color: #408080; font-style: italic"># Print the difference between the original matrix and the SVD one</span>
|
||||
<span style="color: #008000">print</span>(C<span style="color: #666666">-</span>A)
|
||||
</pre></div>
|
||||
<p>
|
||||
The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first
|
||||
|
||||
Binary file not shown.
@@ -1631,7 +1631,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Small exercise: perform the standars scaling by including the standard deviation.\n",
|
||||
"Small exercise: perform the standard scaling by including the standard deviation.\n",
|
||||
"\n",
|
||||
"## Min-Max Scaling\n",
|
||||
"\n",
|
||||
@@ -1720,13 +1720,13 @@
|
||||
"polydegree = np.zeros(maxdegree)\n",
|
||||
"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)\n",
|
||||
"scaler = StandardScaler()\n",
|
||||
"scaler.fit(X_train)\n",
|
||||
"scaler.fit(x_train)\n",
|
||||
"x_train_scaled = scaler.transform(x_train)\n",
|
||||
"x_test_scaled = scaler.transform(x_test)\n",
|
||||
"\n",
|
||||
"for degree in range(maxdegree):\n",
|
||||
" model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))\n",
|
||||
" clf = model.fit(x_train_scale,y_train)\n",
|
||||
" clf = model.fit(x_train_scaled,y_train)\n",
|
||||
" y_fit = clf.predict(x_train_scaled)\n",
|
||||
" y_pred = clf.predict(x_test_scaled) \n",
|
||||
" polydegree[degree] = degree\n",
|
||||
@@ -2118,7 +2118,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"$$\n",
|
||||
"\\boldsymbol{X} = \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1& 1 \\\\ 1& -1\\\\ \\end{bmatrix} \\begin{bmatrix} 2& 0 \\\\ 0& 0\\\\ \\end{bmatrix} \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1& -1 \\\\ 1& 1\\\\ \\end{bmatrix}=\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T,\n",
|
||||
"\\boldsymbol{X} = \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1& 1 \\\\ 1& -1\\\\ \\end{bmatrix} \\begin{bmatrix} 4& 0 \\\\ 0& 0\\\\ \\end{bmatrix} \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1& -1 \\\\ 1& 1\\\\ \\end{bmatrix}=\\boldsymbol{U}\\boldsymbol{\\Sigma}\\boldsymbol{V}^T,\n",
|
||||
"$$"
|
||||
]
|
||||
},
|
||||
@@ -2185,31 +2185,28 @@
|
||||
" SVD is numerically more stable than the inversion algorithms provided by\n",
|
||||
" numpy and scipy.linalg at the cost of being slower.\n",
|
||||
" '''\n",
|
||||
" U, s, VT = np.linalg.svd(A)\n",
|
||||
"# print('test U')\n",
|
||||
"# print( (np.transpose(U) @ U - U @np.transpose(U)))\n",
|
||||
"# print('test VT')\n",
|
||||
"# print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))\n",
|
||||
" U, S, VT = np.linalg.svd(A)\n",
|
||||
" print('test U')\n",
|
||||
" print( (np.transpose(U) @ U - U @np.transpose(U)))\n",
|
||||
" print('test VT')\n",
|
||||
" print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))\n",
|
||||
" print(U)\n",
|
||||
" print(s)\n",
|
||||
" print(S)\n",
|
||||
" print(VT)\n",
|
||||
"\n",
|
||||
" D = np.zeros((len(U),len(VT)))\n",
|
||||
" for i in range(0,len(VT)):\n",
|
||||
" D[i,i]=s[i]\n",
|
||||
" UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)\n",
|
||||
" return np.matmul(V,np.matmul(invD,UT))\n",
|
||||
" D[i,i]=S[i]\n",
|
||||
" return U @ D @ VT\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"X = np.array([ [1.0, -1.0, 2.0], [1.0, 0.0, 1.0], [1.0, 2.0, -1.0], [1.0, 1.0, 0.0] ])\n",
|
||||
"X = np.array([ [1.0,-1.0], [1.0,-1.0]])\n",
|
||||
"print(X)\n",
|
||||
"A = np.transpose(X) @ X\n",
|
||||
"print(A)\n",
|
||||
"# Brute force inversion of super-collinear matrix\n",
|
||||
"#B = np.linalg.inv(A)\n",
|
||||
"#print(B)\n",
|
||||
"C = SVDinv(A)\n",
|
||||
"print(C)"
|
||||
"# Print the difference between the original matrix and the SVD one\n",
|
||||
"print(C-A)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -973,7 +973,7 @@ Xscaled = scaler.transform(X)
|
||||
display(XPandas-Xscaled)
|
||||
!ec
|
||||
|
||||
Small exercise: perform the standars scaling by including the standard deviation.
|
||||
Small exercise: perform the standard scaling by including the standard deviation.
|
||||
|
||||
!split
|
||||
===== Min-Max Scaling =====
|
||||
@@ -1030,13 +1030,13 @@ TrainError = np.zeros(maxdegree)
|
||||
polydegree = np.zeros(maxdegree)
|
||||
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
scaler.fit(x_train)
|
||||
x_train_scaled = scaler.transform(x_train)
|
||||
x_test_scaled = scaler.transform(x_test)
|
||||
|
||||
for degree in range(maxdegree):
|
||||
model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
|
||||
clf = model.fit(x_train_scale,y_train)
|
||||
clf = model.fit(x_train_scaled,y_train)
|
||||
y_fit = clf.predict(x_train_scaled)
|
||||
y_pred = clf.predict(x_test_scaled)
|
||||
polydegree[degree] = degree
|
||||
@@ -1322,7 +1322,7 @@ As an example, the above defective matrix can be decomposed as
|
||||
|
||||
!bt
|
||||
\[
|
||||
\bm{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 2& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\bm{U}\bm{\Sigma}\bm{V}^T,
|
||||
\bm{X} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1& 1 \\ 1& -1\\ \end{bmatrix} \begin{bmatrix} 4& 0 \\ 0& 0\\ \end{bmatrix} \frac{1}{\sqrt{2}}\begin{bmatrix} 1& -1 \\ 1& 1\\ \end{bmatrix}=\bm{U}\bm{\Sigma}\bm{V}^T,
|
||||
\]
|
||||
!et
|
||||
|
||||
@@ -1378,32 +1378,28 @@ def SVDinv(A):
|
||||
SVD is numerically more stable than the inversion algorithms provided by
|
||||
numpy and scipy.linalg at the cost of being slower.
|
||||
'''
|
||||
U, s, VT = np.linalg.svd(A)
|
||||
# print('test U')
|
||||
# print( (np.transpose(U) @ U - U @np.transpose(U)))
|
||||
# print('test VT')
|
||||
# print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
|
||||
U, S, VT = np.linalg.svd(A)
|
||||
print('test U')
|
||||
print( (np.transpose(U) @ U - U @np.transpose(U)))
|
||||
print('test VT')
|
||||
print( (np.transpose(VT) @ VT - VT @np.transpose(VT)))
|
||||
print(U)
|
||||
print(s)
|
||||
print(S)
|
||||
print(VT)
|
||||
|
||||
D = np.zeros((len(U),len(VT)))
|
||||
for i in range(0,len(VT)):
|
||||
D[i,i]=s[i]
|
||||
UT = np.transpose(U); V = np.transpose(VT); invD = np.linalg.inv(D)
|
||||
return np.matmul(V,np.matmul(invD,UT))
|
||||
D[i,i]=S[i]
|
||||
return U @ D @ VT
|
||||
|
||||
|
||||
X = np.array([ [1.0, -1.0, 2.0], [1.0, 0.0, 1.0], [1.0, 2.0, -1.0], [1.0, 1.0, 0.0] ])
|
||||
X = np.array([ [1.0,-1.0], [1.0,-1.0]])
|
||||
print(X)
|
||||
A = np.transpose(X) @ X
|
||||
print(A)
|
||||
# Brute force inversion of super-collinear matrix
|
||||
#B = np.linalg.inv(A)
|
||||
#print(B)
|
||||
C = SVDinv(A)
|
||||
print(C)
|
||||
|
||||
# Print the difference between the original matrix and the SVD one
|
||||
print(C-A)
|
||||
!ec
|
||||
|
||||
The matrix $\bm{X}$ has columns that are linearly dependent. The first
|
||||
|
||||
Reference in New Issue
Block a user