Draw a random value from a given distribution [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array of values which I have generated starting from an observed value y1, and assuming that this value has a poissonian distribution:
array = np.random.poisson(np.real(y1), 10000)
What if I want to extract a random value from array, which is poissonian distributed, and hence has a "most probable value" that peaks at y1? How can I do that? Does it work by simple random extraction, or does it need something else to be specified?
EDIT: trying to be more specific. I have an array whose elements are Poisson distributed. If I want to randomly extract an element from that array, should I tell to the method about the array distribution, or it is not necessary?
I hope this will clarify a bit.

Just
import random
randval = random.choice(array)
should work fine for you. Once array is generated, in order to pick one at random of its items it does not matter any more by what process or according to what distribution array was populated in the first place.

Related

How to do this particular selective masking in numpy [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have two 2d NumPy arrays. I need to mask array 2 on array 1 in such a way that, whatever non zero value present in array2 should get replaced in array1, but others should remain the same.
I need the function selective mask where I can input this two 2d arrays and it returns output.
eg:
array1=np.array([[1,2,3],[0,0,0],[3,3,3]])
array2=n.array([[0,0,0],[0,0,0],[7,8,9]])
result_array=selective_mask(array1,array2)
result array should be [[1,2,3],[0,0,0],[7,8,9]], as you can see only the elements 7,8,9 got switched as that was only the non zero elements in array2.
I came up with a solution.
I used np.where
np.where(array2!=0,array2,array1)

Python, Numpy - Normalize a matrix/array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
This is most likely a dumb question but being a beginner in Python/Numpy I will ask it anyways. I have come across a lot of posts on how to Normalize an array/matrix in numpy. But I am not sure about the WHY. Why/When does an array/matrix need to be normalized in numpy? When is it used?
Normalize can have multiple meanings in difference context. My question belongs to the field of Data Analytics/Data Science. What does Normalization mean in this context? Or more specifically in what situation should I normalize an array?
The second part to this question is - What are the different methods of Normalization and can they be used interchangeably in all situations?
The third and final part - can Normalization be used for Arrays of any dimensions?
Links to any reference material (for beginners) will be appreciated.
Consider trying to cluster objects with two numerical attributes A and B. Both are equally important. Attribute A can range from 0 to 1000 and attribute B can range from 0 to 5.
If you did not normalize A and B you would end up with attribute A completely overpowering attribute B when applying any standard distance metric.

normalize a matrix along one specific dimension [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a matrix of shape [1000,500], and I would like to normalize the matrix along the second dimension. Is the following implementation right?
def norm(x):
return (x - np.mean(x)) / (np.std(x) + 1e-7)
for row_id in range(datamatrix.shape[0]):
datamatrix[row_id,:] = norm(datamatrix[row_id,:])
Your implementation would indeed normalize along the row-axis (I'm not sure what you mean by second dimension as rows are usually the first dimension of matrices, and numpy starts with dimension 0). You don't need to include the colon as it's implicit that you want all the rows.
Do remember to use the float32 dtype in your datamatrix as opposed to a integer dtype as it doesn't do automatic typecasting.
A more efficient, or clean implementation might be to use sklearn.preprocessing.normalize.
But be aware that you're using standard score normalization which assumes your dataset is normally distributed.

Optimize the distribution parameter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Let's consider a distribution L(t) depending on a float parameter t.
I need to compute such a value of t that 5%-quantile of distribution L(t) equals 0.
Are there any solutions in python for this problem? The way of generating a new sample for somehow chosen t and calculating its quantile seems too slow and irrational.
Not sure I understand question clearly, but you have distribution with PDF=L(x,t) on interval [a, b] (a is negative), and you want to find such t that integral from a to 0 over x is equal to 1/20 (5%), right ?
Looks like root finding problem to me.
S_a^0 L(x,t) dx - 1/20 = 0
In Python, function I usually use is scipy.optimize.brentq

Fitting arbitrary data from simulation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello dear Python experts:)
From a simulation I got data (course of energy over the time) which I have to fit. When I plot the energy it has a non-periodic oscillating course. There are a bunch of helping function like curve_fit from scipy etc. But you always have to specify a function with which the fit should take place. But I don't know a proper function a priori.
I need something like a Fourier fit to get a function representing the data (like it is possible in MatLab) to later use this function to determine its maxima. Has anyone an idea how to deal with such a problem?
Here is an example course: 2
If you like, you can have a look at the data in a .csv-file: https://1drv.ms/u/s!AuQAmr8-QRJSdzNTzyvWPhUaEnw
I would be very delighted to get some help:-)
Many thanks:-)
Using the Fourier fit in MATLAB you also specify a model (how many sin/cos you want).
For instance "Fourier 2" is:
f(x) = a0 + a1*cos(x*w) + b1*sin(x*w) +
a2*cos(2*x*w) + b2*sin(2*x*w)
Check http://exnumerus.blogspot.nl/2010/04/how-to-fit-sine-wave-example-in-python.html to see how to fit for "Fourier1".
If you really want no model you need to use something like "eureqa", which is free for academic use (http://www.nutonian.com/download/eureqa-desktop-download/).

Categories

Resources