Issue
I am using random library to generate 5000 data points from a Gaussian distribution and then using the data to compute histogram with 10 bins, see code below.
s = np.random.normal(mu, sigma, 5000)
arrHist = np.histogram(s, 10)
print(arrHist)
Output is:
(array([ 9, 48, 282, 800, 1436, 1424, 742, 216, 40, 3]),
array([1.52017489, 1.61690575, 1.71363661, 1.81036747, 1.90709834,
2.0038292 , 2.10056006, 2.19729092, 2.29402179, 2.39075265,
2.48748351]))
As you can see there are different array lengths and therefore I want to add np.nan
to the shorter array, so that array lengths are the same. I know it is possible to use np.append
to append values to the end of an array, but how can I do it when there are two arrays in arrHist
?
I am new with python, therefore is any help appreciated. Thanks in advance.
Solution
Try this:
result=[]
max_len=max([len(x) for x in arrHist])
for x in arrHist:
if len(x)!=max_len:
x=x.astype('float')
x=np.pad(x, (0,max_len-len(x)), 'constant', constant_values=(np.nan, ))
result.append(x)
Answered By - NicolasPeruchot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.