numpy

Removing Array Elements in Python while keeping track of their position

Hi! I'v got two numpy arrays. The first array contains some zeros (which are distributed randomly over the length of the array), which I would like to remove. My issue is that I would also like to remove the entries of the second array at the index positions where the first array elements are zero. I only came up with a very cumbers...

Python File Slurp w/ endian conversion

It was recently asked how to do a file slurp in python: link text And it was recommended to use something like with open('x.txt') as x: f = x.read() How would I go about doing this to read the file in and convert the endian representation of the data? For example, I have a 1GB binary file that's just a bunch of single precision flo...

least square solution to camera matrix [numpy]

I would like to use use numpy's least square algorithm to solve for a camera matrix from 6 known 3D -> 2D point correspondence. I have been using this website as a reference: http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL%5FCOPIES/OWENS/LECT9/node4.html Currently my camera matrix seems to have very small values: [[ -1.01534118e-11 ...

pythoncomplete in vim - hardcode factory function returns?

I'm using pythoncomplete omnicompletion in vim. It works great when I instantiate classes directly, eg import numpy as np x = np.ndarray(l) then x attributes complete correctly. But I work with numpy and matplotlib so usually use factory functions ie x = np.zeros((2,2)) f = plt.figure() ax = f.add_subplot(111) Is there any wa...

Simplest way to solve mathematical equations in Python

Short Question: Lets say, I want to solve Project Euler problem 9 using python and some equation solving libraries. a + b + c = 1000. a2 + b2 = c2 How do you do it. Long Question: I want to solve a set of equations; linear, sometimes may be quadratic too. Not a specific problem; but often, I have been in situation I wanted to. I...

How to delete columns in numpy.array

I would like to delete selected columns in a numpy.array . This is what I do: n [397]: a = array([[ NaN, 2., 3., NaN], .....: [ 1., 2., 3., 9]]) In [398]: print a [[ NaN 2. 3. NaN] [ 1. 2. 3. 9.]] In [399]: z = any(isnan(a), axis=0) In [400]: print z [ True False False True] In [401]: delete(a, z, ax...

Range of valid numpy values

I'm interested in finding for a particular Numpy type (e.g. np.int64, np.uint32, np.float32, etc.) what the range of all possible valid values is (e.g. np.int32 can store numbers up to 2**31-1). Of course, I guess one can theoretically figure this out for each type, but is there a way to do this at run time to ensure more portable code? ...

Random int64 and float64 numbers

I'm trying to generate random 64-bit integer values for integers and floats using Numpy, within the entire range of valid values for that type. To generate random 32-bit floats, I can use: In [2]: np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo(np.float32).max,size=10) Out[2]: array([ 1.47351436e+37, 9.93620693e+37, 2...

Automatic string length in recarray

If I create a recarray in this way: In [29]: np.rec.fromrecords([(1,'hello'),(2,'world')],names=['a','b']) The result looks fine: Out[29]: rec.array([(1, 'hello'), (2, 'world')], dtype=[('a', '<i8'), ('b', '|S5')]) But if I want to specify the data types: In [32]: np.rec.fromrecords([(1,'hello'),(2,'world')],dtype=[('a',np...

Convert int64 to uint64

I want to convert an int64 numpy array to a uint64 numpy array, adding 2**63 to the values in the process so that they are still within the valid range allowed by the arrays. So for example if I start from a = np.array([-2**63,2**63-1], dtype=np.int64) I want to end up with np.array([0.,2**64], dtype=np.uint64) Sounds simple at fir...

Why does an assignment for double-sliced numpy arrays not work?

Hello everybody, why do the following lines not work as I expect? import numpy as np a = np.array([0,1,2,1,1]) a[a==1][1:] = 3 print a >>> [0 1 2 1 1] # I would expect [0 1 2 3 3] Is this a 'bug' or is there another recommended way to this? On the other hand, the following works: a[a==1] = 3 print a >>> [0 3 2 3 3] Cheers, Philip...

How to create 3 dimensions matrix in numpy , like matlab a(:,:,:)

How to create 3 dimensions matrix in numpy , like matlab a(:,:,:) . I try to convert matlab code that create 3d matrix to python by use numpy.array and i don't know how to create 3d matrix/array in numpy ...

Numpy, problem with long arrays.

I have two arrays (a and b) with n integer elements in the range (0,N). typo: arrays with 2^n integers where the largest integer takes the value N = 3^n I want to calculate the sum of every combination of elements in a and b (sum_ij_ = a_i_ + b_j_ for all i,j). Then take modulus N (sum_ij_ = sum_ij_ % N), and finally calculate the fre...

Convert little endian string to integer

I have read samples out of a wave file using the wave module, but it gives the samples as a string, it's out of wave so it's little endian (for example, '`\x00'). What is the easiest way to convert this into a python integer, or a numpy.int16 type? (It will eventually become a numpy.int16, so going directly there is fine). Code needs t...

Initializing numpy matrix to something other than zero or one

I have the following code: r = numpy.zeros(shape = (width, height, 9)) It creates a width x height x 9 matrix filled with zeros. Instead, I'd like to know if there's a function or way to initialize them instead to NaN. Is there any? Without having to resort to manually doing loops and such? Thanks ...

Loading and saving numpy matrix

I'm having troubles loading a numpy matrix. I successfully saved it to disk through: self.q.dump(fileName) and now I want to be able to load it. From what I understand, the load command should do the trick: self.q.load(fileName) but it seems not. Anyone knows what might be wrong? Maybe the function is not called load? ...

Combining two record arrays

I have two Numpy record arrays that have exactly the same fields. What is the easiest way to combine them into one (i.e. append one table on to the other)? ...

Accessing a matrix element by matrix[(a, b), c] instead of matrix[a, b, c].

I want to achieve the following: Have a AxBxC matrix (where A,B,C are integers). Access that matrix not as matrix[a, b, c] but as matrix[(a, b), c], this is, I have two variables, var1 = (x, y) and var2 = z and want access my matrix as matrix[var1, var2]. How can this be done? I am using numpy matrix, if it makes any difference. I k...

What is the equivalent of MATLAB's repmat in NumPy

I found repmat([1;1], [1 1 1]) in MATLAB code. I am trying to find a NumPy command to do the same thing, but I found that can't use with 3d [1 1 1] ? ...

Just curious about result from NumPy function!

I have used NumPy for my Master thesis. I've converted parts of the code from MATLAB code, but I have doubts in NumPy/Python when I reference: m = numpy.ones((10,2)) m[:,0] which returns: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) and when I ref to: m[:,0:1] it returns: array([[ 1.], [ 1.], [ 1.], ...