Skip to content

Instantly share code, notes, and snippets.

@naviarh
Created July 24, 2018 20:38
Show Gist options
  • Save naviarh/0ba4b3e45aa883673c51efda6035e752 to your computer and use it in GitHub Desktop.
Save naviarh/0ba4b3e45aa883673c51efda6035e752 to your computer and use it in GitHub Desktop.
NumPy.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "NumPy.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/gist/naviarh/0ba4b3e45aa883673c51efda6035e752/numpy.ipynb)"
]
},
{
"metadata": {
"id": "uC2778KOY--G",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"#!/usr/bin/env python3\n",
"# -*- coding: utf-8 -*-"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "Nl4iJKCOZP9A",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "238462bf-324c-4dc7-aeac-5e3b78206070"
},
"cell_type": "code",
"source": [
"# Put these at the top of every notebook, to get automatic reloading and inline plotting\n",
"%reload_ext autoreload\n",
"%autoreload 2\n",
"%matplotlib inline\n",
"!pwd"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"/content\r\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "1OG806oaZaRm",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 59
},
"outputId": "4fb6b3aa-4887-45b3-dda2-c088bb8e7f5a"
},
"cell_type": "code",
"source": [
"import subprocess, os\n",
"os.uname()"
],
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"posix.uname_result(sysname='Linux', nodename='dec86abcee79', release='4.14.33+', version='#1 SMP Wed Jun 20 01:15:52 PDT 2018', machine='x86_64')"
]
},
"metadata": {
"tags": []
},
"execution_count": 3
}
]
},
{
"metadata": {
"id": "wsCUCwm-ZhdH",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# NumPy"
]
},
{
"metadata": {
"id": "YHpPx3udZnPe",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "8df93e7a-8400-4e8c-ee34-9ca10b5f25b9"
},
"cell_type": "code",
"source": [
"import numpy as np\n",
"!pip3 show numpy | grep Name -A 1"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"Name: numpy\r\n",
"Version: 1.14.5\r\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "rtxdcz24d2_W",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"### Создание массивов"
]
},
{
"metadata": {
"id": "IH3tNZiWe8Yz",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "6a8b1e45-561c-469d-f086-6c8f379f7c76"
},
"cell_type": "code",
"source": [
"# По умалочанию тап float64\n",
"np.array([])"
],
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([], dtype=float64)"
]
},
"metadata": {
"tags": []
},
"execution_count": 25
}
]
},
{
"metadata": {
"id": "NGThbGhRgEDh",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "b6363be0-8419-41b5-85c0-d14ac1134309"
},
"cell_type": "code",
"source": [
"type(np.array([]))"
],
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"metadata": {
"tags": []
},
"execution_count": 26
}
]
},
{
"metadata": {
"id": "4nWukgxMZrem",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "f5322c35-7281-4a94-f92a-4342923afa45"
},
"cell_type": "code",
"source": [
"# Ручное создание массива\n",
"np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])"
],
"execution_count": 28,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 28
}
]
},
{
"metadata": {
"id": "IjCYYzcJgXB1",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "e8e9e007-e9cd-465c-ca6d-a6ff90ee5c80"
},
"cell_type": "code",
"source": [
"# Если разное количество элементов, то получаем одномерный массив списков\n",
"np.array([[1, 2, 3], [4, 5], [6]])"
],
"execution_count": 29,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([list([1, 2, 3]), list([4, 5]), list([6])], dtype=object)"
]
},
"metadata": {
"tags": []
},
"execution_count": 29
}
]
},
{
"metadata": {
"id": "iSdNF9u8eTE_",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "acfc20f0-e530-4ca3-d919-78a85f588f70"
},
"cell_type": "code",
"source": [
"# Массив нулей\n",
"np.zeros((3,7))"
],
"execution_count": 34,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0.]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 34
}
]
},
{
"metadata": {
"id": "rpZfFk74e6rK",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"outputId": "00680030-03c1-4d82-f91e-3f6a52edf599"
},
"cell_type": "code",
"source": [
"# Массив единиц\n",
"np.ones((2,3,4))"
],
"execution_count": 38,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]],\n",
"\n",
" [[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 38
}
]
},
{
"metadata": {
"id": "nsMcalNRh4Ay",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 102
},
"outputId": "a3073ac8-83f1-43f7-d112-ef09af5abac6"
},
"cell_type": "code",
"source": [
"# Разрежённый массив единиц\n",
"np.eye(5)"
],
"execution_count": 46,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1., 0., 0., 0., 0.],\n",
" [0., 1., 0., 0., 0.],\n",
" [0., 0., 1., 0., 0.],\n",
" [0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 1.]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 46
}
]
},
{
"metadata": {
"id": "YTcCe6ThiSx1",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "2af3942c-1810-4e19-d098-863da5dbedae"
},
"cell_type": "code",
"source": [
"# Массив заполняется мусором памяти, в которой выделяется место\n",
"np.empty((3, 3))"
],
"execution_count": 47,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[2.66708335e-316, 2.35541533e-312, 2.14321575e-312],\n",
" [8.48798317e-313, 1.06099790e-312, 1.08221785e-312],\n",
" [1.10343781e-312, 8.70018274e-313, 1.99631762e+161]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 47
}
]
},
{
"metadata": {
"id": "oNnJFza_jBmU",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "96961d81-50db-4eec-e5f4-7dab74ed294e"
},
"cell_type": "code",
"source": [
"# Генератор последовательности по шагу\n",
"# (начальное значение (включительно), конечное значение, шаг последовательности)\n",
"print(np.arange(0, 1, 0.1))\n",
"print(np.arange(10, 20))\n",
"print(np.arange(10))"
],
"execution_count": 149,
"outputs": [
{
"output_type": "stream",
"text": [
"[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n",
"[10 11 12 13 14 15 16 17 18 19]\n",
"[0 1 2 3 4 5 6 7 8 9]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "efWi4s4mkdEK",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "f9330c80-cda6-4305-bee0-104f854a1c6f"
},
"cell_type": "code",
"source": [
"# Генератор последовательности по количеству\n",
"# (начальное значение (включительно), конечное значение, шаг последовательности)\n",
"print(np.linspace(0, 1, 11))"
],
"execution_count": 152,
"outputs": [
{
"output_type": "stream",
"text": [
"[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "c4MBQORhj0IA",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "79949d3c-6b09-4a5d-cd30-f379a382e0dc"
},
"cell_type": "code",
"source": [
"# Генератор от функции\n",
"def f(i,j): return i+j\n",
"np.fromfunction(f, (3,4))"
],
"execution_count": 59,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0., 1., 2., 3.],\n",
" [1., 2., 3., 4.],\n",
" [2., 3., 4., 5.]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 59
}
]
},
{
"metadata": {
"id": "sffdV9ITmLfu",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"### печать массивов"
]
},
{
"metadata": {
"id": "q3SVmRc0lx-o",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "063d6626-7f9e-495e-f4ed-2c3c5517b818"
},
"cell_type": "code",
"source": [
"# По умолчанию середина большого массива (боольше 1000 элементов) скрывается \n",
"print(np.zeros((1001)))"
],
"execution_count": 70,
"outputs": [
{
"output_type": "stream",
"text": [
"[0. 0. 0. ... 0. 0. 0.]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "o-66Bm8anA2g",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 731
},
"outputId": "db762dce-9e0d-4261-d921-24a0d4ea9524"
},
"cell_type": "code",
"source": [
"# Отмена обрезки вывода массива\n",
"np.set_printoptions(threshold=np.nan)\n",
"print(np.ones((1001)))"
],
"execution_count": 77,
"outputs": [
{
"output_type": "stream",
"text": [
"[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.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. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "-sR3BEjVpjiT",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "f47565c7-7c23-41d8-9b7b-7726a28085aa"
},
"cell_type": "code",
"source": [
"# Количество элементов в массиве, вызывающее обрезание элементов (по умолчанию 1000)\n",
"np.set_printoptions(threshold=1000)\n",
"print(np.ones((1001)))"
],
"execution_count": 81,
"outputs": [
{
"output_type": "stream",
"text": [
"[1. 1. 1. ... 1. 1. 1.]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "JR80FNq-pvEy",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "62fac86e-068f-405d-8d05-a22a7ec119fa"
},
"cell_type": "code",
"source": [
"# количество элементов в начале и в конце каждой размерности массива (по умолчанию 3)\n",
"np.set_printoptions(edgeitems=4)\n",
"print(np.ones((1001)))"
],
"execution_count": 82,
"outputs": [
{
"output_type": "stream",
"text": [
"[1. 1. 1. 1. ... 1. 1. 1. 1.]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "CuDY1PvamaWf",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "74eca9e6-21f7-4011-ba72-cfeacb0ec0df"
},
"cell_type": "code",
"source": [
"# Количество отображаемых цифр после запятой (по умолчанию 8)\n",
"np.set_printoptions(precision=2)\n",
"print(np.array([0, 0.1, 0.12, 0.123, 0.1234]))\n",
"np.set_printoptions(precision=8)"
],
"execution_count": 93,
"outputs": [
{
"output_type": "stream",
"text": [
"[0. 0.1 0.12 0.12 0.12]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "hBVy5xNvqRDG",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "b2bcfde3-b464-4329-9ca8-8db0c56e471d"
},
"cell_type": "code",
"source": [
"# количество символов в строке, после которых осуществляется перенос (по умолчанию 75)\n",
"np.set_printoptions(linewidth=120)\n",
"print(np.ones((100)))"
],
"execution_count": 94,
"outputs": [
{
"output_type": "stream",
"text": [
"[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
" 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "H-PL27_KouQg",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "5e19202f-2d08-44d9-d0eb-678519c05667"
},
"cell_type": "code",
"source": [
"# suppress - если True, не печатает маленькие значения в scientific notation (по умолчанию False)\n",
"np.set_printoptions(suppress=False)\n",
"print(np.array([0.1, 0.01, 0.001, 0.0001, 0.00001]))\n",
"np.set_printoptions(suppress=True)\n",
"print(np.array([0.1, 0.01, 0.001, 0.0001, 0.00001]))"
],
"execution_count": 95,
"outputs": [
{
"output_type": "stream",
"text": [
"[1.e-01 1.e-02 1.e-03 1.e-04 1.e-05]\n",
"[0.1 0.01 0.001 0.0001 0.00001]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "hcmsDXiGq08Q",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# nanstr - строковое представление NaN (по умолчанию 'nan')\n",
"# infstr - строковое представление inf (по умолчанию 'inf')\n",
"# formatter - позволяет более тонко управлять печатью массивов"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "f3HXtc60sv0v",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"### Случайные числа"
]
},
{
"metadata": {
"id": "Npgp7wB-s08M",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "416b8262-7e46-45a3-b9f9-325b54d5c0c3"
},
"cell_type": "code",
"source": [
"# С помошью модуля random\n",
"import random\n",
"np.array([random.random() for i in range(5)])"
],
"execution_count": 97,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0.61115141, 0.78181718, 0.33503489, 0.2963229 , 0.53246909])"
]
},
"metadata": {
"tags": []
},
"execution_count": 97
}
]
},
{
"metadata": {
"id": "zA7KDj5Fu4xt",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 170
},
"outputId": "64a450f3-b8c2-4653-d3e7-546791cfa04d"
},
"cell_type": "code",
"source": [
"print(random.random()) # от 0 до 1\n",
"print(random.uniform(1.0,1.1)) # от 1.0 до 1.1\n",
"print(random.randint(1,100)) # целые числа в диапазоне\n",
"print(random.randrange(10)) # от 0 до 10\n",
"print(random.randrange(2, 5)) # 2 или 3 или 4 или 5\n",
"print(random.randrange(2, 10, 2)) # с шагом 2\n",
"print(random.choice([1, 2, 3, 4, 5])) # возвращает случайный элемент списка\n",
"l = [1, 2, 3, 4, 5]\n",
"print(random.shuffle(l)) # перемешивает список\n",
"print(l)"
],
"execution_count": 142,
"outputs": [
{
"output_type": "stream",
"text": [
"0.1582067532122342\n",
"1.0010751029933582\n",
"25\n",
"5\n",
"2\n",
"6\n",
"4\n",
"None\n",
"[4, 1, 5, 2, 3]\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "iJENEy8ZtCeZ",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 170
},
"outputId": "181515cf-612c-49ba-dfaf-dc062647f638"
},
"cell_type": "code",
"source": [
"# С помошью numpy.random\n",
"# Эти функции одинаковы: rand, random, sample, ranf\n",
"print(np.random.rand()) # одно число\n",
"print(np.random.rand(2,5)) # массив\n",
"print(np.random.randn(7)) # стандартное нормальное распределение\n",
"print(np.random.randint(0, 3, 10)) # целые числа\n",
"print(np.random.randint(0, 3, (2, 10))) # массив\n",
"print(np.random.permutation(10)) # перемешанная последовательность от 0 до 10\n",
"np.random.bytes(32) # случайная последовательность байтов"
],
"execution_count": 157,
"outputs": [
{
"output_type": "stream",
"text": [
"0.8014452141966191\n",
"[[0.20914643 0.03151448 0.85661642 0.85567154 0.92194144]\n",
" [0.25785345 0.81680168 0.84968521 0.02730004 0.1906843 ]]\n",
"[-0.88246705 1.51964788 -1.12666102 -1.05589165 -1.20449715 -0.14431853 0.31106664]\n",
"[2 0 1 1 2 1 1 0 1 2]\n",
"[[2 0 0 1 1 2 2 1 1 1]\n",
" [0 0 1 1 0 1 0 2 2 2]]\n",
"[2 0 5 3 6 8 1 4 9 7]\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b\"\\xc7n\\x93\\xb8u\\xb1\\xa7\\xc5\\x9e\\x7f\\xce\\xa6\\x9d\\xb3Hv \\x8d\\xfb\\x8c\\x8e\\xe3\\x0bE'\\xe6@zh\\xd0\\xce\\xc2\""
]
},
"metadata": {
"tags": []
},
"execution_count": 157
}
]
},
{
"metadata": {
"id": "MxCCow56t1Hb",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "546f3d78-cba3-4444-a6cd-17f2d9e235bd"
},
"cell_type": "code",
"source": [
"# Перемешивание массива\n",
"print(np.random.permutation(l)) # возврашает новый массив\n",
"print(np.random.shuffle(l)) # перемешивает исходный массив\n",
"l"
],
"execution_count": 143,
"outputs": [
{
"output_type": "stream",
"text": [
"[2 4 5 1 3]\n",
"None\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[5, 2, 3, 1, 4]"
]
},
"metadata": {
"tags": []
},
"execution_count": 143
}
]
},
{
"metadata": {
"id": "8Tjzldsp6SF9",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# Выборки из массива\n",
"'''\n",
"Модель: numpy.random.choice(a, size=None, replace=True, p=None)\n",
"\n",
"a - одномерный массив или число. Если массив, будет производиться выборка из него. Если число, то выборка будет производиться из np.arange(a).\n",
"\n",
"size - размерности массива. Если None, возвращается одно значение.\n",
"\n",
"replace - если True, то одно значение может выбираться более одного раза.\n",
"\n",
"p - вероятности. Это означает, что элементы можно выбирать с неравными вероятностями. Если не заданы, используется равномерное распределение.\n",
"'''"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "-PTmbN0q7En5",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"outputId": "fbd5f17d-2231-4197-b860-752fcc5ebd3a"
},
"cell_type": "code",
"source": [
"# Также можно генерировать числа согласно различным распределениям (Гаусса, Парето и другие)\n",
"# Подробнее: https://docs.scipy.org/doc/numpy/reference/routines.random.html#distributions\n",
"# Например равномерное расспределение:\n",
"np.random.uniform(20, 30, (2, 5))"
],
"execution_count": 161,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[22.17520099, 28.98249015, 29.68315223, 22.53283571, 27.94405604],\n",
" [28.03314426, 28.89856905, 22.39348497, 24.77248181, 24.6377086 ]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 161
}
]
},
{
"metadata": {
"id": "9cYtXG_zAu1E",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment