1. 程式人生 > >numpy.random.randint

numpy.random.randint

https://www.w3cschool.cn/doc_numpy_1_12/numpy_1_12-generated-numpy-random-randint.html

numpy.random.randint(low, high=None, size=None, dtype=‘l’)
Return random integers from low (inclusive) to high (exclusive).

Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high).

If high is None (the default), then results are from [0, low).

np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])

np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

‘’‘生成0~100的隨機數作為樣本’’’
pop = np.random.randint(0,100,100)
pop
array([70, 83, 77, 81, 59, 2, 59, 14, 20, 38, 45, 60, 49, 18, 39, 75, 13,
97, 62, 85, 95, 4, 26, 49, 2, 20, 38, 27, 45, 27, 44, 17, 59, 77,
86, 7, 91, 8, 28, 22, 26, 65, 88, 71, 87, 72, 63, 5, 96, 27, 16,
84, 13, 95, 73, 10, 99, 58, 12, 46, 78, 24, 18, 5, 21, 10, 57, 6,
94, 4, 89, 14, 21, 78, 87, 63, 47, 69, 25, 56, 59, 41, 10, 44, 44,
54, 29, 10, 40, 80, 88, 67, 86, 97, 75, 8, 20, 89, 57, 34])

import numpy as np
t = np.arange(0., 5., 0.2)

array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4,
2.6, 2.8, 3. , 3.2, 3.4, 3.6, 3.8, 4. , 4.2, 4.4, 4.6, 4.8])

print(“B\n”, np.linspace(2.0, 3.0, num=5, retstep=True), “\n”)

B
(array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)

Numpy可以參考:
https://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-2-Numpy.ipynb#Numpy----multidimensional-data-arrays

https://www.geeksforgeeks.org/numpy-linspace-python/

numpy.linspace()
Parameters :

-> start : [optional] start of interval range. By default start = 0
-> stop : end of interval range
-> restep : If True, return (samples, step). By deflut restep = False 返回步長
-> num : [int, optional] No. of samples to generate
-> dtype : type of output array

np.linspace(2.0, 3.0, num=5, retstep=True)
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)

import matplotlib.pyplot as plt
N = 8
y = np.zeros(N)
x1 = np.linspace(0, 10, N, endpoint=True)
x2 = np.linspace(0, 10, N, endpoint=False)
plt.plot(x1, y, ‘o’)
[<matplotlib.lines.Line2D object at 0x…>]

plt.plot(x2, y + 0.5, ‘o’)
[<matplotlib.lines.Line2D object at 0x…>]

plt.ylim([-0.5, 1])
(-0.5, 1)

plt.show()

y = np.zeros(8)
array([0., 0., 0., 0., 0., 0., 0., 0.])

https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py
https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html

直方圖:
index = [0,1,2,3,4]
values = [5,6,7,8,9]
plt.bar(index, values)
plt.show()