diff --git a/Numpy (26.02)/Numpy_Task.ipynb b/Numpy (26.02)/Numpy_Task.ipynb index 593ba20..84205c5 100644 --- a/Numpy (26.02)/Numpy_Task.ipynb +++ b/Numpy (26.02)/Numpy_Task.ipynb @@ -3,6 +3,7 @@ { "cell_type": "code", "execution_count": null, + "id": "cceba168", "metadata": { "id": "medieval-detail" }, @@ -13,6 +14,7 @@ }, { "cell_type": "markdown", + "id": "6a6688d3", "metadata": { "id": "abstract-istanbul" }, @@ -25,20 +27,43 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "entertaining-automation" - }, - "outputs": [], + "execution_count": 1, + "id": "ac44c5bd", + "metadata": { + "id": "entertaining-automation", + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'np' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mpython_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpython_list\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpython_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"numpy.ndarray\"\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32melse\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpython_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpython_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ], "source": [ "python_list = [1, 12, 13, 45, 76, 45, 98, 0]\n", "print()\n", - "python_list = \n", - "print()" + "python_list = python_list if type(python_list) == \"numpy.ndarray\" \\\n", + " else np.array(python_list)\n", + "print(type(python_list))" ] }, { "cell_type": "markdown", + "id": "3b18909f", "metadata": { "id": "loose-tobago" }, @@ -50,17 +75,19 @@ { "cell_type": "code", "execution_count": null, + "id": "10635970", "metadata": { "id": "included-polymer" }, "outputs": [], "source": [ - "z = \n", + "z = np.array([1.5 for i in range(10)])\n", "print(z)" ] }, { "cell_type": "markdown", + "id": "18d509de", "metadata": { "id": "threatened-theme" }, @@ -72,17 +99,19 @@ { "cell_type": "code", "execution_count": null, + "id": "7c914b1a", "metadata": { "id": "alert-endorsement" }, "outputs": [], "source": [ - "z = \n", + "z = np.zeros(25)\n", "print(z)" ] }, { "cell_type": "markdown", + "id": "dfb9593b", "metadata": { "id": "federal-blackberry" }, @@ -94,17 +123,19 @@ { "cell_type": "code", "execution_count": null, + "id": "4690a3cc", "metadata": { "id": "static-filing" }, "outputs": [], "source": [ - "ones = \n", + "ones = np.ones(12)\n", "print(ones)" ] }, { "cell_type": "markdown", + "id": "04a0f114", "metadata": { "id": "whole-chassis" }, @@ -117,17 +148,19 @@ { "cell_type": "code", "execution_count": null, + "id": "18788760", "metadata": { "id": "outstanding-deviation" }, "outputs": [], "source": [ - "ones = \n", + "ones = ones.reshape((3,4))\n", "ones.shape" ] }, { "cell_type": "markdown", + "id": "ae64a711", "metadata": { "id": "cubic-noise" }, @@ -140,19 +173,21 @@ { "cell_type": "code", "execution_count": null, + "id": "49c38501", "metadata": { "id": "foster-memory" }, "outputs": [], "source": [ - "Z = \n", + "Z = np.arange(1, 21, 1).reshape(4,5)\n", "print(Z)\n", - "\n", + "Z[2, 3] = -99\n", "print(Z)" ] }, { "cell_type": "markdown", + "id": "d0f92dd1", "metadata": { "id": "helpful-table" }, @@ -165,19 +200,21 @@ { "cell_type": "code", "execution_count": null, + "id": "7d707a34", "metadata": { "id": "magnetic-leone" }, "outputs": [], "source": [ - "first = \n", + "first = np.random.randint(-10, 11, 15)\n", "print(first)\n", - "second = \n", + "second = first[::-1]\n", "print(second)" ] }, { "cell_type": "markdown", + "id": "30799b42", "metadata": { "id": "executed-september" }, @@ -190,19 +227,21 @@ { "cell_type": "code", "execution_count": null, + "id": "9b9651b4", "metadata": { "id": "pharmaceutical-sigma" }, "outputs": [], "source": [ - "first = \n", + "first = np.random.randint(-15, 16, 25).reshape(5, 5)\n", "print(first)\n", - "\n", + "first = np.array([x ** 2 if x < 0 else x for x in first.flatten()]).reshape(5, 5)\n", "print(first)" ] }, { "cell_type": "markdown", + "id": "a9e69478", "metadata": { "id": "floral-difference" }, @@ -217,17 +256,22 @@ { "cell_type": "code", "execution_count": null, + "id": "5e0abcf3", "metadata": { "id": "saving-conference" }, "outputs": [], "source": [ - "first = \n", - "print(first)\n" + "first = np.random.randint(-15, 16, 15).reshape(3, 5)\n", + "print(first)\n", + "print(\"Max: \", first.max(), \"Min: \", first.min())\n", + "print(\"AvgCols: \", first.mean(axis=0))\n", + "print(\"AvgRows: \", first.mean(axis=1))" ] }, { "cell_type": "markdown", + "id": "57a99904", "metadata": { "id": "diagnostic-departure" }, @@ -241,6 +285,7 @@ { "cell_type": "code", "execution_count": null, + "id": "ae9efb00", "metadata": { "id": "olympic-qatar" }, @@ -249,14 +294,15 @@ "a = np.random.randint(-10, 10, (2, 5))\n", "first_axis = np.random.randint(4, 6)\n", "b = np.random.randint(-10, 10, (first_axis, 3))\n", - "if :\n", - " print(a @ b)\n", + "if a.shape[1] == b.shape[0]:\n", + " print(a.dot(b))\n", "else:\n", - " " + " raise Exception()" ] }, { "cell_type": "markdown", + "id": "4234d89f", "metadata": { "id": "governmental-austin" }, @@ -269,19 +315,23 @@ { "cell_type": "code", "execution_count": null, + "id": "de154fa9", "metadata": { "id": "suffering-mauritius" }, "outputs": [], "source": [ - "mask = \n", - "matrix = \n", + "mask = np.random.uniform(2, 12, (5, 5))\n", + "print(mask)\n", "\n", + "matrix = np.copy(mask)\n", + "matrix[np.tril_indices(matrix.shape[0], -1)] = 0\n", "print(matrix)" ] }, { "cell_type": "markdown", + "id": "eeb9163f", "metadata": { "id": "altered-baghdad" }, @@ -294,19 +344,21 @@ { "cell_type": "code", "execution_count": null, + "id": "bf4bf650", "metadata": { "id": "refined-stuff" }, "outputs": [], "source": [ - "mask = \n", - "matrix = \n", - "\n", + "mask = np.random.normal(10, 2, (5, 5))\n", + "matrix = np.copy(mask)\n", + "matrix[np.diag_indices(mask.shape[0])] = 0\n", "print(matrix)" ] }, { "cell_type": "markdown", + "id": "b297487b", "metadata": { "id": "quiet-complement" }, @@ -318,6 +370,7 @@ { "cell_type": "code", "execution_count": null, + "id": "0c6f03c3", "metadata": { "id": "french-fighter" }, @@ -327,12 +380,13 @@ "print(a)\n", "b = np.random.randint(0,2,5)\n", "print(b)\n", - "equal = \n", + "equal = np.array_equal(a, b)\n", "equal" ] }, { "cell_type": "markdown", + "id": "582bcbcf", "metadata": { "id": "color-amplifier" }, @@ -348,22 +402,24 @@ { "cell_type": "code", "execution_count": null, + "id": "1b6adc2c", "metadata": { "id": "close-daisy" }, "outputs": [], "source": [ - "r, c = \n", - "a = \n", + "r, c = (np.random.randint(3, 8), np.random.randint(2, 13))\n", + "a = np.random.sample((r, c))\n", "print(a)\n", - "N = \n", + "N = np.random.randint(1, r * c / 2)\n", "print(N)\n", - "sample = \n", + "sample = np.random.choice(a.flatten(), N)\n", "print(sample)" ] }, { "cell_type": "markdown", + "id": "9d033f71", "metadata": { "id": "patent-african" }, @@ -377,19 +433,21 @@ { "cell_type": "code", "execution_count": null, + "id": "706e4752", "metadata": { "id": "taken-fabric" }, "outputs": [], "source": [ "a = np.array([1, np.NaN, np.Inf], float)\n", - "\n", - "\n", + "a[np.isnan(a)] = 0\n", + "a[np.isinf(a)] = 0\n", "a" ] }, { "cell_type": "markdown", + "id": "f9c075e8", "metadata": { "id": "analyzed-ireland" }, @@ -402,19 +460,21 @@ { "cell_type": "code", "execution_count": null, + "id": "0cb23382", "metadata": { "id": "imposed-digest" }, "outputs": [], "source": [ - "axis = \n", + "axis = np.random.randint(2, 5, np.random.randint(2, 6))\n", "print(axis)\n", - "matrix = \n", - "print(...)" + "matrix = np.random.sample(axis)\n", + "print(len(matrix.shape))" ] }, { "cell_type": "markdown", + "id": "a81cdf7a", "metadata": { "id": "regulation-colleague" }, @@ -428,6 +488,7 @@ { "cell_type": "code", "execution_count": null, + "id": "15e02c68", "metadata": { "id": "concerned-anthropology" }, @@ -435,9 +496,9 @@ "source": [ "matrix = np.random.normal(50, 10, (10,3))\n", "print(matrix)\n", - "indexes = \n", + "indexes = np.argmax(matrix, axis=1)\n", "print(indexes)\n", - "print(...)" + "print([matrix[i, j] for i, j in enumerate(indexes)])" ] } ], @@ -462,7 +523,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.5" + "version": "3.6.15" } }, "nbformat": 4,