From ed2dca03a804a20248a963c9b59e754fbdfa6479 Mon Sep 17 00:00:00 2001 From: Morten Hjorth-Jensen Date: Thu, 2 Sep 2021 09:13:17 +0200 Subject: [PATCH] update week 35 --- doc/pub/week35/html/week35-reveal.html | 33 ++++++++---------- doc/pub/week35/html/week35-solarized.html | 33 ++++++++---------- doc/pub/week35/html/week35.html | 33 ++++++++---------- doc/pub/week35/ipynb/ipynb-week35-src.tar.gz | Bin 191 -> 192 bytes doc/pub/week35/ipynb/week35.ipynb | 33 ++++++++---------- doc/src/week35/week35.do.txt | 34 ++++++++----------- 6 files changed, 75 insertions(+), 91 deletions(-) diff --git a/doc/pub/week35/html/week35-reveal.html b/doc/pub/week35/html/week35-reveal.html index f38705a0c..72dba7a8b 100644 --- a/doc/pub/week35/html/week35-reveal.html +++ b/doc/pub/week35/html/week35-reveal.html @@ -1326,7 +1326,7 @@ Xscaled = scaler.transform(X) display(XPandas-Xscaled)

-Small exercise: perform the standars scaling by including the standard deviation. +Small exercise: perform the standard scaling by including the standard deviation. @@ -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=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 @@ -1719,7 +1719,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, $$

 
@@ -1788,31 +1788,28 @@ In general the economy-size SVD leads to less FLOPS and still conserving the des 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)

The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first diff --git a/doc/pub/week35/html/week35-solarized.html b/doc/pub/week35/html/week35-solarized.html index d5b47d26b..7f77448c1 100644 --- a/doc/pub/week35/html/week35-solarized.html +++ b/doc/pub/week35/html/week35-solarized.html @@ -1421,7 +1421,7 @@ Xscaled = scaler.transform(X) display(XPandas-Xscaled)

-Small exercise: perform the standars scaling by including the standard deviation. +Small exercise: perform the standard scaling by including the standard deviation.











@@ -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=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 @@ -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, $$

@@ -1862,31 +1862,28 @@ In general the economy-size SVD leads to less FLOPS and still conserving the des 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)

The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first diff --git a/doc/pub/week35/html/week35.html b/doc/pub/week35/html/week35.html index 807f09764..5b00624c0 100644 --- a/doc/pub/week35/html/week35.html +++ b/doc/pub/week35/html/week35.html @@ -1426,7 +1426,7 @@ Xscaled = scaler-Xscaled)

-Small exercise: perform the standars scaling by including the standard deviation. +Small exercise: perform the standard scaling by including the standard deviation.











@@ -1492,13 +1492,13 @@ TrainError = np 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 @@ -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, $$

@@ -1867,31 +1867,28 @@ In general the economy-size SVD leads to less FLOPS and still conserving the des 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)

The matrix \( \boldsymbol{X} \) has columns that are linearly dependent. The first diff --git a/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz b/doc/pub/week35/ipynb/ipynb-week35-src.tar.gz index ae4e62b796a55bdcaf7bed8a1922c7a7534bb6e2..6fef7343b68fe044c340760e38c0ebd231fb8e19 100644 GIT binary patch delta 170 zcmV;b09F6L0l)zzABzY8et9rq00ZsM%?iRW3gilFUb3LJy17(ylk}{qqKxoWk764i9q?gV*VTMzh>Ws>wdN((WmF0&$ z^DFSoKXI(2h3&p~l~$m%!(8hcZiscvBiZ&UheD$rTVU|oNrNC%527fflUj*O*cyE@ YqOno<>t{UA^SrM;079N&^#BL}0G5SJ;Q#;t delta 169 zcmV;a09OCN0lxtyABzY8Kyff(00ZsM%?iRW38#^sIHjr1LRq2S%?)E^`C-re z3Ow^q94l#IyYF446)5dQu5}GJA?lb#vh7t4g%);fff3hE8UbAOz>7jUsTIG3tZ~%8=mB00;m8OMywP diff --git a/doc/pub/week35/ipynb/week35.ipynb b/doc/pub/week35/ipynb/week35.ipynb index 605aaf26d..97f67e309 100644 --- a/doc/pub/week35/ipynb/week35.ipynb +++ b/doc/pub/week35/ipynb/week35.ipynb @@ -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)" ] }, { diff --git a/doc/src/week35/week35.do.txt b/doc/src/week35/week35.do.txt index 92f3f9690..4c3c5815c 100644 --- a/doc/src/week35/week35.do.txt +++ b/doc/src/week35/week35.do.txt @@ -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