Skip to content

Tag: numpy

best way to preserve numpy arrays on disk

I am looking for a fast way to preserve large numpy arrays. I want to save them to the disk in a binary format, then read them back into memory relatively fastly. cPickle is not fast enough, unfortunately. I found numpy.savez and numpy.load. But the weird thing is, numpy.load loads a npy file into “memo…

How do you get the magnitude of a vector in Numpy?

In keeping with the “There’s only one obvious way to do it”, how do you get the magnitude of a vector (1D array) in Numpy? The above works, but I cannot believe that I must specify such a trivial and core function myself. Answer The function you’re after is numpy.linalg.norm. (I reckon…

Extracting specific columns in numpy array

This is an easy question but say I have an MxN matrix. All I want to do is extract specific columns and store them in another numpy array but I get invalid syntax errors. Here is the code: It seems like the above line should suffice but I guess not. I looked around but couldn’t find anything syntax wise…

Add single element to array in numpy

I have a numpy array containing: I want to create an array containing: That is, I want to add the first element on to the end of the array. I have tried the obvious: But I get an error saying ValueError: arrays must have same number of dimensions I don’t understand this – the arrays are both just …

Vectorize over the rows of an array

I have an array X and I want to apply a function f to all the rows of X: Now, y should be array([15,30], ‘i’). Which method or slicing magic will implement rows in the most efficient way? Answer NumPy implements the concept of “action over a particular axis”. The general function is nu…

How does python numpy.where() work?

I am playing with numpy and digging through documentation and I have come across some magic. Namely I am talking about numpy.where(): How do they achieve internally that you are able to pass something like x > 5 into a method? I guess it has something to do with __gt__ but I am looking for a detailed expla…

Immutable numpy array?

Is there a simple way to create an immutable NumPy array? If one has to derive a class from ndarray to do this, what’s the minimum set of methods that one has to override to achieve immutability? Answer You can make a numpy array unwriteable: Also see the discussion in this thread: http://mail.scipy.org…