diff --git a/your-code/main.ipynb b/your-code/main.ipynb index e66d6ce..94a8270 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "import numpy as np" ] }, { @@ -28,11 +28,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.23.5\n" + ] + } + ], "source": [ - "# your code here" + "print(np.__version__)" ] }, { @@ -45,29 +53,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ - "# Method 1" + "# syntax: random.randint(low, high=None, size=None, dtype=int); if high is None (the default), then results are from [0, low)\n", + "# integers, from \"discrete uniform\" distribution, [low, high)\n", + "\n", + "a = np.random.randint(100, size = (2, 3, 5))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ - "# Method 2" + "# syntax: random.rand(d0, d1, ..., dn)\n", + "# floats, from uniform distribution, [0, 1)\n", + "\n", + "a = np.random.rand(2, 3, 5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# syntax: random.randn(d0, d1, ..., dn)\n", + "# floats, from \"standard normal\" distribution\n", + "\n", + "a = np.random.randn(2, 3, 5)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ - "# Method 3" + "# syntax: random.random_sample(size=None)\n", + "# floats, \"continuous uniform\" distribtuion, [0.0, 1.0)\n", + "\n", + "a = np.random.random_sample(size = (2, 3, 5))" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "# syntax: random.random(size=None)\n", + "# floats, [0.0, 1.0)\n", + "\n", + "a = np.random.random(size = (2, 3, 5))" ] }, { @@ -79,11 +122,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.41685763 0.86218193 0.40424214 0.65916744 0.38822751]\n", + " [0.40456356 0.6913081 0.72844646 0.045383 0.50828095]\n", + " [0.376908 0.09780231 0.83434537 0.75355341 0.95188708]]\n", + "\n", + " [[0.46131671 0.32621905 0.89250888 0.74166322 0.90079063]\n", + " [0.78921746 0.42424457 0.9193221 0.60638787 0.24101044]\n", + " [0.41183555 0.55754807 0.38693262 0.37329723 0.90172869]]]\n" + ] + } + ], "source": [ - "# your code here" + "print(a)" ] }, { @@ -95,11 +152,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# syntax: ones(shape, dtype=None, order=\"C\", *, like=None)\n", + "\n", + "b = np.ones(shape = (5, 2, 3), dtype = int)" ] }, { @@ -111,11 +170,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 58, + "metadata": {}, + "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": [ + "print(b)" ] }, { @@ -127,11 +207,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.size == b.size\n", + "\n", + "# yes, a and b have the same size" ] }, { @@ -143,11 +236,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your answer here" + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "operands could not be broadcast together with shapes (2,3,5) (5,2,3) ", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[61], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n", + "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,5) (5,2,3) " + ] + } + ], + "source": [ + "a + b\n", + "\n", + "# no you cannot add a and b because they have a different shape" ] }, { @@ -159,11 +266,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# syntax: transpose(a, axes=None)\n", + "# axes: tuple or list of ints, optional\n", + "# if specified, it must be a tuple or list which contains a permutation of [0,1,…,N-1] where N is the number of axes of a\n", + "# The i’th axis of the returned array will correspond to the axis numbered axes[i] of the input\n", + "\n", + "c = np.transpose(b, axes=[1, 2, 0]) # i could also do c = b.reshape(a.shape)" ] }, { @@ -175,11 +287,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, "outputs": [], "source": [ - "# your code/answer here" + "d = a + c\n", + "\n", + "# it works because a and c have the same shape" ] }, { @@ -191,11 +305,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[0.41685763 0.86218193 0.40424214 0.65916744 0.38822751]\n", + " [0.40456356 0.6913081 0.72844646 0.045383 0.50828095]\n", + " [0.376908 0.09780231 0.83434537 0.75355341 0.95188708]]\n", + "\n", + " [[0.46131671 0.32621905 0.89250888 0.74166322 0.90079063]\n", + " [0.78921746 0.42424457 0.9193221 0.60638787 0.24101044]\n", + " [0.41183555 0.55754807 0.38693262 0.37329723 0.90172869]]] \n", + " \n", + "\n", + "[[[1.41685763 1.86218193 1.40424214 1.65916744 1.38822751]\n", + " [1.40456356 1.6913081 1.72844646 1.045383 1.50828095]\n", + " [1.376908 1.09780231 1.83434537 1.75355341 1.95188708]]\n", + "\n", + " [[1.46131671 1.32621905 1.89250888 1.74166322 1.90079063]\n", + " [1.78921746 1.42424457 1.9193221 1.60638787 1.24101044]\n", + " [1.41183555 1.55754807 1.38693262 1.37329723 1.90172869]]]\n" + ] + } + ], + "source": [ + "print(a, \"\\n \\n\")\n", + "\n", + "print(d)\n", + "\n", + "# the values of d differ by 1 as compared to the values of a (as d is the product of addition of c -only ones- to a)" ] }, { @@ -207,11 +348,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "e = a * c" ] }, { @@ -223,11 +364,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code/answer here" + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]],\n", + "\n", + " [[ True, True, True, True, True],\n", + " [ True, True, True, True, True],\n", + " [ True, True, True, True, True]]])" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "e == a\n", + "\n", + "# it does because c contains only ones" ] }, { @@ -239,11 +399,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "d_max = d.max()\n", + "\n", + "d_min = d.min()\n", + "\n", + "d_mean = d.mean()" ] }, { @@ -255,11 +419,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, 3, 5)" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# syntax empty(shape, dtype=float, order='C', *, like=None)\n", + "# return a new array of given shape and type, without initializing entries\n", + "\n", + "f = np.empty((d.shape))" ] }, { @@ -275,11 +453,19 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 86, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "f[(d > d_min) & (d < d_mean)] = 25\n", + "\n", + "f[(d > d_mean) & (d < d_max)] = 75\n", + "\n", + "f[d == d_mean] = 50\n", + "\n", + "f[d == d_min] = 0\n", + "\n", + "f[d == d_max] = 100" ] }, { @@ -309,11 +495,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1.41685763 1.86218193 1.40424214 1.65916744 1.38822751]\n", + " [1.40456356 1.6913081 1.72844646 1.045383 1.50828095]\n", + " [1.376908 1.09780231 1.83434537 1.75355341 1.95188708]]\n", + "\n", + " [[1.46131671 1.32621905 1.89250888 1.74166322 1.90079063]\n", + " [1.78921746 1.42424457 1.9193221 1.60638787 1.24101044]\n", + " [1.41183555 1.55754807 1.38693262 1.37329723 1.90172869]]] \n", + " \n", + "\n", + "[[[ 25. 75. 25. 75. 25.]\n", + " [ 25. 75. 75. 0. 25.]\n", + " [ 25. 25. 75. 75. 100.]]\n", + "\n", + " [[ 25. 25. 75. 75. 75.]\n", + " [ 75. 25. 75. 75. 25.]\n", + " [ 25. 25. 25. 25. 75.]]]\n" + ] + } + ], + "source": [ + "print(d, \"\\n \\n\")\n", + "\n", + "print(f)\n", + "\n", + "# yes" ] }, { @@ -333,19 +546,48 @@ "**Note**: you don't have to use Numpy in this question." ] }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[['B' 'D' 'B' 'D' 'B']\n", + " ['B' 'D' 'D' 'A' 'B']\n", + " ['B' 'B' 'D' 'D' 'E']]\n", + "\n", + " [['B' 'B' 'D' 'D' 'D']\n", + " ['D' 'B' 'D' 'D' 'B']\n", + " ['B' 'B' 'B' 'B' 'D']]]\n" + ] + } + ], + "source": [ + "f = f.astype(str)\n", + "\n", + "f[(d > d_min) & (d < d_mean)] = \"B\"\n", + "f[(d > d_mean) & (d < d_max)] = \"D\"\n", + "f[d == d_mean] = \"C\"\n", + "f[d == d_min] = \"A\"\n", + "f[d == d_max] = \"E\"\n", + "\n", + "print(f)" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [ - "# your code here" - ] + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -359,7 +601,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.10" } }, "nbformat": 4,