Monday, February 2, 2015

py6. The function choose and percentile in Python

For Python programming, it is best to use a package like Anaconda with almost all the modules. It is easy to update modules using conda command. Lastly if the module is not in conda, you can use the pip program to install it. Remember to use the pip inside Anaconda.


The function np.random.choice selects random values from a given array, with or without replacement. With replacement, we can generate arrays of length greater than that of the the input array.


The np.unique() function finds the unique entries in an array. The percentile function orders the array, and finds the quantiles.

# ex6.py
from __future__ import division, print_function
from numpy.random import choice
import numpy as np
x = choice(100, size = 300, replace = True)
y = len(x)-len(np.unique(x))
print('The number of repeats is %d' % y)
stat = x.mean(), x.std(ddof = 1)
print('The mean is %.2f and standard deviation is %.2f' % stat)
print('The quantiles are:')
v = np.arange(25,85,10)
p = np.percentile(x, v)
print(np.vstack((v,p)))
## The number of repeats is 206
## The mean is 48.65 and standard deviation is 29.26
## The quantiles are:
## [[ 25.    35.    45.    55.    65.    75.  ]
##  [ 24.    34.    42.55  51.    63.7   74.  ]]

No comments:

Post a Comment