{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "numpy_random.ipynb", "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "lhmyI7sB_bKJ", "colab_type": "text" }, "source": [ "# Generate Random Numbers" ] }, { "cell_type": "markdown", "metadata": { "id": "MulyzIdD_kxf", "colab_type": "text" }, "source": [ "`numpy.random` is frequently used for generating random numbers.\n", "\n", "For more details, please refer to [Random sampling (numpy.random)](https://docs.scipy.org/doc/numpy/reference/routines.random.html)" ] }, { "cell_type": "code", "metadata": { "id": "vtrQ-n7PAoYJ", "colab_type": "code", "colab": {} }, "source": [ "import numpy as np" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "nLB8lH051pDg", "colab_type": "text" }, "source": [ "`np.random.seed` sets the seed for the generator.\n", "\n", "With the seed reset (every time), the same set of numbers will appear every time.\n", "\n", "If the random seed is not reset, different numbers appear with every invocation:\n", "\n", "It is useful for debugging as output will not change\n", "\n", "Explainer: https://stackoverflow.com/questions/21494489/what-does-numpy-random-seed0-do" ] }, { "cell_type": "code", "metadata": { "id": "PzxXe5pR3QI0", "colab_type": "code", "colab": {} }, "source": [ "np.random.seed(1)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "cSSX-VCBAnuJ", "colab_type": "text" }, "source": [ "`np.random.rand` randomly generates numbers from a uniform distribution over $[0, 1)$" ] }, { "cell_type": "code", "metadata": { "id": "ran81EdeAKl6", "colab_type": "code", "outputId": "9adb1089-0417-490b-f31e-6730df35d688", "colab": { "base_uri": "https://localhost:8080/", "height": 53 } }, "source": [ "np.random.rand(2, 3) # Generates a 2x3 array with random numbers" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[4.17022005e-01, 7.20324493e-01, 1.14374817e-04],\n", " [3.02332573e-01, 1.46755891e-01, 9.23385948e-02]])" ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "cell_type": "markdown", "metadata": { "id": "g_jgVxcfAi00", "colab_type": "text" }, "source": [ "`np.random.randn` generates numbers following standard normal distribution." ] }, { "cell_type": "code", "metadata": { "id": "OG-suj-PA72b", "colab_type": "code", "outputId": "6fda962e-bc37-4f93-9bdf-aa82b5502117", "colab": { "base_uri": "https://localhost:8080/", "height": 53 } }, "source": [ "np.random.randn(2, 3) # Generate 2 * 3 random numbers" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[-0.52817175, -1.07296862, 0.86540763],\n", " [-2.3015387 , 1.74481176, -0.7612069 ]])" ] }, "metadata": { "tags": [] }, "execution_count": 19 } ] }, { "cell_type": "markdown", "metadata": { "id": "SLw958olA9Uu", "colab_type": "text" }, "source": [ "`np.random.randint(low, high)` generates integers ranging from `low` (inclusive) to `high` (exclusive)" ] }, { "cell_type": "code", "metadata": { "id": "SkRVwD_yCd7r", "colab_type": "code", "outputId": "72c58ee8-4ed7-433e-8a95-60e58293f827", "colab": { "base_uri": "https://localhost:8080/", "height": 71 } }, "source": [ "np.random.randint(low = 0, high = 4, size = (3, 3))" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[3, 0, 2],\n", " [0, 1, 2],\n", " [2, 0, 3]])" ] }, "metadata": { "tags": [] }, "execution_count": 20 } ] }, { "cell_type": "markdown", "metadata": { "id": "MLSvOE4UEA6n", "colab_type": "text" }, "source": [ "`np.random.choice` generates random numbers following a given pmf." ] }, { "cell_type": "code", "metadata": { "id": "NidDNwzK4FRY", "colab_type": "code", "outputId": "13962d5d-09fb-41cb-fe32-e90186c6bfcd", "colab": { "base_uri": "https://localhost:8080/", "height": 71 } }, "source": [ "a = np.arange(4) # 3 has prob=0.4\n", "p = [0.1, 0.2, 0.3, 0.4]\n", "np.random.choice(a = a, size=(3, 4), p = p)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[2, 2, 3, 3],\n", " [3, 3, 0, 2],\n", " [3, 3, 3, 1]])" ] }, "metadata": { "tags": [] }, "execution_count": 21 } ] }, { "cell_type": "markdown", "metadata": { "id": "9Arc1wQq5a2V", "colab_type": "text" }, "source": [ "The sample space is not necessary to be number sets." ] }, { "cell_type": "code", "metadata": { "id": "7JqVclm44hgH", "colab_type": "code", "outputId": "fc86099d-f55f-4d9c-f75f-930e91160710", "colab": { "base_uri": "https://localhost:8080/", "height": 71 } }, "source": [ "a = ['Spade', 'Heart', 'Club', 'Diamond']\n", "p = [0.25, 0.25, 0.25, 0.25]\n", "np.random.choice(a = a, size=(3, 4), p = p)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([['Spade', 'Clud', 'Clud', 'Clud'],\n", " ['Heart', 'Spade', 'Heart', 'Spade'],\n", " ['Diamond', 'Heart', 'Spade', 'Clud']], dtype='