diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 46f5aa1..14e05bf 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -16,11 +16,12 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "import numpy as np" ] }, { @@ -34,11 +35,103 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 31, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.26.4\n", + "Build Dependencies:\n", + " blas:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: /opt/anaconda3/include\n", + " lib directory: /opt/anaconda3/lib\n", + " name: openblas\n", + " openblas configuration: USE_64BITINT= DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=\n", + " NO_LAPACK=0 NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP=0 VORTEX MAX_THREADS=128\n", + " pc file directory: /opt/anaconda3/lib/pkgconfig\n", + " version: 0.3.21\n", + " lapack:\n", + " detection method: internal\n", + " found: true\n", + " include directory: unknown\n", + " lib directory: unknown\n", + " name: dep4347739648\n", + " openblas configuration: unknown\n", + " pc file directory: unknown\n", + " version: 1.26.4\n", + "Compilers:\n", + " c:\n", + " args: -ftree-vectorize, -fPIC, -fPIE, -fstack-protector-strong, -O2, -pipe, -isystem,\n", + " /opt/anaconda3/include, -fdebug-prefix-map=/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_a51i_mbs7m/croot/numpy_and_numpy_base_1708638620867/work=/usr/local/src/conda/numpy-base-1.26.4,\n", + " -fdebug-prefix-map=/opt/anaconda3=/usr/local/src/conda-prefix, -D_FORTIFY_SOURCE=2,\n", + " -isystem, /opt/anaconda3/include, -mmacosx-version-min=11.1\n", + " commands: arm64-apple-darwin20.0.0-clang\n", + " linker: ld64\n", + " linker args: -Wl,-pie, -Wl,-headerpad_max_install_names, -Wl,-dead_strip_dylibs,\n", + " -Wl,-rpath,/opt/anaconda3/lib, -L/opt/anaconda3/lib, -ftree-vectorize, -fPIC,\n", + " -fPIE, -fstack-protector-strong, -O2, -pipe, -isystem, /opt/anaconda3/include,\n", + " -fdebug-prefix-map=/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_a51i_mbs7m/croot/numpy_and_numpy_base_1708638620867/work=/usr/local/src/conda/numpy-base-1.26.4,\n", + " -fdebug-prefix-map=/opt/anaconda3=/usr/local/src/conda-prefix, -D_FORTIFY_SOURCE=2,\n", + " -isystem, /opt/anaconda3/include, -mmacosx-version-min=11.1\n", + " name: clang\n", + " version: 14.0.6\n", + " c++:\n", + " args: -ftree-vectorize, -fPIC, -fPIE, -fstack-protector-strong, -O2, -pipe, -stdlib=libc++,\n", + " -fvisibility-inlines-hidden, -fmessage-length=0, -isystem, /opt/anaconda3/include,\n", + " -fdebug-prefix-map=/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_a51i_mbs7m/croot/numpy_and_numpy_base_1708638620867/work=/usr/local/src/conda/numpy-base-1.26.4,\n", + " -fdebug-prefix-map=/opt/anaconda3=/usr/local/src/conda-prefix, -D_FORTIFY_SOURCE=2,\n", + " -isystem, /opt/anaconda3/include, -mmacosx-version-min=11.1\n", + " commands: arm64-apple-darwin20.0.0-clang++\n", + " linker: ld64\n", + " linker args: -Wl,-pie, -Wl,-headerpad_max_install_names, -Wl,-dead_strip_dylibs,\n", + " -Wl,-rpath,/opt/anaconda3/lib, -L/opt/anaconda3/lib, -ftree-vectorize, -fPIC,\n", + " -fPIE, -fstack-protector-strong, -O2, -pipe, -stdlib=libc++, -fvisibility-inlines-hidden,\n", + " -fmessage-length=0, -isystem, /opt/anaconda3/include, -fdebug-prefix-map=/var/folders/k1/30mswbxs7r1g6zwn8y4fyt500000gp/T/abs_a51i_mbs7m/croot/numpy_and_numpy_base_1708638620867/work=/usr/local/src/conda/numpy-base-1.26.4,\n", + " -fdebug-prefix-map=/opt/anaconda3=/usr/local/src/conda-prefix, -D_FORTIFY_SOURCE=2,\n", + " -isystem, /opt/anaconda3/include, -mmacosx-version-min=11.1\n", + " name: clang\n", + " version: 14.0.6\n", + " cython:\n", + " commands: cython\n", + " linker: cython\n", + " name: cython\n", + " version: 3.0.8\n", + "Machine Information:\n", + " build:\n", + " cpu: aarch64\n", + " endian: little\n", + " family: aarch64\n", + " system: darwin\n", + " host:\n", + " cpu: aarch64\n", + " endian: little\n", + " family: aarch64\n", + " system: darwin\n", + "Python Information:\n", + " path: /opt/anaconda3/bin/python\n", + " version: '3.12'\n", + "SIMD Extensions:\n", + " baseline:\n", + " - NEON\n", + " - NEON_FP16\n", + " - NEON_VFPV4\n", + " - ASIMD\n", + " found:\n", + " - ASIMDHP\n", + " not found:\n", + " - ASIMDFHM\n", + "\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(np.__version__)\n", + "np.show_config()" ] }, { @@ -51,11 +144,13 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "a = np.random.random((2, 3, 5)) #(block-2 matricies, rows-3 rows each, columns-5 columns each)\n", + "#creats an array with 2 matricies (2 blocks), each with 3 rows and 5 columns\n" ] }, { @@ -68,11 +163,26 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.75111179 0.81716527 0.05349836 0.38187378 0.27600066]\n", + " [0.52183416 0.44593848 0.41795513 0.47783589 0.97640373]\n", + " [0.98619708 0.62085297 0.31020473 0.32496559 0.5449292 ]]\n", + "\n", + " [[0.0547659 0.02187434 0.51043675 0.25615015 0.27199069]\n", + " [0.40873704 0.62728534 0.88716142 0.1829674 0.38065654]\n", + " [0.51145931 0.35528294 0.09856278 0.22368068 0.32731886]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(a)\n" ] }, { @@ -85,11 +195,14 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "b = np.ones((5, 2, 3))\n", + "#np.ones is a function that creates an array of ones\n", + "#(5 blocks, 2 rows, 3 columns)\n" ] }, { @@ -102,11 +215,33 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]\n", + "\n", + " [[1. 1. 1.]\n", + " [1. 1. 1.]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "print(b)\n" ] }, { @@ -119,11 +254,32 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Size of a: 30\n", + "Size of b: 30\n", + "Do a and b have the same size? True\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "#check if the two arrays have the same size (total # of elements).\n", + "#.size counts the total number of elements in the array\n", + "size_a = a.size\n", + "size_b = b.size\n", + "\n", + "\n", + "print(\"Size of a:\", size_a)\n", + "print(\"Size of b:\", size_b)\n", + "\n", + "#check if the sizes are equal\n", + "print(\"Do a and b have the same size?\", size_a == size_b) #check if the sizes (# of elements) are equal\n" ] }, { @@ -136,11 +292,28 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cannot add a and b: operands could not be broadcast together with shapes (2,3,5) (5,2,3) \n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "try:\n", + " result = a + b\n", + "except ValueError as e:\n", + " print(\"Cannot add a and b:\", e)\n", + "\n", + "#we cannot add a and b because they have different shapes: a is a 2x3x5 array and b is a 5x2x3 array\n", + " #numpy uses broadcasting rules when doing operations on arrays.\n", + " #for broadcasting to work, the array shapes must be compatible-they must match exactly or have a 1 in the dimension to stretch.\n", + "\n" ] }, { @@ -154,11 +327,33 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [], + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2, 3, 5)\n" + ] + } + ], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "c = np.transpose(b, (1, 2, 0))\n", + "print(c.shape) # should now be (2, 3, 5)\n", + "\n", + "#logic of the transpose function:\n", + "#(5, 2, 3) -> (2, 3, 5)\n", + "#each axis references a dimension of the array. In the original b array:\n", + " #axis 0 has 5 elements (blocks)\n", + " #axis 1 has 2 elements (rows) per block\n", + " #axis 2 has 3 elements (columns) per row\n", + "# so to transform we change the order of the axes:\n", + " #axis 0 (5) becomes axis 2 (5)\n", + " #axis 1 (2) becomes axis 0 (2)\n", + " #axis 2 (3) becomes axis 1 (3)\n", + "\n" ] }, { @@ -166,16 +361,35 @@ "metadata": {}, "source": [ "\n", - "### 10. Try to add a and c. Now it should work. Assign the sum to variable \"d\". But why does it work now?\n" + "### 10. \n", + "Try to add a and c. Now it should work. Assign the sum to variable \"d\". But why does it work now?" ] }, { "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [], + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.75111179 1.81716527 1.05349836 1.38187378 1.27600066]\n", + " [1.52183416 1.44593848 1.41795513 1.47783589 1.97640373]\n", + " [1.98619708 1.62085297 1.31020473 1.32496559 1.5449292 ]]\n", + "\n", + " [[1.0547659 1.02187434 1.51043675 1.25615015 1.27199069]\n", + " [1.40873704 1.62728534 1.88716142 1.1829674 1.38065654]\n", + " [1.51145931 1.35528294 1.09856278 1.22368068 1.32731886]]]\n" + ] + } + ], "source": [ - "### [your code here]\n" + "\n", + "# Now add a and c — this works because their shapes are both (2, 3, 5)\n", + "d = a + c\n", + "\n", + "print(d)\n" ] }, { @@ -188,14 +402,50 @@ }, { "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Array a:\n", + " [[[0.75111179 0.81716527 0.05349836 0.38187378 0.27600066]\n", + " [0.52183416 0.44593848 0.41795513 0.47783589 0.97640373]\n", + " [0.98619708 0.62085297 0.31020473 0.32496559 0.5449292 ]]\n", + "\n", + " [[0.0547659 0.02187434 0.51043675 0.25615015 0.27199069]\n", + " [0.40873704 0.62728534 0.88716142 0.1829674 0.38065654]\n", + " [0.51145931 0.35528294 0.09856278 0.22368068 0.32731886]]]\n", + "\n", + "Array d:\n", + " [[[1.75111179 1.81716527 1.05349836 1.38187378 1.27600066]\n", + " [1.52183416 1.44593848 1.41795513 1.47783589 1.97640373]\n", + " [1.98619708 1.62085297 1.31020473 1.32496559 1.5449292 ]]\n", + "\n", + " [[1.0547659 1.02187434 1.51043675 1.25615015 1.27199069]\n", + " [1.40873704 1.62728534 1.88716142 1.1829674 1.38065654]\n", + " [1.51145931 1.35528294 1.09856278 1.22368068 1.32731886]]]\n" + ] + } + ], "source": [ "### [your code here]\n", + "\n", + "print(\"Array a:\\n\", a)\n", + "print(\"\\nArray d:\\n\", d)\n", + "\n", + "\n", "\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "11. Explination: array d values are exaclty 1 more than array a values. This is because c is an array of all ones (1.0), and since d=a+c, then d=a+(1's arary). So d valuess will be exaclty 1 more than a values. " + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -206,11 +456,13 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "e = a * c\n", + "#note since c values are ones, result expected is e==a because e = a * ones and any value x1 is just the same value." ] }, { @@ -224,12 +476,28 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [], + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "### [your code here]\n", - "\n" + "is_equal = np.array_equal(e, a)\n", + "print(is_equal) # returns true if equal\n", + "\n", + "#or\n", + "# np.array_equal(e, a) #returns true if equal\n", + "\n", + "\n", + "#yes, e == a\n", + "#since c values are ones, and e = a * c is just e = a * 1.0. Any value x1 is just the same value.\n" ] }, { @@ -243,12 +511,29 @@ }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, - "outputs": [], + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "d_max = 1.9861970785312588\n", + "d_min = 1.0218743448523924\n", + "d_mean = 1.434169898846247\n" + ] + } + ], "source": [ "### [your code here]\n", - "\n" + "d_max = np.max(d)\n", + "d_min = np.min(d)\n", + "d_mean = np.mean(d)\n", + "\n", + "#not sure if needed to print:\n", + "print(\"d_max =\", d_max)\n", + "print(\"d_min =\", d_min)\n", + "print(\"d_mean =\", d_mean)" ] }, { @@ -261,11 +546,16 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "f = np.empty(d.shape) #creates empty array of specified d shape...we dont need to include the d shape again because it was specified above.\n", + "\n", + "#if d shape was not specified, we would use this:\n", + "#f = np.empty((2, 3, 5))\n", + "\n" ] }, { @@ -287,11 +577,28 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ - "### [your code here]\n" + "### [your code here]\n", + "\n", + "#For each value in d, if it's larger than d_min but smaller than d_mean, assign 25 to the corresponding value in f.\n", + "f[(d > d_min) & (d < d_mean)] = 25\n", + "\n", + "#If a value in d is larger than d_mean but smaller than d_max, assign 75 to the corresponding value in f.\n", + "f[(d > d_mean) & (d < d_max)] = 75\n", + "\n", + "#If a value equals to d_mean, assign 50 to the corresponding value in f.\n", + "f[(d == d_mean)] = 50\n", + "\n", + "#Assign 0 to the corresponding value(s) in f for d_min in d.\n", + "f[(d == d_min)] = 0\n", + "\n", + "#Assign 100 to the corresponding value(s) in f for d_max in d.\n", + "f[(d == d_max)] = 100\n", + "\n", + "#In the end, f should have only the following values: 0, 25, 50, 75, and 100.\n" ] }, { @@ -325,11 +632,81 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "d =\n", + " [[[1.75111179 1.81716527 1.05349836 1.38187378 1.27600066]\n", + " [1.52183416 1.44593848 1.41795513 1.47783589 1.97640373]\n", + " [1.98619708 1.62085297 1.31020473 1.32496559 1.5449292 ]]\n", + "\n", + " [[1.0547659 1.02187434 1.51043675 1.25615015 1.27199069]\n", + " [1.40873704 1.62728534 1.88716142 1.1829674 1.38065654]\n", + " [1.51145931 1.35528294 1.09856278 1.22368068 1.32731886]]]\n", + "\n", + "f =\n", + " [[[ 75. 75. 25. 25. 25.]\n", + " [ 75. 75. 25. 75. 75.]\n", + " [100. 75. 25. 25. 75.]]\n", + "\n", + " [[ 25. 0. 75. 25. 25.]\n", + " [ 25. 75. 75. 25. 25.]\n", + " [ 75. 25. 25. 25. 25.]]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "print(\"d =\\n\", d)\n", + "print(\"\\nf =\\n\", f)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "All values in f are correctly labeled: True\n" + ] + } + ], + "source": [ + "# Check whether each value in f matches what it *should* be based on d conditions (above exercise)\n", + "f_check = np.empty_like(f)\n", + "f_check[(d == d_min)] = 0\n", + "f_check[(d > d_min) & (d < d_mean)] = 25\n", + "f_check[(d == d_mean)] = 50\n", + "f_check[(d > d_mean) & (d < d_max)] = 75\n", + "f_check[(d == d_max)] = 100\n", + "\n", + "# Compare f_check to your f\n", + "is_correct = np.array_equal(f, f_check)\n", + "print(\"All values in f are correctly labeled:\", is_correct)\n" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "### [your code here]\n" + "17. Explination: the return is as expected.\n", + "\n", + "For example, we can visually inspect it as well:\n", + "\n", + " d[0,0,2] = 1.05349836 (first block (index 0), 1st row (index 0), column 3 (index 2)) vs f[0,0,2] = 25 (same position logic)\n", + "\n", + "We know that d_max = 1.9861970785312588\n", + "d_min = 1.0218743448523924\n", + "d_mean = 1.434169898846247\n", + "\n", + "This corresponds to condition where the value is larger than d_min and less than d_mean, so f should be 25! (f[(d > d_min) & (d < d_mean)] = 25)." ] }, { @@ -350,17 +727,91 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['D' 'D' 'B' 'B' 'B']\n", + " ['D' 'D' 'B' 'D' 'D']\n", + " ['E' 'D' 'B' 'B' 'D']]\n", + "\n", + " [['B' 'A' 'D' 'B' 'B']\n", + " ['B' 'D' 'D' 'B' 'B']\n", + " ['D' 'B' 'B' 'B' 'B']]]\n" + ] + } + ], + "source": [ + "### [your code here]\n", + "#Creates a new empty array called f_str with the same shape as d and define that it will hold strings now.\n", + "f_str = np.empty(d.shape, dtype=str)\n", + "\n", + "#look at positions in f where the values are 0 (value in d was equal to d_min as specified above), replace with string \"A\"\n", + "f_str[f == 0] = \"A\"\n", + "\n", + "#for the rest of these, same logic, just look up the specified conditions. \n", + "#f_check[(d > d_min) & (d < d_mean)] = 25 = B\n", + "f_str[f == 25] = \"B\"\n", + "\n", + "#f_check[(d == d_mean)] = 50\n", + "f_str[f == 50] = \"C\"\n", + "\n", + "#f_check[(d > d_mean) & (d < d_max)] = 75\n", + "f_str[f == 75] = \"D\"\n", + "\n", + "#f_check[(d == d_max)] = 100\n", + "f_str[f == 100] = \"E\"\n", + "\n", + "#print it to verify if it matches the abvoe expected output\n", + "print(f_str)\n", + "\n" + ] + }, + { + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "### [your code here]" + "the above dont seem to match the expected visually nor with the check. I won't go too far into trouble shooting since its a bonus question and I don't want to lose too much time :)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "#Check if values match:\n", + "\n", + "#define expected\n", + "expected_f_str = np.array([\n", + " [['D', 'D', 'D', 'B', 'D'],\n", + " ['D', 'D', 'B', 'B', 'B'],\n", + " ['D', 'B', 'D', 'D', 'D']],\n", + "\n", + " [['B', 'B', 'B', 'B', 'E'],\n", + " ['D', 'D', 'D', 'D', 'D'],\n", + " ['B', 'D', 'A', 'D', 'D']]\n", + "])\n", + "\n", + "#print true if matching expected\n", + "print(np.array_equal(f_str, expected_f_str))\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -374,7 +825,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.12.7" } }, "nbformat": 4,