If you are creating a 1d array in Python is there any benefit to using the NumPy package?
|
How can I build a numpy array out of a generator object?
Let me illustrate the problem:
>>> import numpy
>>> def gimme():
... for x in xrange(10):
... yield ...
|
Is there an existing function in numpy that will tell me if a value is either a numeric type or a numpy array? I'm writing some data-processing code which needs to ... |
Surely a 0d array is scalar, but Numpy does not seem to think so... am I missing something or am I just misunderstanding the concept?
>>> foo = numpy.array(1.11111111111, numpy.float64)
>>> numpy.ndim(foo)
0
>>> ...
|
I need to write a function which will detect if the input contains at least one value which is non-numeric. If a non-numeric value is found I will raise an error ... |
What is the cleanest way to add a field to a structured numpy array? Can it be done destructively, or is it necessary to create a new array and copy ... |
I'm learning to use Numpy and I wanted to see the speed difference in the summation of a list of numbers so I made this code:
np_array = numpy.arange(1000000)
start = time.time()
sum_ = ...
|
|
I would like to provide "all" mathematical functions for the number-like objects created by a module (the uncertainties.py module, which performs calculations with error propagation)—these objects are numbers with ... |
I have a few functions that return an array of data corresponding to parameters ranges.
Example: for a 2d array a, the a_{ij} value corresponds to the parameter set (param1_i, param2_j). How ... |
In pure Python you can grow matrices column by column pretty easily:
data = []
for i in something:
newColumn = getColumnDataAsList(i)
data.append(newColumn)
NumPy's array doesn't ... |
how do I calculate that an array of python numpy or me of all the calculate decimals and not skip like.
>> A = numpy.array ([[1,2,3], [4,5,6], [7,8,9]]).
>> C = numpy.array ([[7,8,9], ...
|
as a replacement value for another within an operation with arrays, or how to search within an array and replace a value by another
for example:
array ([[NaN, 1., 1., 1., 1., 1., ...
|
as I can perform operations on arrays so that does nothing on the diagonal
is calculated such that all but the diagonal
array ([[0., 1.37, 1., 1.37, 1., ...
|
I am looking for an efficient way to compress a numpy array.
I have an array like: dtype=[(name, (np.str_,8), (job, (np.str_,8), (income, np.uint32)] (my favourite example).
if I'm doing something like this: my_array.compress(my_array['income'] ... |
I have two arrays of x-y coordinates, and I would like to find the minimum Euclidean distance between each point in one array with all the points in the other array. ... |
When I print an array i get the following stuff, but I want the full array. Is there any way to do this? Thanks in advance, and everybody a happy, lucky, ... |
Currently I have a code that checks if given element in array is equal = 0 and if so then set the value to 'level' value (temp_board is 2D numpy array, ... |
I'm creating a numpy array which is to be filled with objects of a particular class I've made. I'd like to initialize the array such that it will only ever contain ... |
I have 8823 data points with x,y coordinates. I'm trying to follow the answer on how to get a scatter dataset to be represented as a heatmap but when I ... |
Given a character array:
In [21]: x = np.array(['a ','bb ','cccc '])
One can remove the whitespace using:
In [22]: np.char.strip(x)
Out[22]:
array(['a', 'bb', 'cccc'],
...
|
I swear this should be so easy... Why is it not? :(
In fact, I want to combine 2 parts of the same array to make a complex array:
Data[:,:,:,0] , Data[:,:,:,1]
These don't ... |
I am new to python, apologies if this has been asked already.
Using python and numpy, I am trying to gather data across many netcdf files into a single array by iteratively ... |
I am using SWIG to pass numpy arrays from Python to C++ code:
%include "numpy.i"
%init %{
import_array();
%}
%apply (float* INPLACE_ARRAY1, int DIM1) {(float* data, int n)};
class Class
{
public:
void test(float* ...
|
are there any way to create an object form any class inside a numpy array?. Something like:
a = zeros(4)
for i in range(4):
a[i]=Register()
Thanks
|
I have some data represented in a 1300x1341 matrix. I would like to split this matrix in several pieces (e.g. 9) so that I can loop over and process them. The ... |
I'd like to make a class extending the numpy array base type,
class LemmaMatrix(numpy.ndarray):
@classmethod
def init_from_corpus(cls, ...): cls(numpy.empty(...))
But apparently, it will not allow multi-dimensional array ... |
I'm curious, whether there is any way to print formated numpy.arrays, e.g., in the way similar to this:
x = 1.23456
print '%.3f' % x
If I want to print the numpy.array of floats, ... |
greetings,
I'm not sure if this is a dumb question or not.
Lets say I have 3 numpy arrays, A1,A2,A3, and 3 floats, c1,c2,c3
and I'd like to evaluate B = A1*c1+ A2*c2+ A3*c3
will ... |
I would like to store and load numpy arrays from binary files. For that purposes, I created two small functions. Each binary file should contain the dimensionality of the given matrix.
def ...
|
I have a strange problem in Python 2.6.5 with Numpy. I assign a numpy array, then equate a new variable to it. When I perform any operation to the new ... |
I have a label matrix with dimension (100*100), stored as a numpy array, and I would like to display the matrix with pyglet.
My original idea is to use this matrix to ... |
I am still writing on a python interface for my c code with ctypes. Today I substituted my file reading function with a python version, which was programmed by somebody else ... |
I want to create an array with dtype=np.object, where each element is an array with a numerical type, e.g int or float. For example:
>>> a = np.array([1,2,3])
>>> b = np.empty(3,dtype=np.object)
>>> b[0] ...
|
I am having a problem splicing together two arrays. Let's assume I have two arrays:
a = array([1,2,3])
b = array([4,5,6])
When I do vstack((a,b)) I get
[[1,2,3],[4,5,6]]
and if I do hstack((a,b)) I get:
[1,2,3,4,5,6]
But what ... |
I have a very large numpy array (containing up to a million elements) like the one below:
[ 0 1 6 5 1 2 7 ...
|
How do I write the following loop using Python's implicit looping?
def kl(myA, myB, a, b):
lots of stuff that assumes all inputs are scalars
x, y = meshgrid(inclusive_arange(0.0, xsize, ...
|
What is the most efficient way of serializing a numpy array using simplejson?
|
I am new to NumPy/SciPy. From the documentation, it seems more efficient to preallocate
a single array rather than call append/insert/concatenate.
For example, to add a column of 1's to an array, ... |
I have a 2D array of Numpy data read from a .csv file. Each row represents a data point with the final column containing a a 'key' which corresponds uniquely to ... |
Is there a fast way in numpy to add array A to array B at a specified location?
For instance, if
B = [
[0, 1, 2],
...
|
I have the following array in NumPy:
A = array([1, 2, 3])
How can I obtain the following matrices (without an explicit loop)?
B = [ 1 1 1
...
|
I'm trying to represent an array of evenly spaced floats, an arithmetic progression, starting at a0 and with elements a0, a0 + a1, a0 + 2a1, a0 + 3a1, ...
This is ... |
I'm writing some modelling routines in NumPy that need to select cells randomly from a NumPy array and do some processing on them. All cells must be selected without replacement (as ... |
I would like to convert numpy array to some double* or stl vector on the c++ side. I'm actually using PyCXX for this and I can't figure out the way to ... |
I have many csv files which each contain roughly identical matrices. Each matrix is 11 columns by either 5 or 6 rows. The columns are variables and the rows ... |
I have a 3d, 3x3x3 array of integers. Numpy will print these as a block of the first 3x3, then below it the 2nd 3x3, then below that the 3rd ... |
So I am trying to detect if the values in an array is in a certain range and then return a binary logical array i.e. one for true and zero for ... |
I am working in Python and I have a NumPy array like this:
[1,5,9]
[2,7,3]
[8,4,6]
How do I stretch it to something like the following?
[1,1,5,5,9,9]
[1,1,5,5,9,9]
[2,2,7,7,3,3]
[2,2,7,7,3,3]
[8,8,4,4,6,6]
[8,8,4,4,6,6]
These are just some example arrays, I will ... |
OK, I think this will be fairly simple, but my numpy-fu is not quite strong enough. I've got a an array A of ints; it's tiled N times. I want a ... |
I have two arrays that I want to re-size, but I also want to retain the original values. The code below re-sizes the arrays, but the problem is that it ... |
I am using a Python (via ctypes) wrapped C library to run a series of computation. At different stages of the running, I want to get data into Python, and ... |
I have a 2D numpy array. Is there a way to create a view onto it that would include the first k rows and all columns?
The point is to avoid copying ... |
My code below is not populating the numpy/pylab array called RRmeanArray. Can anyone show me how to fix the code so that it populates the array?
import ...
|
I have a 1D numpy array containing some audio data. I'm doing some processing and want to replace certain parts of the data with white noise. The noise should, however, be ... |
I'd like to perform a splice of sorts in Numpy. Let's say I have two arrays, a and b:
>>> a
array([[ 1, 10],
[ 2, 20],
...
|
from numpy import array
test_list = [[1,2],3]
x = array(test_list) #ValueError: setting an array element with a sequence.
Basically, I have a point with 2 coordinates and a scale and I was trying to ... |
I am beginner to Python and right now i'm writing a code to process some sensors data in python and store it in two arrays with the size of 1001x1 each. ... |
Is there way to initialize a numpy array of a shape and add to it? I will explain what I need with a list example. If I want to create a ... |
I have three arrays
import numpy as np
value = np.array ([1, 3, 3, 5, 5, 7, 3])
index = np.array ([1, 1, 3, 3, 6, 6, 6])
data = np.array ([1, 2, 3, ...
|
#compute first differences of 1d array
from numpy import *
x = arange(10)
y = zeros(len(x))
for i in range(1,len(x)):
y[i] = x[i] - x[i-1]
print y
The above code works but there must ... |
I'm trying to implement a simulation for a lattice model (lattice boltzmann) in Python. Each site of the lattice has a number of properties, and interact with neighboring sites according to ... |
I'm getting the following warning message when I try to use a ctypes array as a numpy array:
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", ...
|
I want to calculate the magnetic field of a wire using biot-savart-law. Some people recommend to use numpy arrays. At first i did it with vpython and it worked. But know ... |
I'd like to 'shear' a numpy array. I'm not sure I'm using the term 'shear' correctly; by shear, I mean something like:
Shift the first column by 0 places
Shift the second column ... |
Is there a good way to pass a large chunk of data between two python subprocesses without using the disk? Here's a cartoon example of what I'm hoping to accomplish:
import sys, ...
|
I'm new to numpy and require to accomplish the following task:
from:
a = array([[1,3,4],[1,2,3]...[1,2,1]])
(add one element to each row) to:
a = array([[1,3,4,x],[1,2,3,x]...[1,2,1,x]])
How can i accomplish this?
I have tried doing stuff like a[n] ... |
I would like to use bincount to sum arrays, however it supports only doubles.
For example this works:
np.bincount([1, 1, 0],weights=np.array([1, 2, 4]))
Out: array([ 4., 3.])
However I would like to use a ... |
I am attempting to plot array values obtained from summing individual columns of a numpy array.
Working on Win XP, Python 2.5, matplotlib-1.0.1, numpy-1.5.1, PIL-1.1.7
Here is the code:
import Image
import numpy as ...
|
I have an ordered array like this: numpy.array([1, 2, 5, 10, 25, 36, 66, 90, 121, 230, 333, 500])
Suppose I want all values up to 60 (if 60 isn't in, i ... |
I have a file containing data in the format:
0.0 x1
0.1 x2
0.2 x3
0.0 x4
0.1 x5
0.2 x6
0.3 x7
...
The data consists of multiple datasets, each starting with 0 in the first column (so x1,x2,x3 ... |
I have the following array:
>>> x = numpy.array([2,4,2,3,1])
>>> x
array([2, 4, 2, 3, 1])
I would like an array of ranges of these values. I can create it like this:
>>> numpy.hstack( (numpy.arange(v) for ...
|
I'd like to access data in a C program that resides in a file that was written from python using numpy.save(...). So far, I was looking for a way to call ... |
Assume the following arrays are given:
a = array([1,3,5])
b = array([2,4,6])
How would one interweave them efficiently so that one gets a third array like this
c = array([1,2,3,4,5,6])
It can be assumed that length(a)==length(b).
... |
Input
I have many numpy structured arrays in a list like this example:
import numpy
a1 = numpy.array([(1, 2), (3, 4), (5, 6)], dtype=[('x', int), ('y', int)])
a2 = numpy.array([(7,10), (8,11), (9,12)], dtype=[('z', ...
|
import Image
from numpy import zeros, asarray
YUV = zeros((240, 320, 3), dtype='uint8')
im = Image.fromarray(YUV, mode="YCbCr")
blah = asarray(im)
When I run this (IPython 0.10.1 on Py 2.7.1) it seems to make python read some ... |
I have a numpy array:
[[ 0. 1. 2. 3. 4.]
[ 7. 8. 9. 10. ...
|
I have an array n x m, and maximum values for each column. What's the best way to replace values greater than the maximum, besides checking each element?
For example:
def check_limits(bad_array, maxs):
...
|
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 ... |
I am having a weird subclass numpy.ndarray issue that feels like
Values of instance variables of superclass persist across instances of subclass
But I have not been able to understand fully or ... |
I'm trying to draw polygons like this:
In [1]: canvas = numpy.zeros((12, 12), dtype=int)
In [2]: mahotas.polygon.fill_polygon(
...: [(1, 1), (1, 10), (10, 10), (10, 1)],
...: canvas)
In [3]: ...
|
I'm trying to fill a preallocated bytearray using the following code:
# preallocate a block array
dt = numpy.dtype('u8')
in_memory_blocks = numpy.zeros(_AVAIL_IN_MEMORY_BLOCKS, dt)
...
# write all the blocks out, flushing only as desired
blocks_per_flush_xrange = xrange(0, ...
|
Is there anything I can do to speed up masked arrays in numpy? I had a terribly inefficient function that I re-wrote to use masked arrays (where I could just ... |
I have a numpy array like
x=np.array([1,2,3,4])
I want to create another numpy array y which is the cumulative sum of x, so that
y=np.array([1,3,6,10])
What is a good num-Pythonic way to ... |
I am waiting for another developer to finish a piece of code that will return an np array of shape (100,2000) with values of either -1,0, or 1.
In the meantime, I ... |
I have a numpy array like
x=np.array([0,1,2,3,4])
and want to create an array where the value in index 0 is in index 1, index 1 is in index 2, etc.
The output I want ... |
I need to create a numpy array of length n, each element of which is v.
Is there anything better than:
a = empty(n)
for i in range(n):
a[i] = v
I ... |
With below, I can get the row and col data from SQL:
How would I get the table headers as part of the result set or array.?
...
|
I have a structured array, for example:
import numpy as np
orig_type = np.dtype([('Col1', '<u4'), ('Col2', '<i4'), ('Col3', '<f8')])
sa = np.empty(4, dtype=orig_type)
where sa looks like (random data):
array([(11772880L, 14527168, 1.079593371731406e-307),
...
|
I am trying to convert a numpy array into a Java-like array using JPype's JArray command.
values = valBD.ReadAsArray()
JArray(float, values.ndim)(values)
leads to the following error message:
JArray(float, values.ndim)(values)
File
... |
Since when did numpy allow you to define an array of python objects? Objects array with numpy.
Is there any fundamental difference between these arrays and a python list?
What is the ... |
Given a file in the format below:
a a 0
a b 1
a c 1
b b 0
b a 1
b c 1
c c 0
c a 1
c b 1
The third column is the distance between ... |
I have a numpy array:
[[1 3 1]
[8 9 0]
[1 3 1]
[8 4 1]
[5 1 0]]
and I want to produce subtotals (count, sums, averages) for columns 0,1 for ... |
I'm trying to use numpy's savetxt function to generate a bunch of files as inputs for another piece of software.
I'm trying to write an array of the form:
a=np.array([1,2,3,4,...])
a.shape=>(1,n)
to a text file ... |
What is the fastest way to copy data from array b to array a, without modifying the address of array a. I need this because an external library (PyFFTW) uses a ... |
This to understand things better. It is not an actual problem that I need to fix. A cstringIO object is supposed to emulate a string, file and also an iterator over ... |
I know how to get a ctypes pointer to the beginning of a numpy array:
a = np.arange(10000, dtype=np.double)
p = a.ctypes.data_as(POINTER(c_double))
p.contents
c_double(0.0)
however, I need to pass the pointer to, let's say, element 100, ... |
I have a numpy array with a custom dtype:
a = np.zeros(100, dtype=np.dtype([('one',np.double),('two',np.int)]))
a['one']=np.arange(100)
a['two']=np.arange(100)*-1
I want to create a ctypes pointer that I can pass to a C library.
The problem is that the C ... |
I have allocated a chunk of double in a C library and I would like to create a numpy 1D array based on that data; ideally I would like two versions ... |
import numpy as np
import numpy.ma as ma
"""This operates as expected with one value masked"""
a = [0., 1., 1.e20, 9.]
error_value = 1.e20
b = ma.masked_values(a, error_value)
print b
"""This does not, all values are masked ...
|
I'd like to use python's multiprocessing module to utilize a multi-core Linux server.
I need all processes to have read/write access to the same shared memory.
Instead of using a list or ... |