numpy.random.choice() a Python

numpy.random.choice() a Python

Amb l'ajuda de elecció () mètode, podem obtenir les mostres aleatòries de la matriu unidimensional i retornar les mostres aleatòries de la matriu numpy.

Sintaxi: numpy.random.choice(a, size=None, substituir=True, p=None)

Paràmetres:

1) a - Matriu 1-D de numpy amb mostres aleatòries.

2) mida - Forma de sortida de mostres aleatòries de matriu numpy.

3) substituir - Tant si la mostra és amb o sense substitució.

4) p - La probabilitat s'adjunta amb totes les mostres en a.

Sortida: Retorna la matriu numpy de mostres aleatòries.

Exemple #1:

En aquest exemple podem veure que utilitzant elecció () mètode, podem obtenir les mostres aleatòries de la matriu numpy, pot generar mostres uniformes o no uniformes mitjançant aquest mètode.

Python 3




# import choice> import> numpy as np> import> matplotlib.pyplot as plt> > # Using choice() method> gfg> => np.random.choice(> 13> ,> 5000> )> > count, bins, ignored> => plt.hist(gfg,> 25> , density> => True> )> plt.show()>

Sortida:

Exemple #2:

Python 3




# import choice> import> numpy as np> import> matplotlib.pyplot as plt> > # Using choice() method> gfg> => np.random.choice(> 5> ,> 1000> , p> => [> 0.2> ,> 0.1> ,> 0.3> ,> 0.4> ,> 0> ])> > count, bins, ignored> => plt.hist(gfg,> 14> , density> => True> )> plt.show()>

Sortida: