Random Number from Histogram

It’s probably what np.random.choice does in @Ophion’s answer, but you can construct a normalized cumulative density function, then choose based on a uniform random number: from __future__ import division import numpy as np import matplotlib.pyplot as plt data = np.random.normal(size=1000) hist, bins = np.histogram(data, bins=50) bin_midpoints = bins[:-1] + np.diff(bins)/2 cdf = np.cumsum(hist) cdf = … Read more

How to create a white image in Python?

Every color in an image is represented by one byte. So to create an image array, you should set it’s dtype to uint8. And, you don’t need for-loop to set every elements to 255, you can use fill() method or slice index: import numpy as np img = np.zeros([100,100,3],dtype=np.uint8) img.fill(255) # or img[:] = 255

pyinstaller creating EXE RuntimeError: maximum recursion depth exceeded while calling a Python object

This worked for me Run pyinstaller and stop it to generate the spec file : pyinstaller filename.py A file with .spec as extension should be generated Now add the following lines to the beginning of the spec file : import sys sys.setrecursionlimit(5000) Now run the spec file using : pyinstaller filename.spec

Evaluate sympy expression from an array of values

First of all, at the moment SymPy does not guarantee support for numpy arrays which is what you want in this case. Check this bug report http://code.google.com/p/sympy/issues/detail?id=537 Second, If you want to evaluate something numerically for many values SymPy is not the best choice (it is a symbolic library after all). Use numpy and scipy. … Read more

interpolate 3D volume with numpy and or scipy

In scipy 0.14 or later, there is a new function scipy.interpolate.RegularGridInterpolator which closely resembles interp3. The MATLAB command Vi = interp3(x,y,z,V,xi,yi,zi) would translate to something like: from numpy import array from scipy.interpolate import RegularGridInterpolator as rgi my_interpolating_function = rgi((x,y,z), V) Vi = my_interpolating_function(array([xi,yi,zi]).T) Here is a full example demonstrating both; it will help you understand … Read more

Tutorial for scipy.cluster.hierarchy [closed]

There are three steps in hierarchical agglomerative clustering (HAC): Quantify Data (metric argument) Cluster Data (method argument) Choose the number of clusters Doing z = linkage(a) will accomplish the first two steps. Since you did not specify any parameters it uses the standard values metric=”euclidean” method = ‘single’ So z = linkage(a) will give you … Read more

Highest Posterior Density Region and Central Credible Region

From my understanding “central credible region” is not any different from how confidence intervals are calculated; all you need is the inverse of cdf function at alpha/2 and 1-alpha/2; in scipy this is called ppf ( percentage point function ); so as for Gaussian posterior distribution: >>> from scipy.stats import norm >>> alpha = .05 … Read more