scipy

unevenly centered subplots in matplotlib in Python?

I am plotting a simple pair of subplots in matplotlib that are for some reason unevenly centered. I plot them as follows: plt.figure() # first subplot s1 = plt.subplot(2, 1, 1) plt.bar([1, 2, 3], [4, 5, 6]) # second subplot s2 = plt.subplot(2, 1, 2) plt.pcolor(rand(5,5)) # add colorbar plt.colorbar() # square axes axes_square(s1) axes...

drawing just half of a matrix in matplotlib with python

How can I draw only half of a matrix, e.g. the upper / lower part of a matrix, with pcolor for example? Suppose I have a n x n matrix and I draw it using pcolor(my_matrix). I want only the lower half to be shown since other values are redundant. How can this be done? thanks. ...

Scipy interpolation on a numpy array

I have a lookup table that is defined the following way: | <1 2 3 4 5+ -------|---------------------------- <10000 | 3.6 6.5 9.1 11.5 13.8 20000 | 3.9 7.3 10.0 13.1 15.9 20000+ | 4.5 9.2 12.2 14.8 18.2 TR_ua1 = np.array([ [3.6, 6.5, 9.1, 11.5, 13.8], [3.9, 7.3, 10.0, 13.1, 15.9], ...

How to speed-up python nested loop?

I'm performing a nested loop in python that is included below. This serves as a basic way of searching through existing financial time series and looking for periods in the time series that match certain characteristics. In this case there are two separate, equally sized, arrays representing the 'close' (i.e. the price of an asset) and...

Instrumentation for numerical linear algebra in Python

I use numpy for numerical linear algebra. I suspect that I can get much better performance if I make small modifications in how I carry out certain computations so that they are more memory efficient, for example. I was wondering if there is any form of instrumentation available in python to detect cache and TLB misses. There is a very ...

scipy smart optimize

I need to fit some points from different datasets with straight lines. From every dataset I want to fit a line. So I got the parameters ai and bi that describe the i-line: ai + bi*x. The problem is that I want to impose that every ai are equal because I want the same intercepta. I found a tutorial here: http://www.scipy.org/Cookbook/Fitt...

Libraries for manipulating multivariate polynomials

Hi. I need to write some code that deals with generating and manipulating multivariable polynomials. I'll outline my task with a simplified example. Lets say I am given three expressions: 2x^2, 3y + 1, and 1z. I then need to multiply these together which would give me 6x^2yz + 2x^2z. Then I would like to find the partial derivatives of...

Inverse Distance Weighted (IDW) Interpolation with Python

The Question: What is the best way to calculate inverse distance weighted (IDW) interpolation in Python, for point locations? Some Background: Currently I'm using RPy2 to interface with R and it's gstat module. Unfortunately, the gstat module conflicts with arcgisscripting which I got around by running RPy2 based analysis in a separate...

Scipy sparse triangular matrix?

Hi, I am using Scipy to construct a large, sparse (250k X 250k) co-occurrence matrix using scipy.sparse.lil_matrix. Co-occurrence matrices are triangular; that is, M[i,j] == M[j,i]. Since it would be highly inefficient (and in my case, impossible) to store all the data twice, I'm currently storing data at the coordinate (i,j) where i i...

Python scipy.weave and STANN C++ Library

I'm trying out scipy.weave to build a fast minimal spanning tree program in Python. Unfortunately, using scipy.weave with a C++ library that I found, STANN, is more difficult that I had assumed. Here's the link to the STANN library: http://sites.google.com/a/compgeom.com/stann/ Below is the Python with scipy.weave script that I wrote. ...

using the stats package in scipy error in Python?

I am trying to use the scipy stats package in Python and am getting the following error (on Mac OS X): $ python Python 2.6.5 (r265:79359, Mar 24 2010, 01:32:55) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import scipy >>> from scipy import stats I then get t...

using Pygsl with GCC 4.0 in Python

I am trying to install pygsl using latest version of GCC, i.e.: $ gcc --version i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659) I get the error: $ sudo python setup.py build numpy Building testing ufuncs! running build running build_py running build_ext building 'errno' extension C compiler: gcc-4.0 -arch ppc -arch...

error installing numpy on Mac OS X 10.6

I am having lots of trouble installing numpy on Mac OS X 10.6. When I try to install it from source, I get the following error: customize NAGFCompiler Could not locate executable f95 customize AbsoftFCompiler Could not locate executable f90 Could not locate executable f77 customize IBMFCompiler Could not locate executable xlf90 Could no...

cannot change font to Helvetica in Matplotlib in Python on Mac OS X 10.6

I am trying to change the matplotlib font to helvetica, which I'd like to use in a PDF plot. I try the following: import matplotlib matplotlib.use('PDF') import matplotlib.pylab as plt from matplotlib import rc plt.rcParams['ps.useafm'] = True rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) plt.rcParams['pdf.fonttype'] ...

Python+Scipy+Integration: dealing with precision errors in functions with spikes

Hi, I am trying to use scipy.integrate.quad to integrate a function over a very large range (0..10,000). The function is zero over most of its range but has a spike in a very small range (e.g. 1,602..1,618). When integrating, I would expect the output to be positive, but I guess that somehow quad's guessing algorithm is getting confus...

How to plot empirical cdf in matplotlib in Python?

How can I plot the empirical CDF of an array of numbers in matplotlib in Python? I'm looking for the cdf analog of pylab's "hist" function. One thing I can think of is: from scipy.stats import cumfreq a = array([...]) # my array of numbers num_bins = 20 b = cumfreq(a, num_bins) plt.plot(b) Is that correct though? Is there an easie...

Numpy transpose multiplication problem

I tried to find the eigenvalues of a matrix multiplied by its transpose but I couldn't do it using numpy. testmatrix = numpy.array([[1,2],[3,4],[5,6],[7,8]]) prod = testmatrix * testmatrix.T print eig(prod) I expected to get the following result for the product: 5 11 17 23 11 25 39 53 17 39 61 83 23 53 ...

Fitting a pareto distribution with (python) Scipy

I have a data set that I know has a Pareto distribution. Can someone point me to how to fit this data set in Scipy? I got the below code to run but I have no idea what is being returned to me (a,b,c). Also, after obtaining a,b,c, how do I calculate the variance using them? import scipy.stats as ss import scipy as sp a,b,c=ss.pareto...

Interpolation over an irregular grid

So, I have three numpy arrays which store latitude, longitude, and some property value on a grid -- that is, I have LAT(y,x), LON(y,x), and, say temperature T(y,x), for some limits of x and y. The grid isn't necessarily regular -- in fact, it's tripolar. I then want to interpolate these property (temperature) values onto a bunch of dif...

How to elementwise-multiply a scipy.sparse matrix by a broadcasted dense 1d array?

Suppose I have a 2d sparse array. In my real usecase both the number of rows and columns are much bigger (say 20000 and 50000) hence it cannot fit in memory when a dense representation is used: >>> import numpy as np >>> import scipy.sparse as ssp >>> a = ssp.lil_matrix((5, 3)) >>> a[1, 2] = -1 >>> a[4, 1] = 2 >>> a.todense() matrix([[...