{"cells":[{"cell_type":"markdown","metadata":{"id":"Ac5OxfyShuau"},"source":["# 2 Practice and Comprehensive Questions Solutions"]},{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":35},"id":"Y2IuptI5huav","outputId":"09d041ab-f746-4881-f9d1-a5a71f0df4f1","executionInfo":{"status":"ok","timestamp":1790300881253,"user_tz":-540,"elapsed":61,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"execute_result","data":{"text/plain":["'%.3f'"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"}},"metadata":{},"execution_count":1}],"source":["import numpy as np\n","import numpy.random as random\n","import numpy.linalg as LA\n","\n","# Display up to 3 decimal places\n","%precision 3"]},{"cell_type":"code","source":["URL = \"https://www.ncei.noaa.gov/pub/data/cdo/samples/GHCND_sample_csv.csv\"\n","\n","raw = np.loadtxt(URL, delimiter=\",\", usecols=[6,7,8], skiprows=1)\n","\n","jan_tmax = raw[0:31, 0]\n","jan_tmin = raw[0:31, 1]\n","jan_prcp = raw[0:31, 2]\n","jan_tmax = jan_tmax / 10\n","jan_tmin = jan_tmin / 10\n","jan_prcp = jan_prcp / 10\n","\n","X = raw / 10"],"metadata":{"id":"d6OWcFCO3VTW","executionInfo":{"status":"ok","timestamp":1790300881559,"user_tz":-540,"elapsed":301,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"execution_count":2,"outputs":[]},{"cell_type":"code","source":["X"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"xh-ljJ2I-XQn","executionInfo":{"status":"ok","timestamp":1790300881603,"user_tz":-540,"elapsed":42,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"d8d6bbbd-d962-4ed8-8dcc-e209f708de1b"},"execution_count":3,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([[-1.780e+01, -3.110e+01, 0.000e+00],\n"," [-2.440e+01, -3.220e+01, 0.000e+00],\n"," [-1.940e+01, -2.890e+01, 0.000e+00],\n"," [-1.670e+01, -2.000e+01, 1.500e+00],\n"," [-1.330e+01, -1.670e+01, 9.999e+02],\n"," [-1.330e+01, -1.720e+01, 9.999e+02],\n"," [-1.500e+01, -2.780e+01, 0.000e+00],\n"," [-2.330e+01, -3.280e+01, 0.000e+00],\n"," [-2.330e+01, -3.220e+01, 0.000e+00],\n"," [-1.170e+01, -2.440e+01, 0.000e+00],\n"," [-6.700e+00, -1.280e+01, 0.000e+00],\n"," [-7.800e+00, -1.220e+01, 0.000e+00],\n"," [-1.700e+00, -8.900e+00, 0.000e+00],\n"," [ 3.900e+00, -7.200e+00, 0.000e+00],\n"," [-6.700e+00, -7.200e+00, 0.000e+00],\n"," [ 2.200e+00, -5.000e+00, 0.000e+00],\n"," [ 3.300e+00, -4.400e+00, 0.000e+00],\n"," [ 6.000e-01, -1.720e+01, 0.000e+00],\n"," [-5.600e+00, -1.830e+01, 0.000e+00],\n"," [-6.700e+00, -1.390e+01, 0.000e+00],\n"," [-6.700e+00, -9.400e+00, 2.500e+00],\n"," [-4.400e+00, -6.700e+00, 0.000e+00],\n"," [-6.000e-01, -4.400e+00, 0.000e+00],\n"," [ 0.000e+00, -1.100e+00, 0.000e+00],\n"," [-1.100e+00, -1.610e+01, 0.000e+00],\n"," [-1.610e+01, -2.330e+01, 0.000e+00],\n"," [-1.670e+01, -2.220e+01, 0.000e+00],\n"," [-1.670e+01, -2.830e+01, 0.000e+00],\n"," [-1.890e+01, -2.830e+01, 0.000e+00],\n"," [-1.560e+01, -2.670e+01, 0.000e+00],\n"," [-1.500e+01, -2.720e+01, 0.000e+00]])"]},"metadata":{},"execution_count":3}]},{"cell_type":"markdown","metadata":{"id":"t-fS4I7yhuaz"},"source":["### "]},{"cell_type":"markdown","metadata":{"id":"RpuZHT0Ohua0"},"source":["(1) Create two 1-dimenional `np.ndarray` arrays with a length of 3, 1 with even numbers as elements, and 1 with odd numbers as elements. Then use the `type()` function to confirm that they are of type `np.ndarray`."]},{"cell_type":"code","execution_count":4,"metadata":{"id":"KQEOSLnPhua0","executionInfo":{"status":"ok","timestamp":1790300881605,"user_tz":-540,"elapsed":34,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[],"source":["# (1)\n","array1 = np.array([0,2,4])\n","array2 = np.array([1,3,5])"]},{"cell_type":"code","execution_count":5,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"1h6zbFDNhua3","outputId":"af0c8e18-94e7-40e2-cddd-dd2554840a4e","executionInfo":{"status":"ok","timestamp":1790300881606,"user_tz":-540,"elapsed":31,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"execute_result","data":{"text/plain":["numpy.ndarray"]},"metadata":{},"execution_count":5}],"source":["type(array1)"]},{"cell_type":"code","execution_count":6,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Gi5f-pckhua5","outputId":"3b7cb0d9-83e5-4768-9c47-bba8644ddefc","executionInfo":{"status":"ok","timestamp":1790300881613,"user_tz":-540,"elapsed":7,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"execute_result","data":{"text/plain":["numpy.ndarray"]},"metadata":{},"execution_count":6}],"source":["type(array2)"]},{"cell_type":"markdown","metadata":{"id":"gSF9oPmLld_-"},"source":["(2) Add the 2 arrays that you created in (1) and confirm that it results in element-wise addition of the 2 arrays."]},{"cell_type":"code","execution_count":7,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Xr1WMwqDhua8","outputId":"7b2e0b2f-a4c2-4db6-867c-7bde2f60a89e","executionInfo":{"status":"ok","timestamp":1790300881622,"user_tz":-540,"elapsed":7,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["[1 5 9]\n"]}],"source":["# (2)\n","print(array1 + array2)"]},{"cell_type":"markdown","metadata":{"id":"sKi7Cze4hua-"},"source":["(3) Create a function `normalize(a)` that takes a 1-dimensional array as an argument and outputs a 1-dimensional array where the input is normalized.\n","\n","Here, normalization of a vector means scaling the original vector so that the square root of the sum of the squares of all its elements, which is called the L2 norm, equals 1.\n","\n","In this task, please implement the function using only **NumPy's universal operations and functions**.\n","\n","Hint: Let's try and normalize the vector $v=(1,1,1)$ by hand. The L2 norm of $v$ ,denoted as $||v||_2$, is $||v||_2 = \\sqrt{1^2+1^2+1^2}=\\sqrt{3}$. If we scale the original vector $v$ by the reciprocal of the L2 norm $\\frac{1}{\\sqrt{3}}$, we get a new vector $v' = (\\frac{1}{\\sqrt{3}},\\frac{1}{\\sqrt{3}},\\frac{1}{\\sqrt{3}})$. We can confirm that the L2 norm of $v'$ is 1:\n"," $$||v'|| = \\sqrt{(\\frac{1}{\\sqrt{3}})^2+(\\frac{1}{\\sqrt{3}})^2+(\\frac{1}{\\sqrt{3}})^2} = \\sqrt{\\frac13+\\frac13+\\frac13}=1$$\n"]},{"cell_type":"code","execution_count":8,"metadata":{"id":"3EH7ubT3hua_","executionInfo":{"status":"ok","timestamp":1790300881625,"user_tz":-540,"elapsed":2,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[],"source":["# (3)\n","def normalize(a):\n"," return a / np.sqrt(np.sum(a**2))"]},{"cell_type":"code","execution_count":9,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"izrOBw4AhubB","outputId":"3d4cdc7d-6e02-4448-dccf-98d39d37b26a","executionInfo":{"status":"ok","timestamp":1790300881640,"user_tz":-540,"elapsed":13,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([0, 2, 4])"]},"metadata":{},"execution_count":9}],"source":["array1"]},{"cell_type":"code","execution_count":10,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"L3KrpyeRhubE","outputId":"3eaa6ba3-d994-4734-bacf-68a930659f4f","executionInfo":{"status":"ok","timestamp":1790300881646,"user_tz":-540,"elapsed":5,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["[0. 0.447 0.894]\n","0.9999999999999999\n"]}],"source":["print(normalize(array1))\n","print(np.sqrt(np.sum(normalize(array1)**2)))"]},{"cell_type":"markdown","metadata":{"id":"QMCgRlfNhubG"},"source":["### "]},{"cell_type":"markdown","metadata":{"id":"Z1Edl3-1hubG"},"source":["(1) Using the array `jan_tmax` that we built, create a new array `tmax_mon` that contains the max temperatures of all Mondays in January. As mentioned previously, January 1st is a Friday."]},{"cell_type":"code","execution_count":11,"metadata":{"id":"zt643SPmhubH","executionInfo":{"status":"ok","timestamp":1790300881651,"user_tz":-540,"elapsed":2,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[],"source":["# (1)\n","tmax_mon = jan_tmax[3::7]"]},{"cell_type":"markdown","metadata":{"id":"pbJSoOPzhubJ"},"source":["(2) Calculate the average max temperature of all Mondays in January, then calculate the standard deviation of the max temperature of all Mondays in January.\n","\n","Hint: The formula for the standard deviation $\\sigma$ of a given data set is:\n","$$\n","\\sigma = \\sqrt{\\frac{\\sum (x_i - \\mu)^2}{N}}\n","$$\n","where $\\mu$ is the average value of the data, and $N$ is the size of the dataset.\n","\n","For example, let's calculate the standard deviation of the 2 datasets:\n","$$\n","D_1 = (-5,0,5),\\quad D_2 = (-100,0,100)\n","$$\n","You can tell that both datasets have the same average value of $0$, but $D_2$ is more \"spread out\" than $D_1$.The standard deviation of the 2 datasets $\\sigma_1$ and $\\sigma_2$ are:\n","$$\n","\\sigma_1 = \\sqrt{\\frac{(-5-0)^2+(0-0)^2+(5-0)^2}{3}} = 4.08 \\\\\n","\\sigma_2 = \\sqrt{\\frac{(-100-0)^2+(0-0)^2+(100-0)^2}{3}} = 81.6\n","$$\n","As you can see, the more \"spread out\" dataset had a higher standard deviation. Therefore, one can say that standard deviation is a measure of how spread out the data points are within a dataset.\n","\n","\n","You will never have to write out the code for this, as NumPy already has a function `np.std()` that does this for you (you are free to use it in this problem of course!)."]},{"cell_type":"code","execution_count":12,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"E42MzJDBhubM","outputId":"add25214-ae8f-41de-9e79-78993b451316","executionInfo":{"status":"ok","timestamp":1790300881723,"user_tz":-540,"elapsed":70,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["Mean: -5.975\n","Standard deviation: 6.7555070128007415\n"]}],"source":["# (2)\n","print(\"Mean: \", tmax_mon.mean())\n","print(\"Standard deviation: \", tmax_mon.std())"]},{"cell_type":"markdown","metadata":{"id":"51GnJo69hubS"},"source":["### "]},{"cell_type":"markdown","metadata":{"id":"Bp6a1YKzhubS"},"source":["(1) Using the 2-dimensional array `X` we just built, find the mean value and standard deviation for each column (`TMAX`, `TMIN`, `PRCP`), resulting with 2 1-dimensional arrays of length 3."]},{"cell_type":"code","execution_count":13,"metadata":{"id":"a-6IgGUKhubT","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1790300881723,"user_tz":-540,"elapsed":40,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}},"outputId":"5c5d6881-cb4e-407a-be30-a7e6d61d72f2"},"outputs":[{"output_type":"stream","name":"stdout","text":["Means: [-10.168 -18.197 64.639]\n","Standard deviations: [ 8.296 9.667 245.612]\n"]}],"source":["# (1)\n","X_means = np.mean(X, axis=0)\n","X_stds = np.std(X, axis=0)\n","print(\"Means: \", X_means)\n","print(\"Standard deviations: \", X_stds)"]},{"cell_type":"markdown","metadata":{"id":"BgHcZmARleAK"},"source":["(2) **Standardize** all elements in `X` with respect to the mean and standard deviation that you calculated in (1), and store the new values in a new array `X_std`. This should result in a 2-dimensional array with a shape of (31,3).\n","\n","Hint: If a dataset has a mean of $\\mu$ and a standard deviation of $\\sigma$, to standardize a data point $x$ means to perform the transformation: \n","$$\n","z = \\frac{x - \\mu}{\\sigma}\n","$$\n","where $z$ is the standardized version of the original data point (or the \"**z-score**\"). Therefore, you are going to perform this transformation on all of the elements in `X` in this problem.\n","\n","For example, the data set $-5,0,5$ has a mean value of $0$ and a standard deviation of $4.08$, so performing this transformation would result in a data set of$-1.2,0,1.2$.\n","\n","**Standardization** is used very frequently in statistics and machine learning, and we will use it later in this course as well. Whenever you have multiple variables in a dataset (such as `TMAX`,`TMIN`,`PRCP`) with different scales or units, standardization forces them to have the exact same scale, making the data more consistent and easier to deal with."]},{"cell_type":"code","execution_count":14,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"XIgc6xdHhubV","outputId":"ca82baab-9a8c-4bcf-f667-9a19ecca3e2d","executionInfo":{"status":"ok","timestamp":1790300881724,"user_tz":-540,"elapsed":33,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"execute_result","data":{"text/plain":["(31, 3)"]},"metadata":{},"execution_count":14}],"source":["# (2)\n","X_std = (X - X_means) / X_stds\n","X_std.shape"]},{"cell_type":"markdown","metadata":{"id":"OzM1oTGghubg"},"source":["### "]},{"cell_type":"markdown","metadata":{"id":"1Dd6gLPQhubh"},"source":["(1) Implement a function `get_data_zscore(date)` that takes an integer `date` (from 4 to 28) representing a day in January.\n","\n","The function should:\n","\n","1. Retrieve the `TMAX`,`TMIN`,`PCRP` of the days within 3 days of `date` (e.g., for January 5th, retrieve January 2nd through January 8th).\n","2. Calculate and return the z-score (exactly like you did in Practice Question 2-3!) of the target day's `TMAX`,`TMIN`,`PCRP` relative to that 7-day window. This should result in a 1-dimensional array with a length of 3."]},{"cell_type":"code","execution_count":15,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"KIGN9XR2hubh","outputId":"e8c56e6c-8318-4aec-9118-f33904466e8b","executionInfo":{"status":"ok","timestamp":1790300881724,"user_tz":-540,"elapsed":23,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([-0.317, -0.329, 3.08 ])"]},"metadata":{},"execution_count":15}],"source":["# (1)\n","def get_data_zscore(date):\n"," means = np.mean(X[date-4:date+3])\n"," stds = np.std(X[date-4:date+3])\n"," answer = (X[date] - means) / stds\n"," return answer\n","\n","get_data_zscore(4)"]},{"cell_type":"markdown","metadata":{"id":"JaiwYDyTleAO"},"source":["(2) Using boolean indexing that you just learned, calculate the average max temperature of all days with no rain, and of all days with rain (make sure to exclude days with a missing value `PCRP` value)."]},{"cell_type":"code","execution_count":16,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"va5AnutVhubj","outputId":"5002fa3e-e8ce-4223-84a1-c0bf891e33a4","executionInfo":{"status":"ok","timestamp":1790300881724,"user_tz":-540,"elapsed":15,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["Average tmax of days with no rain: -9.822222222222221\n","Average tmax of days with rain: -12.5\n"]}],"source":["# (2)\n","mask_no_rain = X[:,2] == 0\n","mask_rain = (X[:,2] != 0) & (X[:,2] != 99.99)\n","no_rain_mean = np.mean(X[mask_no_rain,0])\n","rain_mean = np.mean(X[mask_rain,0])\n","\n","print(\"Average tmax of days with no rain: \", no_rain_mean)\n","print(\"Average tmax of days with rain: \", rain_mean)"]},{"cell_type":"markdown","metadata":{"id":"BJ7Sbusihubt"},"source":["***"]},{"cell_type":"markdown","metadata":{"id":"SbBKi1ha1G8J"},"source":["## 2.7 Comprehensive Question"]},{"cell_type":"markdown","metadata":{"id":"DbjSUnT11G8J"},"source":["**Question 1**\\\n","Recently, in natural language processing using deep learning, models based on Google’s Transformer have gained attention due to their human-level performance on several NLP tasks.\n","\n","In January 2020, Google introduced [Reformer](https://arxiv.org/abs/2001.04451), which improves the time and space complexity of the Transformer model. In this task, we will implement a part of the Reformer paper, which uses [*cross-polytope LSH*](https://arxiv.org/abs/1509.02897) to improve the time complexity of the Transformer, with guided instructions."]},{"cell_type":"markdown","metadata":{"id":"NbfBhqjV1G8K"},"source":["Consider the following question:\n",">Given $n$ vectors, we want to assign the same integer value to \"similar\" vectors and different integer values to \"dissimilar\" vectors. In this context, \"similar\" vectors are those whose angle is close to 0.\n","\n","\n","The challenges of this problem are as follows:\n","- How small should the angle be for vectors to be considered \"similar\"?\n","- Even if vectors $a$ and $b$, and $b$ and $c$ are \"similar\", $a$ and $c$ may not necessarily be considered \"similar.\"\n","- To compute the angle $\\theta$, we can use their inner product and norms to calculate $\\cos \\theta$. We could then sort this value and assign integers accordingly, but sorting can become slow as $n$ increases (since the number of combinations of vectors is $_nC_2 = O(n^2)$)."]},{"cell_type":"markdown","metadata":{"id":"WNgnmYBG1G8K"},"source":["We consider the following probablistic approach:\n","\n","1. Rotate all vectors by the same random angle.\n","\n","2. Vectors that are \"similar\" will, with **high probability**, remain in the same quadrant after rotation, while vectors that are \"dissimilar\" will, with **high probability**, end up in different quadrants.\n","\n","3. Therefore, by assigning each vector an integer value corresponding to the quadrant it belongs to after the random rotation, the problem can be solved with **high probability**.\n","\n","Let's implement this approach.\n","\n"]},{"cell_type":"markdown","metadata":{"id":"jsgoTPIt1G8K"},"source":["This time, we consider 100 3D row vectors."]},{"cell_type":"code","execution_count":17,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"NUStisgF1G8K","outputId":"adca1222-1e45-447a-a356-4ea2b67199e3","tags":[],"executionInfo":{"status":"ok","timestamp":1790300881725,"user_tz":-540,"elapsed":10,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["[[ 1.35 0.567 -1.294]\n"," [ 0.997 0.537 -0.908]\n"," [-0.347 -0.252 -1.698]\n"," [-0.157 1.829 -0.427]\n"," [ 1.143 1.938 1.889]\n"," [-0.191 0.322 -1.377]\n"," [-0.166 1.361 -1.238]\n"," [ 0.534 -1.663 2.017]\n"," [-0.387 1.882 0.498]\n"," [ 0.897 -1.08 -0.528]\n"," [-0.733 0.561 1.756]\n"," [ 0.621 -0.03 0.038]\n"," [ 1.163 0.723 1.367]\n"," [ 1.179 -0.161 0.32 ]\n"," [-0.137 0.013 0.211]\n"," [-0.022 -0.935 0.165]\n"," [ 0.061 -1.446 -0.542]\n"," [-0.629 0.632 0.795]\n"," [-1.536 -1.579 0.769]\n"," [-1.63 -2.331 -0.372]\n"," [ 0.574 -0.263 -0.359]\n"," [-0.115 0.465 0.429]\n"," [-0.594 -1.103 0.891]\n"," [ 0.925 0.161 1.187]\n"," [-0.597 1.217 0.233]\n"," [-0.989 1.093 1.772]\n"," [-0.689 -1.314 -0.715]\n"," [ 0.659 0.414 1.01 ]\n"," [ 0.213 2.047 0.227]\n"," [ 0.56 -0.697 2.328]\n"," [ 1.427 -0.717 0.965]\n"," [ 0.589 0.857 1.14 ]\n"," [ 0.276 0.328 0.356]\n"," [-0.674 0.914 -0.207]\n"," [-1.202 -0.052 -0.251]\n"," [ 1.028 -0.336 -0.797]\n"," [ 0.407 -0.691 -0.598]\n"," [-0.511 0.421 -0.927]\n"," [ 0.687 0.818 2.139]\n"," [ 0.595 1.216 -0.257]\n"," [-0.026 -0.557 -0.984]\n"," [-0.012 0.902 -1.774]\n"," [ 0.632 0.685 -0.771]\n"," [ 1.596 -1.334 -0.6 ]\n"," [-0.466 0.228 -0.207]\n"," [-1.908 -1.238 -0.757]\n"," [ 1.245 1.686 0.237]\n"," [ 1.723 1.677 -0.134]\n"," [-0.377 0.141 -0.824]\n"," [ 0.618 -0.752 1.038]\n"," [-0.003 0.758 0.899]\n"," [ 0.094 -0.204 0.459]\n"," [-0.327 0.751 -0.443]\n"," [ 0.643 1.063 -0.265]\n"," [-0.211 0.434 -1.045]\n"," [-0.158 1.294 -1.641]\n"," [-1.02 -0.641 -0.715]\n"," [-0.577 0.894 0.77 ]\n"," [-2.169 -1.324 1.231]\n"," [ 0.083 -0.317 -0.46 ]\n"," [ 1.63 0.595 0.284]\n"," [ 0.168 0.589 0.189]\n"," [-0.267 -2.03 -0.438]\n"," [ 1.462 -2.118 -0.187]\n"," [-1.607 -1.041 -1.072]\n"," [-1.982 -0.375 -0.966]\n"," [ 0.407 0.535 -0.727]\n"," [ 1.215 -1.426 -0.206]\n"," [-0.672 -0.509 -0.367]\n"," [ 1.152 -1.143 -0.106]\n"," [ 1.418 -0.974 0.269]\n"," [-1.23 -0.744 0.426]\n"," [ 0.377 -0.84 -0.238]\n"," [ 0.816 -1.031 0.422]\n"," [-0.43 0.395 -0.518]\n"," [-0.026 -0.729 0.337]\n"," [-0.499 0.103 -0.576]\n"," [-2.641 1.18 -0.052]\n"," [ 0.806 0.064 -0.278]\n"," [-1.511 -0.905 -0.126]\n"," [ 1.307 -0.064 0.029]\n"," [ 0.261 -0.975 1.272]\n"," [-1.607 1.75 -2.122]\n"," [-1.654 0.73 0.726]\n"," [ 0.285 -0.061 2.219]\n"," [-0.213 0.215 -1.875]\n"," [-1.498 -0.024 -0.809]\n"," [ 1.212 -1.013 0.656]\n"," [ 2.099 -0.696 -0.328]\n"," [-0.817 -0.842 0.462]\n"," [-1.025 1.33 0.587]\n"," [ 0.586 -2.441 0.846]\n"," [-1.02 -0.719 0.677]\n"," [ 0.862 0.797 1.251]\n"," [-1.258 1.974 0.378]\n"," [-0.636 -1.148 0.901]\n"," [-2.096 1.572 0.203]\n"," [-0.015 1.101 0.914]\n"," [-1.652 -0.905 -0.956]\n"," [ 0.285 -0.049 1.349]]\n"]}],"source":["A = random.randn(100, 3) # A matrix stacked with 100 3D row vectors\n","print(A)"]},{"cell_type":"markdown","metadata":{"id":"0D0dmHr-1G8K"},"source":["(1) Generate a rotation matrix $R$ that rotates 100 vectors, and compute the matrix of rotated vectors stacked vertically into a shape of (100, 3). Here, the 3D rotation matrix $R$ for a random rotation is generated by passing a 1D array of three randomly generated angle parameters to the `gen_R_3d` function."]},{"cell_type":"code","execution_count":18,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"W_gXh6Gd1G8L","outputId":"5789007b-a2a6-42e5-fe2e-44e8c1b2a235","tags":[],"executionInfo":{"status":"ok","timestamp":1790300881752,"user_tz":-540,"elapsed":8,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["[[ 1.430e+00 -1.101e+00 7.495e-01]\n"," [ 1.034e+00 -9.053e-01 4.682e-01]\n"," [-7.452e-02 -3.728e-01 1.710e+00]\n"," [-3.338e-01 -1.841e+00 -2.294e-01]\n"," [ 6.093e-01 -1.174e+00 -2.623e+00]\n"," [-4.001e-02 -7.958e-01 1.184e+00]\n"," [-1.703e-01 -1.708e+00 6.829e-01]\n"," [ 4.635e-01 2.251e+00 -1.356e+00]\n"," [-6.942e-01 -1.530e+00 -1.057e+00]\n"," [ 1.094e+00 7.404e-01 7.099e-01]\n"," [-1.034e+00 1.844e-01 -1.683e+00]\n"," [ 6.085e-01 -2.375e-03 -1.334e-01]\n"," [ 8.594e-01 -2.445e-01 -1.716e+00]\n"," [ 1.134e+00 1.841e-01 -4.445e-01]\n"," [-1.649e-01 7.569e-02 -1.739e-01]\n"," [ 7.734e-02 9.285e-01 1.838e-01]\n"," [ 3.226e-01 1.133e+00 1.000e+00]\n"," [-8.098e-01 -2.456e-01 -8.432e-01]\n"," [-1.408e+00 1.857e+00 1.240e-01]\n"," [-1.245e+00 2.135e+00 1.455e+00]\n"," [ 6.473e-01 6.956e-02 3.218e-01]\n"," [-2.328e-01 -2.630e-01 -5.388e-01]\n"," [-5.622e-01 1.394e+00 -3.219e-01]\n"," [ 7.239e-01 2.263e-01 -1.310e+00]\n"," [-7.769e-01 -9.977e-01 -5.405e-01]\n"," [-1.358e+00 -2.838e-01 -1.841e+00]\n"," [-4.063e-01 1.000e+00 1.244e+00]\n"," [ 4.539e-01 -5.502e-02 -1.190e+00]\n"," [-8.876e-02 -1.826e+00 -9.723e-01]\n"," [ 3.203e-01 1.470e+00 -1.989e+00]\n"," [ 1.362e+00 9.214e-01 -8.828e-01]\n"," [ 3.092e-01 -4.114e-01 -1.454e+00]\n"," [ 1.788e-01 -1.912e-01 -4.918e-01]\n"," [-7.520e-01 -8.750e-01 -1.546e-02]\n"," [-1.138e+00 4.017e-02 4.608e-01]\n"," [ 1.163e+00 -5.808e-02 6.700e-01]\n"," [ 5.725e-01 3.884e-01 7.228e-01]\n"," [-4.294e-01 -6.977e-01 7.913e-01]\n"," [ 2.734e-01 -1.229e-02 -2.375e+00]\n"," [ 4.609e-01 -1.263e+00 -2.998e-01]\n"," [ 1.825e-01 1.523e-01 1.106e+00]\n"," [ 1.147e-01 -1.492e+00 1.312e+00]\n"," [ 6.378e-01 -9.648e-01 3.540e-01]\n"," [ 1.824e+00 8.992e-01 7.438e-01]\n"," [-4.584e-01 -2.551e-01 1.909e-01]\n"," [-1.608e+00 1.000e+00 1.469e+00]\n"," [ 9.705e-01 -1.562e+00 -1.034e+00]\n"," [ 1.491e+00 -1.725e+00 -7.750e-01]\n"," [-2.755e-01 -4.097e-01 7.730e-01]\n"," [ 5.617e-01 1.038e+00 -7.948e-01]\n"," [-2.255e-01 -3.673e-01 -1.094e+00]\n"," [ 5.532e-02 3.527e-01 -3.658e-01]\n"," [-3.576e-01 -8.364e-01 1.977e-01]\n"," [ 5.288e-01 -1.128e+00 -2.470e-01]\n"," [-1.200e-01 -7.746e-01 8.433e-01]\n"," [-9.822e-02 -1.796e+00 1.075e+00]\n"," [-8.193e-01 4.006e-01 1.063e+00]\n"," [-7.896e-01 -5.012e-01 -9.229e-01]\n"," [-2.127e+00 1.837e+00 -2.796e-01]\n"," [ 1.860e-01 1.168e-01 5.204e-01]\n"," [ 1.484e+00 -5.611e-01 -7.582e-01]\n"," [ 6.220e-02 -4.870e-01 -4.121e-01]\n"," [ 6.307e-02 1.735e+00 1.169e+00]\n"," [ 1.737e+00 1.788e+00 6.662e-01]\n"," [-1.295e+00 6.800e-01 1.636e+00]\n"," [-1.764e+00 1.297e-01 1.368e+00]\n"," [ 4.303e-01 -7.941e-01 4.066e-01]\n"," [ 1.408e+00 1.157e+00 4.807e-01]\n"," [-5.429e-01 3.826e-01 6.357e-01]\n"," [ 1.295e+00 9.367e-01 3.004e-01]\n"," [ 1.482e+00 9.011e-01 -1.511e-01]\n"," [-1.169e+00 9.344e-01 8.958e-02]\n"," [ 5.129e-01 6.626e-01 4.503e-01]\n"," [ 8.777e-01 1.053e+00 -1.658e-01]\n"," [-4.022e-01 -5.278e-01 4.115e-01]\n"," [ 2.344e-02 8.022e-01 -4.649e-02]\n"," [-4.243e-01 -2.734e-01 5.802e-01]\n"," [-2.739e+00 -9.245e-01 9.373e-02]\n"," [ 8.212e-01 -2.195e-01 9.141e-02]\n"," [-1.348e+00 8.985e-01 7.025e-01]\n"," [ 1.287e+00 -2.282e-02 -2.334e-01]\n"," [ 2.086e-01 1.357e+00 -8.681e-01]\n"," [-1.514e+00 -2.294e+00 1.610e+00]\n"," [-1.819e+00 -2.888e-01 -6.349e-01]\n"," [-1.751e-02 8.605e-01 -2.066e+00]\n"," [ 2.093e-02 -8.798e-01 1.683e+00]\n"," [-1.357e+00 -1.724e-01 1.015e+00]\n"," [ 1.232e+00 1.096e+00 -4.562e-01]\n"," [ 2.197e+00 3.742e-01 1.791e-01]\n"," [-7.566e-01 1.009e+00 1.771e-02]\n"," [-1.261e+00 -9.404e-01 -8.304e-01]\n"," [ 7.766e-01 2.532e+00 -1.447e-02]\n"," [-1.001e+00 9.894e-01 -1.867e-01]\n"," [ 5.704e-01 -3.342e-01 -1.583e+00]\n"," [-1.545e+00 -1.598e+00 -8.261e-01]\n"," [-5.987e-01 1.442e+00 -3.088e-01]\n"," [-2.290e+00 -1.231e+00 -3.753e-01]\n"," [-2.838e-01 -6.789e-01 -1.227e+00]\n"," [-1.373e+00 6.003e-01 1.489e+00]\n"," [ 1.010e-01 5.262e-01 -1.271e+00]]\n"]}],"source":["# (1)\n","def gen_R_3d(p):\n"," px, py, pz = p\n"," Rx = np.array([\n"," [1, 0, 0],\n"," [0, np.cos(px), np.sin(px)],\n"," [0, -np.sin(px), np.cos(px)]\n"," ])\n"," Ry = np.array([\n"," [np.cos(py), 0, -np.sin(py)],\n"," [0, 1, 0],\n"," [np.sin(py), 0, np.cos(py)]\n"," ])\n"," Rz = np.array([\n"," [np.cos(pz), np.sin(pz), 0],\n"," [-np.sin(pz), np.cos(pz), 0],\n"," [0, 0, 1]\n"," ])\n","\n"," return Rz @ Ry @ Rx\n","\n","p = 2 * np.pi * random.rand(3)\n","R = gen_R_3d(p)\n","\n","B = (R @ A.T).T\n","print(B)"]},{"cell_type":"markdown","metadata":{"id":"s0ivAfmh1G8L","tags":[]},"source":["(2) To normalize each vector obtained in (1) so that the L2 norm equals 1 and then stack them vertically to form a (100, 3) matrix, you would follow these steps:\n","\n","Compute the L2 norm for each vector.\n","Normalize each vector by dividing by its L2 norm.\n","Stack the normalized vectors without changing their order."]},{"cell_type":"code","execution_count":19,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ZaI99uHi1G8L","outputId":"c141b66c-f655-4557-afe5-2c4e08c6089d","tags":[],"executionInfo":{"status":"ok","timestamp":1790300881758,"user_tz":-540,"elapsed":9,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["[[ 0.732 -0.564 0.384]\n"," [ 0.712 -0.624 0.323]\n"," [-0.043 -0.213 0.976]\n"," [-0.177 -0.977 -0.122]\n"," [ 0.207 -0.4 -0.893]\n"," [-0.028 -0.558 0.83 ]\n"," [-0.092 -0.925 0.37 ]\n"," [ 0.174 0.843 -0.508]\n"," [-0.35 -0.771 -0.532]\n"," [ 0.73 0.494 0.473]\n"," [-0.521 0.093 -0.848]\n"," [ 0.977 -0.004 -0.214]\n"," [ 0.444 -0.126 -0.887]\n"," [ 0.921 0.149 -0.361]\n"," [-0.656 0.301 -0.692]\n"," [ 0.081 0.978 0.194]\n"," [ 0.209 0.733 0.647]\n"," [-0.678 -0.206 -0.706]\n"," [-0.603 0.796 0.053]\n"," [-0.434 0.744 0.507]\n"," [ 0.891 0.096 0.443]\n"," [-0.362 -0.409 -0.838]\n"," [-0.366 0.907 -0.209]\n"," [ 0.478 0.149 -0.865]\n"," [-0.565 -0.726 -0.393]\n"," [-0.589 -0.123 -0.799]\n"," [-0.247 0.607 0.755]\n"," [ 0.356 -0.043 -0.933]\n"," [-0.043 -0.882 -0.47 ]\n"," [ 0.128 0.589 -0.798]\n"," [ 0.73 0.494 -0.473]\n"," [ 0.2 -0.267 -0.943]\n"," [ 0.321 -0.343 -0.883]\n"," [-0.652 -0.758 -0.013]\n"," [-0.926 0.033 0.375]\n"," [ 0.866 -0.043 0.499]\n"," [ 0.572 0.388 0.722]\n"," [-0.377 -0.613 0.695]\n"," [ 0.114 -0.005 -0.993]\n"," [ 0.335 -0.917 -0.218]\n"," [ 0.161 0.135 0.978]\n"," [ 0.058 -0.75 0.659]\n"," [ 0.527 -0.798 0.293]\n"," [ 0.842 0.415 0.343]\n"," [-0.821 -0.457 0.342]\n"," [-0.671 0.417 0.613]\n"," [ 0.46 -0.74 -0.49 ]\n"," [ 0.619 -0.716 -0.322]\n"," [-0.3 -0.447 0.843]\n"," [ 0.395 0.73 -0.559]\n"," [-0.192 -0.312 -0.93 ]\n"," [ 0.108 0.69 -0.716]\n"," [-0.384 -0.899 0.212]\n"," [ 0.416 -0.888 -0.195]\n"," [-0.104 -0.673 0.732]\n"," [-0.047 -0.857 0.513]\n"," [-0.585 0.286 0.759]\n"," [-0.601 -0.381 -0.702]\n"," [-0.753 0.65 -0.099]\n"," [ 0.329 0.207 0.921]\n"," [ 0.844 -0.319 -0.431]\n"," [ 0.097 -0.76 -0.643]\n"," [ 0.03 0.829 0.559]\n"," [ 0.673 0.693 0.258]\n"," [-0.59 0.31 0.746]\n"," [-0.789 0.058 0.612]\n"," [ 0.434 -0.802 0.41 ]\n"," [ 0.747 0.614 0.255]\n"," [-0.591 0.416 0.691]\n"," [ 0.796 0.576 0.185]\n"," [ 0.851 0.518 -0.087]\n"," [-0.78 0.623 0.06 ]\n"," [ 0.539 0.697 0.473]\n"," [ 0.636 0.763 -0.12 ]\n"," [-0.515 -0.676 0.527]\n"," [ 0.029 0.998 -0.058]\n"," [-0.552 -0.355 0.754]\n"," [-0.947 -0.32 0.032]\n"," [ 0.961 -0.257 0.107]\n"," [-0.763 0.509 0.398]\n"," [ 0.984 -0.017 -0.178]\n"," [ 0.128 0.835 -0.535]\n"," [-0.475 -0.72 0.505]\n"," [-0.934 -0.148 -0.326]\n"," [-0.008 0.384 -0.923]\n"," [ 0.011 -0.463 0.886]\n"," [-0.797 -0.101 0.596]\n"," [ 0.72 0.641 -0.267]\n"," [ 0.983 0.167 0.08 ]\n"," [-0.6 0.8 0.014]\n"," [-0.709 -0.529 -0.467]\n"," [ 0.293 0.956 -0.005]\n"," [-0.705 0.697 -0.131]\n"," [ 0.332 -0.195 -0.923]\n"," [-0.651 -0.674 -0.348]\n"," [-0.376 0.906 -0.194]\n"," [-0.872 -0.469 -0.143]\n"," [-0.198 -0.474 -0.858]\n"," [-0.65 0.284 0.705]\n"," [ 0.073 0.382 -0.921]]\n"]}],"source":["# (2)\n","B_norm_l2 = LA.norm(B, ord=2, axis=1, keepdims=True)\n","B_normalized = B / B_norm_l2\n","print(B_normalized)"]},{"cell_type":"markdown","metadata":{"id":"xk-rd04c1G8L"},"source":["(3) For the 100 vectors with a norm of 1 obtained in (2), compute a matrix of shape (100, 6) by stacking the original 100 vectors and their -1 multiplied counterparts **horizontally**, without changing the order of the vectors."]},{"cell_type":"code","execution_count":20,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"PtpwVgAZ1G8L","outputId":"5289cb9b-94a2-47d8-a277-24e66572bcbd","tags":[],"executionInfo":{"status":"ok","timestamp":1790300881807,"user_tz":-540,"elapsed":49,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["[[ 0.732 -0.564 0.384 -0.732 0.564 -0.384]\n"," [ 0.712 -0.624 0.323 -0.712 0.624 -0.323]\n"," [-0.043 -0.213 0.976 0.043 0.213 -0.976]\n"," [-0.177 -0.977 -0.122 0.177 0.977 0.122]\n"," [ 0.207 -0.4 -0.893 -0.207 0.4 0.893]\n"," [-0.028 -0.558 0.83 0.028 0.558 -0.83 ]\n"," [-0.092 -0.925 0.37 0.092 0.925 -0.37 ]\n"," [ 0.174 0.843 -0.508 -0.174 -0.843 0.508]\n"," [-0.35 -0.771 -0.532 0.35 0.771 0.532]\n"," [ 0.73 0.494 0.473 -0.73 -0.494 -0.473]\n"," [-0.521 0.093 -0.848 0.521 -0.093 0.848]\n"," [ 0.977 -0.004 -0.214 -0.977 0.004 0.214]\n"," [ 0.444 -0.126 -0.887 -0.444 0.126 0.887]\n"," [ 0.921 0.149 -0.361 -0.921 -0.149 0.361]\n"," [-0.656 0.301 -0.692 0.656 -0.301 0.692]\n"," [ 0.081 0.978 0.194 -0.081 -0.978 -0.194]\n"," [ 0.209 0.733 0.647 -0.209 -0.733 -0.647]\n"," [-0.678 -0.206 -0.706 0.678 0.206 0.706]\n"," [-0.603 0.796 0.053 0.603 -0.796 -0.053]\n"," [-0.434 0.744 0.507 0.434 -0.744 -0.507]\n"," [ 0.891 0.096 0.443 -0.891 -0.096 -0.443]\n"," [-0.362 -0.409 -0.838 0.362 0.409 0.838]\n"," [-0.366 0.907 -0.209 0.366 -0.907 0.209]\n"," [ 0.478 0.149 -0.865 -0.478 -0.149 0.865]\n"," [-0.565 -0.726 -0.393 0.565 0.726 0.393]\n"," [-0.589 -0.123 -0.799 0.589 0.123 0.799]\n"," [-0.247 0.607 0.755 0.247 -0.607 -0.755]\n"," [ 0.356 -0.043 -0.933 -0.356 0.043 0.933]\n"," [-0.043 -0.882 -0.47 0.043 0.882 0.47 ]\n"," [ 0.128 0.589 -0.798 -0.128 -0.589 0.798]\n"," [ 0.73 0.494 -0.473 -0.73 -0.494 0.473]\n"," [ 0.2 -0.267 -0.943 -0.2 0.267 0.943]\n"," [ 0.321 -0.343 -0.883 -0.321 0.343 0.883]\n"," [-0.652 -0.758 -0.013 0.652 0.758 0.013]\n"," [-0.926 0.033 0.375 0.926 -0.033 -0.375]\n"," [ 0.866 -0.043 0.499 -0.866 0.043 -0.499]\n"," [ 0.572 0.388 0.722 -0.572 -0.388 -0.722]\n"," [-0.377 -0.613 0.695 0.377 0.613 -0.695]\n"," [ 0.114 -0.005 -0.993 -0.114 0.005 0.993]\n"," [ 0.335 -0.917 -0.218 -0.335 0.917 0.218]\n"," [ 0.161 0.135 0.978 -0.161 -0.135 -0.978]\n"," [ 0.058 -0.75 0.659 -0.058 0.75 -0.659]\n"," [ 0.527 -0.798 0.293 -0.527 0.798 -0.293]\n"," [ 0.842 0.415 0.343 -0.842 -0.415 -0.343]\n"," [-0.821 -0.457 0.342 0.821 0.457 -0.342]\n"," [-0.671 0.417 0.613 0.671 -0.417 -0.613]\n"," [ 0.46 -0.74 -0.49 -0.46 0.74 0.49 ]\n"," [ 0.619 -0.716 -0.322 -0.619 0.716 0.322]\n"," [-0.3 -0.447 0.843 0.3 0.447 -0.843]\n"," [ 0.395 0.73 -0.559 -0.395 -0.73 0.559]\n"," [-0.192 -0.312 -0.93 0.192 0.312 0.93 ]\n"," [ 0.108 0.69 -0.716 -0.108 -0.69 0.716]\n"," [-0.384 -0.899 0.212 0.384 0.899 -0.212]\n"," [ 0.416 -0.888 -0.195 -0.416 0.888 0.195]\n"," [-0.104 -0.673 0.732 0.104 0.673 -0.732]\n"," [-0.047 -0.857 0.513 0.047 0.857 -0.513]\n"," [-0.585 0.286 0.759 0.585 -0.286 -0.759]\n"," [-0.601 -0.381 -0.702 0.601 0.381 0.702]\n"," [-0.753 0.65 -0.099 0.753 -0.65 0.099]\n"," [ 0.329 0.207 0.921 -0.329 -0.207 -0.921]\n"," [ 0.844 -0.319 -0.431 -0.844 0.319 0.431]\n"," [ 0.097 -0.76 -0.643 -0.097 0.76 0.643]\n"," [ 0.03 0.829 0.559 -0.03 -0.829 -0.559]\n"," [ 0.673 0.693 0.258 -0.673 -0.693 -0.258]\n"," [-0.59 0.31 0.746 0.59 -0.31 -0.746]\n"," [-0.789 0.058 0.612 0.789 -0.058 -0.612]\n"," [ 0.434 -0.802 0.41 -0.434 0.802 -0.41 ]\n"," [ 0.747 0.614 0.255 -0.747 -0.614 -0.255]\n"," [-0.591 0.416 0.691 0.591 -0.416 -0.691]\n"," [ 0.796 0.576 0.185 -0.796 -0.576 -0.185]\n"," [ 0.851 0.518 -0.087 -0.851 -0.518 0.087]\n"," [-0.78 0.623 0.06 0.78 -0.623 -0.06 ]\n"," [ 0.539 0.697 0.473 -0.539 -0.697 -0.473]\n"," [ 0.636 0.763 -0.12 -0.636 -0.763 0.12 ]\n"," [-0.515 -0.676 0.527 0.515 0.676 -0.527]\n"," [ 0.029 0.998 -0.058 -0.029 -0.998 0.058]\n"," [-0.552 -0.355 0.754 0.552 0.355 -0.754]\n"," [-0.947 -0.32 0.032 0.947 0.32 -0.032]\n"," [ 0.961 -0.257 0.107 -0.961 0.257 -0.107]\n"," [-0.763 0.509 0.398 0.763 -0.509 -0.398]\n"," [ 0.984 -0.017 -0.178 -0.984 0.017 0.178]\n"," [ 0.128 0.835 -0.535 -0.128 -0.835 0.535]\n"," [-0.475 -0.72 0.505 0.475 0.72 -0.505]\n"," [-0.934 -0.148 -0.326 0.934 0.148 0.326]\n"," [-0.008 0.384 -0.923 0.008 -0.384 0.923]\n"," [ 0.011 -0.463 0.886 -0.011 0.463 -0.886]\n"," [-0.797 -0.101 0.596 0.797 0.101 -0.596]\n"," [ 0.72 0.641 -0.267 -0.72 -0.641 0.267]\n"," [ 0.983 0.167 0.08 -0.983 -0.167 -0.08 ]\n"," [-0.6 0.8 0.014 0.6 -0.8 -0.014]\n"," [-0.709 -0.529 -0.467 0.709 0.529 0.467]\n"," [ 0.293 0.956 -0.005 -0.293 -0.956 0.005]\n"," [-0.705 0.697 -0.131 0.705 -0.697 0.131]\n"," [ 0.332 -0.195 -0.923 -0.332 0.195 0.923]\n"," [-0.651 -0.674 -0.348 0.651 0.674 0.348]\n"," [-0.376 0.906 -0.194 0.376 -0.906 0.194]\n"," [-0.872 -0.469 -0.143 0.872 0.469 0.143]\n"," [-0.198 -0.474 -0.858 0.198 0.474 0.858]\n"," [-0.65 0.284 0.705 0.65 -0.284 -0.705]\n"," [ 0.073 0.382 -0.921 -0.073 -0.382 0.921]]\n"]}],"source":["# (3)\n","C = np.concatenate([B_normalized, -B_normalized], axis=1)\n","print(C)"]},{"cell_type":"markdown","metadata":{"id":"4XdINNOH1G8L"},"source":["(4) (Somewhat Difficult)\n","Currently, in a 3D space, there are 8 quadrants numbered from 0 to 7. Instead of directly finding the quadrant, we will use 6 standard basis vectors and their inverse (6 vectors in total). The task is to find the \"most similar\" vector from this set.\n","\n","After rotating the vectors, find the corresponding vector number and store it in a (100, 1) matrix.\n","(Hint: Think about how to assign the vector number using the normalized vectors. You can avoid using the inner product for efficiency. If you're unsure, try considering the case in 2D and then apply the same idea to 3D. You can utilize the normalization step from part (2).)"]},{"cell_type":"code","execution_count":21,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"icjlqBN31G8M","outputId":"c684e20d-3c31-4b0e-abb9-ca9662ca9d4f","tags":[],"executionInfo":{"status":"ok","timestamp":1790300881808,"user_tz":-540,"elapsed":10,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["[[0]\n"," [0]\n"," [2]\n"," [4]\n"," [5]\n"," [2]\n"," [4]\n"," [1]\n"," [4]\n"," [0]\n"," [5]\n"," [0]\n"," [5]\n"," [0]\n"," [5]\n"," [1]\n"," [1]\n"," [5]\n"," [1]\n"," [1]\n"," [0]\n"," [5]\n"," [1]\n"," [5]\n"," [4]\n"," [5]\n"," [2]\n"," [5]\n"," [4]\n"," [5]\n"," [0]\n"," [5]\n"," [5]\n"," [4]\n"," [3]\n"," [0]\n"," [2]\n"," [2]\n"," [5]\n"," [4]\n"," [2]\n"," [4]\n"," [4]\n"," [0]\n"," [3]\n"," [3]\n"," [4]\n"," [4]\n"," [2]\n"," [1]\n"," [5]\n"," [5]\n"," [4]\n"," [4]\n"," [2]\n"," [4]\n"," [2]\n"," [5]\n"," [3]\n"," [2]\n"," [0]\n"," [4]\n"," [1]\n"," [1]\n"," [2]\n"," [3]\n"," [4]\n"," [0]\n"," [2]\n"," [0]\n"," [0]\n"," [3]\n"," [1]\n"," [1]\n"," [4]\n"," [1]\n"," [2]\n"," [3]\n"," [0]\n"," [3]\n"," [0]\n"," [1]\n"," [4]\n"," [3]\n"," [5]\n"," [2]\n"," [3]\n"," [0]\n"," [0]\n"," [1]\n"," [3]\n"," [1]\n"," [3]\n"," [5]\n"," [4]\n"," [1]\n"," [3]\n"," [5]\n"," [2]\n"," [5]]\n"]}],"source":["# (4)\n","C_argmax = np.argmax(C, axis=1).reshape(-1,1)\n","print(C_argmax)"]},{"cell_type":"markdown","metadata":{"id":"27yH1ScW1G8M"},"source":["The 100 integers obtained here will be the values assigned to 100 vectors.\n","\n","(5) (For those with extra time)\n","For the 100 vectors, calculate the cosine of the angle $\\theta$ between every pair of vectors. Verify that vectors that are \"similar\" (i.e., have a small angle between them) are likely to have been assigned the same integer value, while vectors that are \"dissimilar\" (i.e., have a large angle between them) are likely to have been assigned different integer values."]},{"cell_type":"code","execution_count":22,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"YcwJcfle1G8M","outputId":"af6503e8-c637-4fb1-87c4-3f1ff76bf579","executionInfo":{"status":"ok","timestamp":1790300881819,"user_tz":-540,"elapsed":14,"user":{"displayName":"木村進太郎","userId":"02485141327510571181"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["Proportion of similar vectors with the same integer value assigned: 0.5973451327433629\n","Proportion of dissimilar vectors with different integer values assigned]: 0.9075374531835206\n"]}],"source":["# (5)\n","D = B_normalized\n","D_num = C_argmax\n","\n","#Setting a threshold for cosine similarity to determine similarity.\n","threshold = 1 / np.sqrt(2)\n","\n","n_similar = 0 # Number of similar vectors\n","n_similar_same = 0 # Number of similar vectors with the same index\n","n_notsimilar = 0 # Number of dissimilar vectors\n","n_notsimilar_dif = 0 # Number of dissimilar vectors with different indices\n","for i in range(100):\n"," for j in range(i+1,100):\n"," cos = np.dot(D[i],D[j]) / (LA.norm(D[i]) * LA.norm(D[j]))\n"," # Similar vectors\n"," if cos > threshold:\n"," n_similar += 1\n"," if D_num[i] == D_num[j]:\n"," n_similar_same += 1\n"," # Dissimilr vectors\n"," else:\n"," n_notsimilar += 1\n"," if D_num[i] != D_num[j]:\n"," n_notsimilar_dif += 1\n","\n","print('Proportion of similar vectors with the same integer value assigned: ', n_similar_same / n_similar)\n","print('Proportion of dissimilar vectors with different integer values assigned]: ', n_notsimilar_dif / n_notsimilar)"]}],"metadata":{"colab":{"provenance":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.14"}},"nbformat":4,"nbformat_minor":0}