How to perform cubic spline interpolation in python?

Short answer: from scipy import interpolate def f(x): x_points = [ 0, 1, 2, 3, 4, 5] y_points = [12,14,22,39,58,77] tck = interpolate.splrep(x_points, y_points) return interpolate.splev(x, tck) print(f(1.25)) Long answer: scipy separates the steps involved in spline interpolation into two operations, most likely for computational efficiency. The coefficients describing the spline curve are computed, using … Read more

Populate a Pandas SparseDataFrame from a SciPy Sparse Matrix

A direct conversion is not supported ATM. Contributions are welcome! Try this, should be ok on memory as the SpareSeries is much like a csc_matrix (for 1 column) and pretty space efficient In [37]: col = np.array([0,0,1,2,2,2]) In [38]: data = np.array([1,2,3,4,5,6],dtype=”float64″) In [39]: m = csc_matrix( (data,(row,col)), shape=(3,3) ) In [40]: m Out[40]: <3×3 … Read more

Get coordinates of local maxima in 2D array above certain value

import numpy as np import scipy import scipy.ndimage as ndimage import scipy.ndimage.filters as filters import matplotlib.pyplot as plt fname=”/tmp/slice0000.png” neighborhood_size = 5 threshold = 1500 data = scipy.misc.imread(fname) data_max = filters.maximum_filter(data, neighborhood_size) maxima = (data == data_max) data_min = filters.minimum_filter(data, neighborhood_size) diff = ((data_max – data_min) > threshold) maxima[diff == 0] = 0 labeled, num_objects … Read more

How do I plot list of tuples?

If I get your question correctly, you could do something like this. >>> import matplotlib.pyplot as plt >>> testList =[(0, 6.0705199999997801e-08), (1, 2.1015700100300739e-08), (2, 7.6280656623374823e-09), (3, 5.7348209304555086e-09), (4, 3.6812203579604238e-09), (5, 4.1572516753310418e-09)] >>> from math import log >>> testList2 = [(elem1, log(elem2)) for elem1, elem2 in testList] >>> testList2 [(0, -16.617236475334405), (1, -17.67799605473062), (2, -18.691431541177973), (3, … Read more

savefig without frames, axes, only content

EDIT Changed aspect=”normal to aspect=”auto’ since that changed in more recent versions of matplotlib (thanks to @Luke19). Assuming : import matplotlib.pyplot as plt To make a figure without the frame : fig = plt.figure(frameon=False) fig.set_size_inches(w,h) To make the content fill the whole figure ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) Then draw your … Read more

Overflow Error in Python’s numpy.exp function

As fuglede says, the issue here is that np.float64 can’t handle a number as large as exp(1234.1). Try using np.float128 instead: >>> cc = np.array([[0.120,0.34,-1234.1]], dtype=np.float128) >>> cc array([[ 0.12, 0.34, -1234.1]], dtype=float128) >>> 1 / (1 + np.exp(-cc)) array([[ 0.52996405, 0.58419052, 1.0893812e-536]], dtype=float128) Note however, that there are certain quirks with using extended precision. … Read more

Numpy transpose of 1D array not giving expected result

NumPy’s transpose() effectively reverses the shape of an array. If the array is one-dimensional, this means it has no effect. In NumPy, the arrays array([1, 2, 3]) and array([1, 2, 3]) are actually the same – they only differ in whitespace. What you probably want are the corresponding two-dimensional arrays, for which transpose() would work … Read more