Tagged Questions
1
vote
1answer
13 views
Create 2D array from where clause on 1D array numpy
I have a 1D array containing integer values:
a = np.array([1,2,3,3,2,2,3,2,3])
a
array([1, 2, 3, 3, 2, 2, 3, 2, 3])
I would like to create a 2D array with the first dimension holding the index of ...
1
vote
2answers
20 views
Memory Error with numpy on several large arrays
So I am trying to carry out the following calculations on a series of large arrays but I keep getting the error:
MemoryError
In total there are 9 grain_size arrays 2745 by 2654 (Note: I could use ...
2
votes
1answer
17 views
Obtain one number from numpy subarrays of size 2 given its peer
I have a array with pairs of numbers (subarrays of size 2) like this:
pairs = np.array([[1, 2],[5, 12],[9, 33],[9, 1],[34,7]])
and the peers array, like this:
nums = np.array([1,12,9])
What I ...
1
vote
3answers
25 views
numpy array print index of certain value
Given a numpy array
A = np.array([[[29, 64, 83],
[17, 92, 38],
[67, 34, 20]],
[[73, 28, 45],
[19, 84, 61],
[22, 63, 49]],
...
0
votes
1answer
38 views
Is there a faster way to search a numpy array
I have a numpy array of roughly 3125000 entries the data is structured using the following dtype
dt = np.dtype([('startPoint', '<u8' ), ('endPoint', '<u8')])
The data is from a file that has ...
0
votes
2answers
38 views
Determine sum of numpy array while excluding certain values
I would like to determine the sum of a two dimensional numpy array. However, elements with a certain value I want to exclude from this summation. What is the most efficient way to do this?
For ...
-1
votes
3answers
35 views
Numpy array construction using tuples
C = numpy.array([a^b for a,b in A,B])
Is what I attempted. I thought it would xor each individual element in A and B which are matrices of their own and store it as the same shape within C. How ...
2
votes
2answers
46 views
Producing an array from an ellipse
I have an equation that creates an ellipse in the general form x^2/a^2 + y^2/b^2 = 1. I wish to produce an array whereby all points inside the ellipse are set to one and all points outside are a zero. ...
0
votes
1answer
32 views
numpy.ndarray.shape changing dimension
The tuple holding the dimensions of a numpy array (numpy.ndarray.shape) changes size. E.g:
len(numpy.array([1,2,3]).shape) -> 1, shape=(1,)
len(numpy.array([[1,2,3],[4,5,6]]).shape) -> 2, ...
1
vote
3answers
63 views
How to declare and fill an array in python?
I am new to python syntax not new to programing. I need to create an empty array in python and fill it in a loop method.
data1 = np.array([ra,dec,[]])
Here is what I have. The ra and dec portions ...
2
votes
3answers
37 views
Comparing a numpy array object to multiple conditions
I am trying to use numpy.where to find the indices I want. Here's the code:
import numpy as np
a = np.array([20,58,32,0,107,57]).reshape(2,3)
item_index = np.where((a == 58) | (a == 107) | (a == 20))
...
0
votes
4answers
32 views
Simpler way to create a matrix/list of indices?
I wonder what could be the easiest way to create a bi-dimensional array, that has for each row the indices to another multi-dimensional array.
For example, let's say I have a cube 4x4, the "indices ...
1
vote
1answer
36 views
Binning data based on one column in 2D array and estimate mean in each bin using cython
In order to optimize the speed of my code which is very vital for the speed of my MCMC, I want to substitute some of the bottlenecks of my python code with cython. Since I am working with a huge two ...
0
votes
2answers
44 views
Determine range of elements that do not occur in any of several arrays
I have several arrays denoting x- and y-coordinates. What I would like to do is to determine the y-range for which none of the resulting lines overlap with one another. To illustrate what I mean I ...
2
votes
1answer
64 views
Indexing and slicing structured ndarrays
Now I'm trying to understand possible ways to index numpy structured arrays, and I kinda get stuck with it. Just a couple of simple examples:
import numpy as np
arr = np.array(zip(range(5), range(5, ...
0
votes
1answer
16 views
Numpy return recarray with 'column headings' and 'dtype'?
I am following this example explicitly:
http://wiki.scipy.org/Cookbook/InputOutput#head-d528c8c74e765542c351a768b47c7bc9a2ca8e85
import numpy
def readarray(filename, dtype, separator=','):
...
0
votes
1answer
31 views
Manipulating a large binary image array with numpy and cv2
My code is the following:
import cv2; import numpy as np
class MyClass:
def __init__(self,imagefile):
self.image = cv2.imread(imagefile)
#image details
self.h,self.w = ...
0
votes
1answer
36 views
Numpy arctan2 of multidimensional array
I am trying to shape up some code that was written to take single float values, so it works fine using 1D (and eventually 2D) numpy.arrays as input.
Striped down to a minimal example the function ...
-2
votes
2answers
46 views
Re-arrangement within a numpy array
I have a numpy array that looks like as follows:
[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2. ...
3
votes
1answer
55 views
Creating new numpy array from existing array efficiently
I have existing numpy array (uint8) which looks like this:
(Values are 8bit, i am interested only in last 3 of them)
[
[ 00000AAA, 00000BBB, 00000CCC ],
[ 00000FFF, 00000EEE, 00000DDD ],
...
2
votes
2answers
28 views
Ignoring negative values when using np.log(array)
When taking the log of a specific column within a numpy array, i.e., logSFROIIdC = np.log(data_dC[:, 9]) the compiler returns the error:
-c:13: RuntimeWarning: divide by zero encountered in log.
...
1
vote
3answers
61 views
using index values to select items from a numpy array
Suppose I have a pair of numpy arrays X and I that look like this (X is 2D, I is 1D)
X I
-----------------
3.4 9.13 0
3.5 3.43 1
3.6 2.01 2
3.7 6.11 0
3.8 ...
1
vote
1answer
29 views
Access individual elements of an array of several arrays, Python
I am using a function that returns array of several arrays let say A. I don't know how to access each individual element. Thus, I am thinking to rearrange A to a matrix of two arrays so I could access ...
1
vote
1answer
31 views
How to get cumulated values from timestamped arrays in even intervals [PYTHON]
I have a numpy array with timestamps in seconds
For example, this array named a:
a = np.array(10,95,99,100,250)
Then I have an array b which gives me the amount of shares that come in ...
1
vote
3answers
27 views
Creating an array for each column of a data frame by column name
I am trying to create a number of arrays that correspond to the data values of each column in a large data frame, and I want each array to share the same name as its corresponding column. Here is an ...
0
votes
1answer
30 views
Reading in a text file as a numpy array with float values
I do not know python at all thus I have been unsuccessful in interpreting similar previous answers and using them.
I have a python script that I wish to execute in unix. The script uses an input file ...
2
votes
1answer
41 views
Implementing np.subtract.outer for index of array([[array … structure in python
I have the following set of indices;
ind = array([[array([0, 1, 4])],
[array([0, 1, 2, 5])],
[array([1, 2, 3, 6])],
[array([2, 3, 7])],
[array([0, 4, 5, ...
1
vote
1answer
37 views
BytesIO stream to Numpy arrays? (Picamera)
I'm using Picamera with the Raspberry Pi to take pictures and then analyze the differences between them. I'm using the following code to capture to a BytesIO stream and then read that into a Numpy ...
2
votes
1answer
24 views
numpy irregularly strided array
Quoting the documentation on numpy array structure in memory:
Several algorithms in NumPy work on arbitrarily strided arrays.
However, some algorithms require single-segment arrays. When an
...
0
votes
0answers
21 views
Declaring arrays of different sizes within a pre-existing array in python
I have been trying to declare a three dimensional array with the following constraints using numpy in Python 2.7.3:
1.) MyArray(First, Second, Third)
2.) First has 11 elements
3.) Second has 14 ...
2
votes
1answer
47 views
Apply a pairwise shapely function on two numpy arrays of shapely objects
I have two arrays of different length. One contains shapely polygons, the other contains shapely points. I want to run the a_polygon.contains(a_point) shapely function for every possible combination ...
0
votes
3answers
35 views
Augmenting a matrix in Python using numpy
I'm trying to augment a matrix to solve an equation, yet have been unable to. And yes, I saw the "Augment a matrix in NumPy" question; it is not what I need.
So my problem: create an augmented matrix ...
0
votes
3answers
23 views
Changing shape of numpy array read from a text file
I am working in python and am loading a text file that looks like this:
3 4
5 6
7 8
9 10
I use np.loadtxt('filename.txt') and it outputs an array like this:
([[3, 4]
[5, 6]
[7, ...
0
votes
1answer
51 views
Total size of array must be unchanged
I am using a Python module called emcee to sample a distribution. I need to pass a (37,100) (which I have named Ntrig and Nsamp, respectively) array called events to the below function.
def mp(SNR2, ...
0
votes
1answer
29 views
TypeError: return arrays must be of ArrayType for a function that uses only floats
This one really stumps me. I have a function that calculates the weight of a word, I've confirmed that both a and b local variables are of type float:
def word_weight(term):
a = term_freq(term)
...
0
votes
1answer
39 views
use python to raise an exception if a user-defined class is passed an array of the incorrect shape
I'm writing a class that gets passed an array. I want to raise an exception unless that array is the correct length. I would prefer to accomplish this using the "try...except" feature of python, and ...
0
votes
1answer
33 views
Reshaping arrays in an array of arrays
I have an array of 40 arrays, each with a 1x150 shape. Is there a way to reshape the arrays so that I have 40 arrays of 3x50 arrays?
I am not sure if there is a way to use np.reshape and just do it ...
2
votes
2answers
28 views
Array of a subclass of numpy
I have a problem. Problem is: I want to create a subclass of numpy array, and then make an array of objects of that type. When I reference an item in that array, I want it still to be an instance of ...
0
votes
1answer
40 views
Delete dimension of array?
I have a numpy array of shape (0, 2000, 6). I would like to delete the first dimension so that the array's shape is (2000,6). Is there a simple way to do this?
0
votes
2answers
50 views
Syntax Error in function definition
I'm trying to define a function that will integrate a function that takes an array as an input. I understand that this may not be possible, and was wondering what can be done to reconcile this? Here ...
1
vote
1answer
23 views
How to make the elements of a NumPy array property settable?
I have a property of a Python object that returns an array.
Now, I can set the setter of that property such that the whole array is settable.
However, I'm missing how to make the elements by ...
-1
votes
3answers
67 views
Finding the consecutive zeros in a numpy array
I have the following array
a = [1, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0, 0, 0, 0, 9, 8, 7,0,10,11]
What I would like to find the start and the end index of the array where the values are zeros ...
1
vote
2answers
39 views
Copy values from numpy array to another array depending on a third value
im using numpy 2d arrays. I want to copy values from one array to another array, but only if an expression is true.
e.g.
for row in range(len(anImage1)):
for col in range(len(anImage1[0])):
if ...
0
votes
1answer
35 views
Writing an ASCII file from a 2D numpy array
I have a 2D numpy array. I want to make a ASCII file out of it. Here's the code I am using where a3 is the numpy array
f = open('ASCIIout.asc', 'w')
numpy.savetxt(ASCIIout, a3)
f.write("ncols " + ...
0
votes
0answers
16 views
Is there a function similar to Sympy's solve that will take in an array?
I'm trying to solve a quadratic equation and I've had great success using Sympy's soler, but I want to solve for a changing value of a, b, and c (in the traditional format ax**2 + bx + c = 0) that ...
2
votes
1answer
29 views
python: how to add column to record array in numpy
I am trying to add a column to a numpy record.
This is my code:
import numpy
import numpy.lib.recfunctions
data=[[20140101,'a'],[20140102,'b'],[20140103,'c']]
data_array=numpy.array(data)
...
1
vote
1answer
52 views
How to access numpy array passed through Cython
I am trying to understand which is the fastest and safest way to access a NumPy array passed through Cython to a C code.
I have the following files:
func.c:
typedef struct {
int nr; ...
0
votes
1answer
28 views
Python - Avarage values of numpy multidimensionnal array every specific time
I have a multidimensionnal array "test[:,:,:]" and i would like to get averaged values on the test.shape[0] dimension for every 4 "frames" i would like to keep the same dimensions of my array and ...
4
votes
3answers
61 views
Python - Sum 4D Array
Given a 4D array M: (m, n, r, r), how can I sum all the m * n inner matrices (of shape (r, r)) to get a new matrix of shape (r * r)?
For example,
M [[[[ 4, 1],
[ 2, 1]],
[[ ...
1
vote
2answers
30 views
What's the numpy/pythonic way to non-destructively replace a column x of a matrix with f(x)?
I have a matrix M:
import numpy
M = numpy.array([[1,2,3], [4,5,6]])
I want a function that returns an array with every entry x of the colth column of M replaced by f(x), but doesn't modify the ...