Skip to content
Advertisement

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 “memory-map”. That means regular manipulating of

Concatenating two one-dimensional NumPy arrays

How do I concatenate two one-dimensional arrays in NumPy? I tried numpy.concatenate: But I get an error: TypeError: only length-1 arrays can be converted to Python scalars Answer Use: The arrays you want to concatenate need to be passed in as a sequence, not as separate arguments. From the NumPy documentation: numpy.concatenate((a1, a2, …), axis=0) Join a sequence of arrays

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 it should be in base numpy as a

How does one find the largest consecutive set of numbers in a list that are not necessarily adjacent?

For instance, if I have a list This algorithm should return [1,2,3,4,5,6,7,8,9,10,11]. To clarify, the longest list should run forwards. I was wondering what is an algorithmically efficient way to do this (preferably not O(n^2))? Also, I’m open to a solution not in python since the algorithm is what matters. Thank you. Answer Here is a simple one-pass O(n) solution:

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 regarding

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 1d arrays.

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 numpy.apply_along_axis(): (where sum can of course be

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 explanation. Answer How

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/pipermail/numpy-discussion/2008-December/039274.html and the documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flags.html

Advertisement