Random¶
In [1]:
%pip install numpy
import numpy as np
Requirement already satisfied: numpy in /home/samrat/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages (1.24.4) Note: you may need to restart the kernel to use updated packages.
In [6]:
np.random.seed(123)
np.random.randint(low=1, high=7, size=3)
Out[6]:
array([6, 3, 5])
In [7]:
np.random.seed(2357)
np.random.choice(a=np.arange(1, 7), size=3, replace=False, p=None)
Out[7]:
array([6, 5, 1])
In [8]:
np.random.choice(a=np.arange(1, 7), size=3, replace=False, p=np.array([0.1, 0.1, 0.1, 0.1, 0.3, 0.3 ]))
Out[8]:
array([5, 2, 6])
In [9]:
np.random.choice(a=np.array(['you', 'can', 'use', 'strings', 'too']), size=3, replace=False)
Out[9]:
array(['use', 'you', 'can'], dtype='<U7')
In [10]:
foo = np.array([[1,2], [3,4], [5,6],[7,8],[9,10]])
In [11]:
np.random.seed(1234)
rand_rows = np.random.randint(low=0, high=foo.shape[0], size=3)
rand_rows
Out[11]:
array([3, 4, 4])
In [12]:
foo[rand_rows]
Out[12]:
array([[ 7, 8], [ 9, 10], [ 9, 10]])
In [14]:
np.random.seed(1234)
rand_rows = np.random.choice(a=np.arange(start=0, stop=foo.shape[0]), replace=False, size=3)
rand_rows
Out[14]:
array([4, 0, 1])
In [15]:
foo[rand_rows]
Out[15]:
array([[ 9, 10], [ 1, 2], [ 3, 4]])
In [16]:
np.random.permutation(foo)
Out[16]:
array([[ 7, 8], [ 5, 6], [ 3, 4], [ 1, 2], [ 9, 10]])
In [17]:
np.random.uniform(low=1.0, high=2.0, size=(2,2))
Out[17]:
array([[1.86066977, 1.15063697], [1.19851876, 1.81516293]])
In [20]:
np.random.normal(loc=0.0, scale=1.0, size=2)
Out[20]:
array([-0.00867858, -0.32106129])
In [21]:
np.random.binomial(n=10, p=0.25, size=(3,2))
Out[21]:
array([[2, 4], [1, 0], [2, 0]])
In [22]:
generator = np.random.default_rng(seed=123)
generator.integers(low=1, high=7, size=3)
Out[22]:
array([1, 5, 4])
In [23]:
generator.choice(a=np.arange(start=1, stop=7), size=3)
Out[23]:
array([1, 6, 2])
In [25]:
generator.permutation(foo, axis=1)
Out[25]:
array([[ 1, 2], [ 3, 4], [ 5, 6], [ 7, 8], [ 9, 10]])