np.loadtxt for a file with multiple matrices

I have a file that looks something like this: some text the grids are 3 x 3 more text matrix marker 1 1 3 2 4 7 4 2 9 1 1 new matrix 2 4 9 4 1 1 3 4 4 3 1 new matrix 3 3 7 2 1 1 3 4 2 3 2 .. the file continues, with several 3x3 matrices appearing in

Produce a spectrogram from the microphone

Below I have code that will take input from a microphone, and if the average of the audio block passes a certain threshold it will produce a spectrogram of the audio block (which is 30 ms long). Here is what a generated spectrogram looks like in the

Insert / Add / Update elements in the matrix

Let's say I've initialized a matrix/array that has 400 rows, 3 columns: distances = np.zeros([400, 3], dtype=np.float64) Now, I have a for loop that returns 1200 objects (float values) and I want to "append" each element into distances (row by r

Effective way to find rectangle coordinates in 0-1 networks

Say I have an MxN matrix of 0's and 1's. It may or may not be sparse. I want a function to efficiently find rectangles in the array, where by rectangle I mean: a set of 4 elements that are all 1's that create the 4 corners of a rectangle, such that t

python indexing and affect by ndarray line

Something is wrong in my script and I found the error, but I'm completely stuck. there is array b which contains two elements: b = np.zeros ((1,2)) b[0,0] = 272 b[0,1] = 1578 I want to check if there are elements in the second columns, greater than a

how to add a numpy matrix in an empty array

I want to append a numpy array(matrix) into an array through a loop data=[[2 2 2] [3 3 3]] Weights=[[4 4 4] [4 4 4] [4 4 4]] All=np.array([]) for i in data: #i=[2 2 2 ] #for example h=i*Weights #h=[[8 8 8][8 8 8][8 8 8]] All=np.concatenate((All,h),ax

Quick queue for reading numpy tables only

I have a multiprocessing job where I'm queuing read only numpy arrays, as part of a producer consumer pipeline. Currently they're being pickled, because this is the default behaviour of multiprocessing.Queue which slows down performance. Is there any

Matlab range in Python

I must translate some Matlab code into Python 3 and I often come across ranges of the form start:step:stop. When these arguments are all integers, I easily translate this command with np.arange(), but when some of the arguments are floats, especially

Python - Statistical distribution

I'm quite new to python world. Also, I'm not a statistician. I'm in the need to implementing mathematical models developed by mathematicians in a computer science programming language. I've chosen python after some research. I'm comfortable with prog

Problems modeling the two-body orbit

Skip to Update 2 below, if you don't want to read too much background. I'm trying to implement a model for simple orbital simulations (two body). However, when I try to use the code I've written, the plots generated from the result look quite odd. Th

max number vs max amax vs

numpy has three different functions which seem like they can be used for the same things --- except that numpy.maximum can only be used element-wise, while numpy.max and numpy.amax can be used on particular axes, or all elements. Why is there more th

Reading a file contains lists of values ​​in a table

I have a big text file which is an output of some codes and it contains lists of numbers. The format of the lists in the file is as following [ 11.42102518 3.3538624 231.82137052 352.12777653] [ 12.68274035 2.84982539 292.99135783 331.74058558] [ 11.

Performance comparison Fortran, Numpy, Cython and Numexpr

I have following function: def get_denom(n_comp,qs,x,cp,cs): ''' len(n_comp) = 1 # number of proteins len(cp) = n_comp # protein concentration len(qp) = n_comp # protein capacity len(x) = 3*n_comp + 1 # fit parameters len(cs) = 1 ''' k = x[0:n_comp]

convert the channel list to a numeric list of lists

I'm reading data (numbers) from a file into a list, as follows: weight_file = open(model_name, 'r').readlines() weights = weight_file[6:] It seems that I can't read them straight into a numpy.array because the first rows of the file contains words. S

Python defaultdict for large datasets

I am using defaultdict to store millions of phrases, so my data structure looks like mydict['string'] = set(['other', 'strings']). It seems to work ok for smaller sets but when I hit anything over 10 million keys, my program just crashes with the hel

Distance to non-consecutive elements in a numpy array

Using numpy or itertools is there a efficient way to determine the distance to next non-consecutive elements. > import numpy as np > a=np.array(['a','b','b','c','d','a','b','b','c','c','c','d']) I would want the output to be. [1, 2, 1, 1, 1, 1, 2, 1

Numpy python How to change values ​​effectively

In my program I current create a numpy array full of zeros and then for loop through each element replacing it with the desired value. Is there a more efficient way of doing this? Below is an example of what I am doing however, instead of a int I hav

Finding the number of zero elements in the data

I have the code: def find_zeros(data): '''creates a list of the indexes of the zeros in the data''' zeroidx=np.where(np.any(data==0, axis=1)) print zeroidx return len(zeroidx) But the result is: (array([525, 526, 527, 528, 529, 530, 531, 532, 533, 53

Find the root of digital integration

I am trying to reproduce this Mathematica program in Python: It finds the roots of an numerical integration, and forms a plot of these values. However, I cannot get my attempt to run. Current attempt: from scipy.integrate import quad from scipy impor

Multidimensional Euclidean Distance in Python

I want to calcuate the eucledian distance in multiple dimensions (24 dimensions) between 2 arrays. I'm using Numpy-Scipy. Here is my code: import numpy,scipy; A=numpy.array([116.629, 7192.6, 4535.66, 279714, 176404, 443608, 295522, 1.18399e+07, 7.742

Get cross-lines on two numpy 2D arrays

I want to get the intersecting (common) rows across two 2D numpy arrays. E.g., if the following arrays are passed as inputs: array([[1, 4], [2, 5], [3, 6]]) array([[1, 4], [3, 6], [7, 8]]) the output should be: array([[1, 4], [3, 6]) I know how to do

assign points to bins

What is a good way to bin numerical values into a certain range? For example, suppose I have a list of values and I want to bin them into N bins by their range. Right now, I do something like this: from scipy import * num_bins = 3 # number of bins to