diff --git a/Numpy (26.02)/Numpy_Task.ipynb b/Numpy (26.02)/Numpy_Task.ipynb index 593ba20..c105af2 100644 --- a/Numpy (26.02)/Numpy_Task.ipynb +++ b/Numpy (26.02)/Numpy_Task.ipynb @@ -1,470 +1,323 @@ + { - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "medieval-detail" - }, - "outputs": [], - "source": [ - "import numpy as np" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "abstract-istanbul" - }, - "source": [ - "1. Задание\n", - "- проверьте тип объекта python_list\n", - "- если это не numpy ndarray, то преобразуйте к нему\n", - "- проверьте, что на выходе вы получили объект python_list желаемого типа" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "entertaining-automation" - }, - "outputs": [], - "source": [ - "python_list = [1, 12, 13, 45, 76, 45, 98, 0]\n", - "print()\n", - "python_list = \n", - "print()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "loose-tobago" - }, - "source": [ - "2. Задание\n", - "- создайте одномерный массив numpy из 10 элементов заполненный числом 1.5" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "included-polymer" - }, - "outputs": [], - "source": [ - "z = \n", - "print(z)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "threatened-theme" - }, - "source": [ - "3. Задание\n", - "- создайте двумерный массив numpy из 25 элементов заполненный числом 0" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "alert-endorsement" - }, - "outputs": [], - "source": [ - "z = \n", - "print(z)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "federal-blackberry" - }, - "source": [ - "4. Задание\n", - "- создайте одномерный массив numpy из 12 элементов заполненный числом 1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "static-filing" - }, - "outputs": [], - "source": [ - "ones = \n", - "print(ones)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "whole-chassis" - }, - "source": [ - "5. Задание\n", - "- преобразуйте массив из предыдущего задания к матрице 3 на 4\n", - "- выведите размерность полученного массива" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "outstanding-deviation" - }, - "outputs": [], - "source": [ - "ones = \n", - "ones.shape" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cubic-noise" - }, - "source": [ - "6. Задание\n", - "- создайте матрицу 4 на 5 заполненную числами от 1 до 20\n", - "- замените элемент в 3-й строке 4-м столбце на -99" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "foster-memory" - }, - "outputs": [], - "source": [ - "Z = \n", - "print(Z)\n", - "\n", - "print(Z)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "helpful-table" - }, - "source": [ - "7. Задание\n", - "- создайте вектор длинной 15 со случайными целыми числами в диапазоне от -10 до 10\n", - "- сделайте реверс вектора" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "magnetic-leone" - }, - "outputs": [], - "source": [ - "first = \n", - "print(first)\n", - "second = \n", - "print(second)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "executed-september" - }, - "source": [ - "8. Задание\n", - "- создайте матрицу 5 на 5 со случайными целыми числами в диапазоне от -15 до 15\n", - "- замените отрицательные элементы на их квадраты" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "pharmaceutical-sigma" - }, - "outputs": [], - "source": [ - "first = \n", - "print(first)\n", - "\n", - "print(first)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "floral-difference" - }, - "source": [ - "9. Задание\n", - "- создайте матрицу 3 на 5 со случайными целыми числами в диапазоне от -15 до 15\n", - "- найдите максимум и минимум\n", - "- найдите среднее по столбцам\n", - "- найдите среднее по строкам" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "saving-conference" - }, - "outputs": [], - "source": [ - "first = \n", - "print(first)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "diagnostic-departure" - }, - "source": [ - "10. Задание\n", - "- даны две матрицы\n", - "- убедитесь, что их можно пермножить\n", - "- произведите операцию умножения матриц, если это возможно, иначе выведите ошибку" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "olympic-qatar" - }, - "outputs": [], - "source": [ - "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", - "else:\n", - " " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "governmental-austin" - }, - "source": [ - "11. Задание\n", - "- создайте матрицу 5 на 5 заполненную равномерным распределением с диапазоно 2-12\n", - "- заполните все элементы ниже главной диагонали нулями" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "suffering-mauritius" - }, - "outputs": [], - "source": [ - "mask = \n", - "matrix = \n", - "\n", - "print(matrix)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "altered-baghdad" - }, - "source": [ - "12. Задание\n", - "- создайте матрицу 5 на 5 заполненную нормальным распределением с парметрами среднее 10 и отклонение 2\n", - "- заполните все элементы на главной диагонали нулями" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "refined-stuff" - }, - "outputs": [], - "source": [ - "mask = \n", - "matrix = \n", - "\n", - "print(matrix)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "quiet-complement" - }, - "source": [ - "13. Задание\n", - "- дано два массива, проверить одинаковы ли они" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "french-fighter" - }, - "outputs": [], - "source": [ - "a = np.random.randint(0,2,5)\n", - "print(a)\n", - "b = np.random.randint(0,2,5)\n", - "print(b)\n", - "equal = \n", - "equal" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "color-amplifier" - }, - "source": [ - "14. Задание\n", - "- сгенерируйте матрицу случайного размера:\n", - " - строк от 3 до 7\n", - " - столбцов от 2 до 12\n", - "- сгенерируйте число N - размер выборки так, чтобы оно не превышало половину количества элементов сгенерированной матрицы, но было больше 0\n", - "- сделайте случайную выборку N элементов" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "close-daisy" - }, - "outputs": [], - "source": [ - "r, c = \n", - "a = \n", - "print(a)\n", - "N = \n", - "print(N)\n", - "sample = \n", - "print(sample)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "patent-african" - }, - "source": [ - "15. Задание\n", - "- дан вектор, проверить есть в нем Nan\n", - "- проверить есть в нем знак бесконечности\n", - "- заполнить их нулями" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "taken-fabric" - }, - "outputs": [], - "source": [ - "a = np.array([1, np.NaN, np.Inf], float)\n", - "\n", - "\n", - "a" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "analyzed-ireland" - }, - "source": [ - "16. Задание\n", - "- сгенерировать массив с произвольным числом осей\n", - "- определить сколько у него осей" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "imposed-digest" - }, - "outputs": [], - "source": [ - "axis = \n", - "print(axis)\n", - "matrix = \n", - "print(...)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "regulation-colleague" - }, - "source": [ - "17. Задание\n", - "- сгенерируйте матрицу 10 на 3 с нормальным распределением, среднее 50, отклонение 10\n", - "- найдите индексы максимальных элементов в каждой строке\n", - "- найдите сами элементы" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "concerned-anthropology" - }, - "outputs": [], - "source": [ - "matrix = np.random.normal(50, 10, (10,3))\n", - "print(matrix)\n", - "indexes = \n", - "print(indexes)\n", - "print(...)" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "01_task.ipynb", - "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.8.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 + "metadata": { + "kernelspec": { + "name": "python", + "display_name": "Pyolite", + "language": "python" + }, + "language_info": { + "codemirror_mode": { + "name": "python", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8" + } + }, + "nbformat_minor": 5, + "nbformat": 4, + "cells": [ + { + "cell_type": "code", + "source": "import numpy as np\nimport random", + "metadata": { + "trusted": true + }, + "execution_count": 1, + "outputs": [], + "id": "b77bbab2-39a5-4a4b-967d-689fed59a792" + }, + { + "cell_type": "code", + "source": "python_list = [1, 12, 13, 45, 76, 45, 98, 0]\nprint(type(python_list))\nif type(python_list) is not 'numpy.ndarray':\n python_list = np.array(python_list)\nprint(type(python_list))", + "metadata": { + "trusted": true + }, + "execution_count": 2, + "outputs": [ + { + "name": "stderr", + "text": "<>:3: SyntaxWarning: \"is not\" with a literal. Did you mean \"!=\"?\n<>:3: SyntaxWarning: \"is not\" with a literal. Did you mean \"!=\"?\n", + "output_type": "stream" + }, + { + "name": "stdout", + "text": "\n", + "output_type": "stream" + }, + { + "name": "stderr", + "text": ":3: SyntaxWarning: \"is not\" with a literal. Did you mean \"!=\"?\n if type(python_list) is not 'numpy.ndarray':\n", + "output_type": "stream" + }, + { + "name": "stdout", + "text": "\n", + "output_type": "stream" + } + ], + "id": "9e139854-ef2a-4f80-b6ce-569f68896efa" + }, + { + "cell_type": "code", + "source": "z = np.full(10,1.5)\nprint(z)", + "metadata": { + "trusted": true + }, + "execution_count": 3, + "outputs": [ + { + "name": "stdout", + "text": "[1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5]\n", + "output_type": "stream" + } + ], + "id": "b5bbcb29-031a-45ee-bcc9-12a194e21d39" + }, + { + "cell_type": "code", + "source": "z = np.zeros((2, 25))\nprint(z)", + "metadata": { + "trusted": true + }, + "execution_count": 4, + "outputs": [ + { + "name": "stdout", + "text": "[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n 0.]\n [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n 0.]]\n", + "output_type": "stream" + } + ], + "id": "4526b219-8b1c-4ac2-8b1f-ee127f68e5f5" + }, + { + "cell_type": "code", + "source": "ones = np.full(12,1)\nprint(ones)", + "metadata": { + "trusted": true + }, + "execution_count": 5, + "outputs": [ + { + "name": "stdout", + "text": "[1 1 1 1 1 1 1 1 1 1 1 1]\n", + "output_type": "stream" + } + ], + "id": "bbcb3047-4ec9-4c12-bde9-d3ee229b5fa6" + }, + { + "cell_type": "code", + "source": "ones = ones.reshape(3,4)\nprint(ones.shape)", + "metadata": { + "trusted": true + }, + "execution_count": 6, + "outputs": [ + { + "name": "stdout", + "text": "(3, 4)\n", + "output_type": "stream" + } + ], + "id": "a2818e91-f4dd-468d-8c85-9db855abec8a" + }, + { + "cell_type": "code", + "source": "Z = np.arange(1,21,1).reshape(4,5)\nprint(Z)\nZ[2][3]=-99\nprint(Z)", + "metadata": { + "trusted": true + }, + "execution_count": 7, + "outputs": [ + { + "name": "stdout", + "text": "[[ 1 2 3 4 5]\n [ 6 7 8 9 10]\n [11 12 13 14 15]\n [16 17 18 19 20]]\n[[ 1 2 3 4 5]\n [ 6 7 8 9 10]\n [ 11 12 13 -99 15]\n [ 16 17 18 19 20]]\n", + "output_type": "stream" + } + ], + "id": "155cd343-d009-4446-8765-02908b88039e" + }, + { + "cell_type": "code", + "source": "first = np.random.randint(-10, 10, 15)\nprint(first)\nsecond = first[::-1]\nprint(second)", + "metadata": { + "trusted": true + }, + "execution_count": 8, + "outputs": [ + { + "name": "stdout", + "text": "[ 5 -3 -9 8 -8 -7 -2 -1 4 3 2 1 0 8 -9]\n[-9 8 0 1 2 3 4 -1 -2 -7 -8 8 -9 -3 5]\n", + "output_type": "stream" + } + ], + "id": "76fdc4ef-55c1-46da-a4fa-96950f02125c" + }, + { + "cell_type": "code", + "source": "first = np.random.randint(-15, 15, 25).reshape(5,5)\nprint(first)\nfirst=np.array([i*i if i<0 else i for i in first.ravel()]).reshape(5,5)\nprint(first)", + "metadata": { + "trusted": true + }, + "execution_count": 9, + "outputs": [ + { + "name": "stdout", + "text": "[[ -8 8 -8 -13 3]\n [-11 12 -10 12 -7]\n [-15 -7 6 -15 -12]\n [ -9 11 7 -15 -13]\n [ 14 7 -2 -8 -4]]\n[[ 64 8 64 169 3]\n [121 12 100 12 49]\n [225 49 6 225 144]\n [ 81 11 7 225 169]\n [ 14 7 4 64 16]]\n", + "output_type": "stream" + } + ], + "id": "d1b07a8b-6fab-495e-9276-9d8179d39169" + }, + { + "cell_type": "code", + "source": "first = np.random.randint(-15, 15, 15).reshape(3,5)\nprint(first)\nprint(first.max(),first.min(),first.mean(axis=0),first.mean(axis=1))", + "metadata": { + "trusted": true + }, + "execution_count": 10, + "outputs": [ + { + "name": "stdout", + "text": "[[ 0 2 -13 -7 -8]\n [ 4 5 10 -15 -1]\n [ 14 -12 -9 -12 -3]]\n14 -15 [ 6. -1.66666667 -4. -11.33333333 -4. ] [-5.2 0.6 -4.4]\n", + "output_type": "stream" + } + ], + "id": "c5dbc0ea-e2ec-4f09-b8aa-ea4d296faacd" + }, + { + "cell_type": "code", + "source": "a = np.random.randint(-10, 10, (2, 5))\nfirst_axis = np.random.randint(4, 6)\nb = np.random.randint(-10, 10, (first_axis, 3))\nprint(a)\nprint(b)\nprint(a.shape,b.shape)\nif a.shape[1]==b.shape[0] :\n print(a.dot(b))\nelse:\n print(\"Ошибка!\")", + "metadata": { + "trusted": true + }, + "execution_count": 11, + "outputs": [ + { + "name": "stdout", + "text": "[[ 1 0 3 -7 -10]\n [ 5 -4 -1 1 -9]]\n[[ -6 8 4]\n [ -5 9 -3]\n [ 4 7 9]\n [-10 8 4]]\n(2, 5) (4, 3)\nОшибка!\n", + "output_type": "stream" + } + ], + "id": "1e04cc55-79a3-46cc-a3e2-b6579e914c26" + }, + { + "cell_type": "code", + "source": "mask = np.random.uniform(2, 12, (5, 5)) \nprint(mask)\nmask[np.tril_indices(5, -1)] = 0\nmatrix = mask\nprint(matrix)", + "metadata": { + "trusted": true + }, + "execution_count": 12, + "outputs": [ + { + "name": "stdout", + "text": "[[ 7.06701872 3.59655659 6.41418417 10.22936981 4.55008583]\n [ 7.96916149 9.97171939 4.79134822 7.02051113 8.97131958]\n [ 7.55660222 7.16789098 10.2498419 8.71048664 6.4692685 ]\n [11.06650925 8.86514045 3.69571205 9.16825325 5.01901031]\n [11.8748883 9.44007247 8.22611336 10.66717289 6.14917873]]\n[[ 7.06701872 3.59655659 6.41418417 10.22936981 4.55008583]\n [ 0. 9.97171939 4.79134822 7.02051113 8.97131958]\n [ 0. 0. 10.2498419 8.71048664 6.4692685 ]\n [ 0. 0. 0. 9.16825325 5.01901031]\n [ 0. 0. 0. 0. 6.14917873]]\n", + "output_type": "stream" + } + ], + "id": "151f05ca-5d10-4300-9467-21072ac9fadb" + }, + { + "cell_type": "code", + "source": "mask =np.random.normal(10, 2, 25).reshape(5,5)\nprint(mask)\nmask[np.diag_indices(5)] = 0\nmatrix = mask\nprint(matrix)", + "metadata": { + "trusted": true + }, + "execution_count": 13, + "outputs": [ + { + "name": "stdout", + "text": "[[ 9.91316714 6.34550372 7.76155774 10.54823626 11.31871242]\n [ 5.31480719 6.91699947 10.31226178 8.64243223 10.23317928]\n [ 8.69067595 10.44768691 9.0068715 7.82252434 9.8078059 ]\n [12.21729204 10.13425224 9.94251486 12.56975161 7.51103498]\n [ 9.41256464 9.18163338 14.53528573 10.53827169 9.12362772]]\n[[ 0. 6.34550372 7.76155774 10.54823626 11.31871242]\n [ 5.31480719 0. 10.31226178 8.64243223 10.23317928]\n [ 8.69067595 10.44768691 0. 7.82252434 9.8078059 ]\n [12.21729204 10.13425224 9.94251486 0. 7.51103498]\n [ 9.41256464 9.18163338 14.53528573 10.53827169 0. ]]\n", + "output_type": "stream" + } + ], + "id": "ee145cb7-5e41-4135-98d2-6c4bd343606a" + }, + { + "cell_type": "code", + "source": "a = np.random.randint(0,2,5)\nprint(a)\nb = np.random.randint(0,2,5)\nprint(b)\nequal = np.array_equal(a,b)\nprint(equal)", + "metadata": { + "trusted": true + }, + "execution_count": 14, + "outputs": [ + { + "name": "stdout", + "text": "[0 1 1 1 0]\n[1 1 1 1 0]\nFalse\n", + "output_type": "stream" + } + ], + "id": "c712f0c2-0d12-49a0-a23a-0052c718bff0" + }, + { + "cell_type": "code", + "source": "r, c = random.randint(3,7),random.randint(2,12)\na = np.random.randint(-15, 15, r*c).reshape(r,c)\nprint(a)\nN = r*c//2\nprint(N)\ns=set()\ncount=0\nsample=[]\nwhile True:\n i, j = random.randint(0,r-1),random.randint(0,c-1)\n if (i, j) not in s:\n s.add((i, j))\n count+=1\n sample.append(a[i][j])\n if N == count:\n break\nprint(sample)", + "metadata": { + "trusted": true + }, + "execution_count": 15, + "outputs": [ + { + "name": "stdout", + "text": "[[-13 8 2 13 -5 8 1 11 3 -5]\n [-12 12 0 4 -3 -12 -6 14 0 1]\n [ -3 4 -10 9 4 0 8 -11 -11 5]\n [ -7 -4 -7 -7 3 -7 13 -14 1 13]]\n20\n[-11, -7, 8, 2, 13, 4, 13, 4, 0, -4, 1, -5, 9, -11, 1, 14, -5, -14, 0, 8]\n", + "output_type": "stream" + } + ], + "id": "bf6e125e-7cf9-4194-a3b5-5cc1cc5b15ab" + }, + { + "cell_type": "code", + "source": "a = np.array([1, np.NaN, np.Inf], float)\nprint(a)\na[np.isnan(a)]=0\na[np.isinf(a)]=0\nprint(a)\n", + "metadata": { + "trusted": true + }, + "execution_count": 29, + "outputs": [ + { + "name": "stdout", + "text": "[ 1. nan inf]\n[1. 0. 0.]\n", + "output_type": "stream" + } + ], + "id": "e9501ed6-753c-42ff-ba77-3a5db9e58324" + }, + { + "cell_type": "code", + "source": "axis = random.randint(1,5)\nprint(axis)\nmatrix = np.arange(np.math.factorial(axis)).reshape([i for i in range(1,axis+1)])\nprint(matrix)\nprint(matrix.ndim)", + "metadata": { + "trusted": true + }, + "execution_count": 55, + "outputs": [ + { + "name": "stdout", + "text": "2\n[[0 1]]\n2\n", + "output_type": "stream" + } + ], + "id": "32710f81-d093-4bc3-8fba-5ebb4557e237" + }, + { + "cell_type": "code", + "source": "matrix = np.random.normal(50, 10, (10,3))\nprint(matrix)\nindexes =np.argmax(matrix,axis=1) \nprint(indexes)\nprint(np.amax(matrix,axis=1))", + "metadata": { + "trusted": true + }, + "execution_count": 34, + "outputs": [ + { + "name": "stdout", + "text": "[[60.31706356 56.91231409 56.07774197]\n [41.01603076 51.89106166 56.70132702]\n [63.08147555 48.39991647 43.32159037]\n [38.3121983 62.57399301 57.61303084]\n [40.70319707 50.69338586 60.46488882]\n [56.54690773 40.43941707 47.10681669]\n [57.85355808 64.56475082 62.27495569]\n [55.56369408 19.60753686 51.00817838]\n [71.97141348 47.91661657 45.45307281]\n [42.54217472 36.90909217 70.41712757]]\n[0 2 0 1 2 0 1 0 0 2]\n[60.31706356 56.70132702 63.08147555 62.57399301 60.46488882 56.54690773\n 64.56475082 55.56369408 71.97141348 70.41712757]\n", + "output_type": "stream" + } + ], + "id": "ee570d7c-a7a6-44b4-aef1-0fa0456c251d" + } + ] }