How to fill a numpy Python charrray with spaces?

For some reason I'm struggling to initialize a numpy.chararray with spaces. This works: char_array1 = np.chararray((3, 3)) char_array1[:] = 'a' char_array1 Output: chararray([['a', 'a', 'a'], ['a', 'a', 'a'], ['a', 'a', 'a']], dtype='|S1') This doesn

The most efficient way to store this vector?

I have a JSON string which contains a dictionary mapping index to float values. This is representative of a vector. For example, { 'distro': {0: 2.42, 3: 2.56}, 'constant': 4.55 'size': 10000 } represents a vector of size 10000 having 2.42 on index 0

What is an axis in Python with the Numpy module?

when I use np.stack, sometimes have to use axis, like axis=1. I don't understand what the axis means for it. for exmaple, c1 = np.ones((2, 3)) c2 = np.zeros((2, 3)) c = np.stack([c1, c2], axis = 1) this shows like, array([[[1., 1., 1.], [0., 0., 0.]]

Replace numpy functions with Python only

I have a python function that employs the numpy package. It uses numpy.sort and numpy.array functions as shown below: def function(group): pre_data = np.sort(np.array( [c["data"] for c in group[1]], dtype = np.float64 )) How can I re-write the s

Binary Matrix Entries

I have the following problem. I want to create a numpy-matrix of size 2^L x L+2. In the first column are variables, which I define later in the program. In the last L columns should be all possibilities to distribute zeroes and ones (In my opinion bi

Python version changes

I'm currently using python 2.7.1 with some packages as shows below In [4]: scipy.__version__ Out[4]: '0.17.0' In [5]: numpy.__version__ Out[5]: '1.10.4' In [6]: skimage.__version__ Out[6]: '0.12.3' Looking into the What's new page for python 3.5 i co

Propagation speed of the proper matrix

I was trying to do linear algebra numerical computation in C++. I used Python Numpy for quick model and I would like to find a C++ linear algebra pack for some further speed up. Eigen seems to be quite a good point to start. I wrote a small performan

Apply the numpy function according to the string

Is it possible to apply a numpy function based on a string ? If I give 'max' call np.max. values = np.array([[1,2,-1],[2,3,6], [0,-1,4]]) aggregator = 'max' print np.max(values, axis=0) >>> [2 3 6] What I hope is something like this : some_cool_f

How to create a multidimensional array from lists with NumPy?

There should be a way to turn a lists like this: a = [[1], [2], [3], [4], [5]] b = [[6], [7], [8], [9], [10]] to something like this: c = [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]] Right now I'm accomplishing this using for loops. c = [] for pos in ra

Vectorize iterative addition in NumPy tables

For each element in a randomized array of 2D indices (with potential duplicates), I want to "+=1" to the corresponding grid in a 2D zero array. However, I don't know how to optimize the computation. Using the standard for loop, as shown here, de

Numerical problem with np.exp ()

I have the following code x = -10 for i in range(2,10): print i, " | ",np.exp(-x**i) with the following output: 2 | 3.72007597602e-44 3 | inf 4 | 0.0 5 | inf 6 | 0.0 7 | inf 8 | 0.0 9 | inf Why is the results ~0 for i even and Inf for i odd?Sinc

Python function that manages scalars or tables

How best to write a function that can accept either scalar floats or numpy vectors (1-d array), and return a scalar, 1-d array, or 2-d array, depending on the input? The function is expensive and is called often, and I don't want to place a burden on

Group and average numerical matrix

Say I have an arbitrary numpy matrix that looks like this: arr = [[ 6.0 12.0 1.0] [ 7.0 9.0 1.0] [ 8.0 7.0 1.0] [ 4.0 3.0 2.0] [ 6.0 1.0 2.0] [ 2.0 5.0 2.0] [ 9.0 4.0 3.0] [ 2.0 1.0 4.0] [ 8.0 4.0 4.0] [ 3.0 5.0 4.0]] What would be an efficient way o

Trace thousands of files with python

I have in the order or 10^5 binary files which I read one by one in a for loop with numpy's fromfile and plot with pyplot's imshow. Each file takes about a minute to read and plot. Is there a way to speed things up? Here is some pseudo code to explai

Recursive comparison of Numpy with all data in the row

I have a booleen numpy array as follows: bool_arr = array([[ True, True, True, True], [False, False, True, True], [False, False, False, True]], dtype=bool) I want to compare, along the rows, returning True only for the first instance of True, otherwi

Calculate the overlap area of ​​two functions

I need to calculate the area where two functions overlap. I use normal distributions in this particular simplified example, but I need a more general procedure that adapts to other functions too. See image below to get an idea of what I mean, where t

get a total value of array on a loop, python

I have a loop which generates an array from a text file. Every time it passes through the loop I want it to add the new array to the old one but I'm not sure how to do this. For example: loop=np.arange(1,50) for arg in loop: str(arg) a=np.genfromtxt(

improved performance of Numpy points by removing copy of tables

Given a matrix QT: % ipython Python 2.7.3 In [3]: QT.dtype Out[3]: dtype('float64') In [4]: QT.__class__ Out[4]: numpy.ndarray In [5]: QT.flags Out[5]: C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True UPDATEIFCO

Terminology: Python and Numpy - `iterable` versus` array_like`

What is the difference between an iterable and an array_like object in Python programs which use Numpy? Both iterable and array_like are often seen in Python documentation and they share some similar properties. I understand that in this context an a

Statistics with numpy

I am working at some plots and statistics for work and I am not sure how I can do some statistics using numpy: I have a list of prices and another one of basePrices. And I want to know how many prices are with X percent above basePrice, how many are