Cara menggunakan np.random.multivariate_normal python

The multivariate normal, multinormal or Gaussian distribution is a generalisation of the one-dimensional normal distribution to higher dimensions.

Such a distribution is specified by its mean and covariance matrix, which are analogous to the mean [average or “centre”] and variance [standard deviation squared or “width”] of the one-dimensional normal distribution.

Parameters:

mean : [N,] ndarray

Mean of the N-dimensional distribution.

cov : [N,N] ndarray

Covariance matrix of the distribution.

size : tuple of ints, optional

Given a shape of, for example, [m,n,k], m*n*k samples are generated, and packed in an m-by-n-by-k arrangement. Because each sample is N-dimensional, the output shape is [m,n,k,N]. If no shape is specified, a single sample is returned.

Returns:

out : ndarray

The drawn samples, arranged according to size. If the shape given is [m,n,...], then the shape of out is is [m,n,...,N].

In other words, each entry out[i,j,...,:] is an N-dimensional value drawn from the distribution.

Notes

The mean is a coordinate in N-dimensional space, which represents the location where samples are most likely to be generated. This is analogous to the peak of the bell curve for the one-dimensional or univariate normal distribution.

def _new_recombination2[self, X, trials=100]: print[" * Trying to reboot..."] from numpy import average, identity, cov, logspace from numpy.random import multivariate_normal from matplotlib.pyplot import scatter, show, xlim, ylim, subplots, legend #fig, ax = subplots[1,1, figsize=[5,5]] best_solutions = self._get_best_solutions[int[self.numberofparticles/3]] all_sols = [] for sol in best_solutions: all_sols.append[sol.X] all_sols = array[all_sols].T #print [all_sols] com = [average[ x, weights=logspace[0,-2,self.numberofparticles/3] ] for x in all_sols] cova = cov[all_sols] res = multivariate_normal[com, cova, trials] if False: scatter[all_sols[0], all_sols[1], label="all selected solutions"] scatter[com[0], com[1], label="weighted average"] scatter[res.T[0], res.T[1], alpha=0.5, s=10, label="new samples"] scatter[all_sols[0][0], all_sols[1][0], label="best individual"] xlim[-100,100] ylim[-100,100] legend[] show[] ; exit[] for r in res: for d in range[len[r]]: if r[d]>self.Boundaries[d][1]: r[d] = self.Boundaries[d][1] elif r[d]

With the help of np.multivariate_normal[] method, we can get the array of multivariate normal values by using np.multivariate_normal[] method.

Syntax : np.multivariate_normal[mean, matrix, size]
Return : Return the array of multivariate normal values.

Example #1 :
In this example we can see that by using np.multivariate_normal[] method, we are able to get the array of multivariate normal values by using this method.




# import numpy

import numpy as np

  

mean= np.multivariate_normal[]0np.multivariate_normal[]1np.multivariate_normal[]2np.multivariate_normal[]3np.multivariate_normal[]4

np.multivariate_normal[]5= np.multivariate_normal[]7np.multivariate_normal[]1np.multivariate_normal[]2np.multivariate_normal[mean, matrix, size]0np.multivariate_normal[]2np.multivariate_normal[mean, matrix, size]0np.multivariate_normal[mean, matrix, size]1np.multivariate_normal[mean, matrix, size]0np.multivariate_normal[]2np.multivariate_normal[]1np.multivariate_normal[]2np.multivariate_normal[mean, matrix, size]0np.multivariate_normal[mean, matrix, size]1np.multivariate_normal[mean, matrix, size]0np.multivariate_normal[]2np.multivariate_normal[mean, matrix, size]0np.multivariate_normal[]2np.multivariate_normal[]1np.multivariate_normal[mean, matrix, size]5

.T transposes a matrix. So in your case, if n=2, your code will work [or at least, will run without error] without the transpose, because a matrix such as:

>>> np.random.multivariate_normal[mean, cov, size = 2]
array[[[ 1.4594626 , -0.55863612],
       [-1.17139735, -0.36484634]]]

Can be split into 2 arrays [x will be [ 1.4594626 , -1.17139735] and y will be [-0.55863612, -0.36484634]]. Note that this is not necessarily what you are looking for, and you might end up plotting the wrong thing [depending what you're trying to do].

But for anything bigger [or smaller], it won't:

>>> np.random.multivariate_normal[mean, cov, size = 5]
array[[[-0.34091962,  2.2368088 ],
       [-1.11081547,  0.93089064],
       [ 1.45452483, -0.40007311],
       [ 0.96038401,  0.26206106],
       [ 0.3079481 ,  0.66869094]]]

Because that is essentially 5 arrays that you are trying to unpack into 2 variables [hence the error]. However when you transpose it:

>>> np.random.multivariate_normal[mean, cov, size = 5].T
array[[[ 0.04466423,  0.88384196,  0.09108559, -2.30473587,  1.58497064],
       [ 0.66190894,  0.90202853,  0.31090378,  0.95697681, -0.61557393]]]

You're good to go. Your x array will be the first "row":

>>> np.random.multivariate_normal[mean, cov, size = 5]
array[[[-0.34091962,  2.2368088 ],
       [-1.11081547,  0.93089064],
       [ 1.45452483, -0.40007311],
       [ 0.96038401,  0.26206106],
       [ 0.3079481 ,  0.66869094]]]
0 and y will be your second:
>>> np.random.multivariate_normal[mean, cov, size = 5]
array[[[-0.34091962,  2.2368088 ],
       [-1.11081547,  0.93089064],
       [ 1.45452483, -0.40007311],
       [ 0.96038401,  0.26206106],
       [ 0.3079481 ,  0.66869094]]]
2

Bài mới nhất

Chủ Đề