How to seperate multiple matrices from for loop? - python

I have a function the gives a matrix as a result, since im using a for loop and append the results are 20 matrices in an array. I would like to add up the lower and the upper values of every matrix. np.sum(np.tril(matrix, -1)) will add up the values of all the matrices. Is it possible to do it per matrix? Or can i get 20 seperate matrices to do this?
matrix = []
for i in clubs:
matrix.append(simulate_match(poisson_model, 'ARSENAL', i, max_goals=10))

Related

Cross correlation of subarrays

I want to get cross correlation each of subarray. I only find the code of correlation of given arrays, but not of subarrays.
I have one array, but the array has subarrays.
z=[sensor_z[pre: next] for pre, next in zip(peak_pos,peak_pos[1:len(peak_pos)])]
Thats my array, the array is divided in subarrays.
What i want is something like
k = []
for elem in z:
k.append(np.correlate(elem,elem+1))
print (k)
I want to calculate the cross correlation of each subarray. I want to compare all subarrays and get the cross correlation of each subarray.
Does anyone an idea how to get this?

How to define a nested matrix?

I am new to coding and Python and I would be grateful if someone could help me with this problem. I want to define a 200x1 matrix every cell of which is a 8X17 matrix and the cells of this one are 1x3 vectors.
The values in the 1x3 vectors are generated randomly (from different sets of data). I want to run a genetic algorithm and this would define 200 members of my initial populations.
I have tried this in MATLAB using cell arrays; but they significantly slow down my code.
This is what I have tried in MATLAB.
P = cell (200, 1);
for i = 1:200
P{i,1} = cell(8, 17);
for j = 1:8
for k = 1:17
P{i,1}{j,k} = zeros(1,3);
P{i,1}{j,k}(1,1) = randi(Hmax{j}(k));
P{i,1}{j,k}(1,2) = randi([Hmin{j}(k),Hmax{j}(k)]);
P{i,1}{j,k}(1,3) = randi(Wmax{j}(k));
end
end
end
Hmin, Hmax, and Wmax are 8x17 matrices of data which have been imported as cell arrays. They are simple numbers which provide a range for the randomly generated values in 1x3 vectors.
And for those wondering what MATLAB cell arrays are, see
Cell Arrays.

Broadcasting the dot product of a list of 2D points with a 2x2 matrix

I have a list of points xy with the shape(2,100). I want to take the dot product with a 2x2 matrix as follows:
g = xy.T#W#xy
which should result in a vector of 100 values. How can I do this with Python?
I know it should result in 100 values because the above express works well if I feed in one 2D point. How can I vectorize the above?
We can np.einsum -
np.einsum('ij,ik,kj->j',xy,W,xy, optimize=True)

Efficient NumPy rows rotation over variable distances

Given a 2D M x N NumPy array and a list of rotation distances, I want to rotate all M rows over the distances in the list. This is what I currently have:
import numpy as np
M = 6
N = 8
dists = [2,0,2,1,4,2] # for example
matrix = np.random.randint(0,2,(M,N))
for i in range(M):
matrix[i] = np.roll(matrix[i], -dists[i])
The last two lines are actually part of an inner loop that gets executed hundreds of thousands of times and it is bottlenecking my performance as measured by cProfile. Is it possible to, for instance, avoid the for-loop and to do this more efficiently?
We can simulate the rolling behaviour with modulus operation after adding dists with a range(0...N) array to give us column indices for each row from where elements are to be picked and shuffled in the same row. We can vectorize this process across all rows with the help of broadcasting. Thus, we would have an implementation like so -
M,N = matrix.shape # Store matrix shape
# Get column indices for all elems for a rolled version with modulus operation
col_idx = np.mod(np.arange(N) + dists[:,None],N)
# Index into matrix with ranged row indices and col indices to get final o/p
out = matrix[np.arange(M)[:,None],col_idx]

How to extract vector from 3d numpy array?

I have a set of numpy.arrays of NXM (two dimensions: Range and Azimuth).
I need to form a stack of three dimensions and extract a single dimension vector to compute a covariance matrix (the red vectors in the picture).
How i do this efficiently and easy in Python?
You can make a 3D numpy array pretty easily and then just use the indexing to pull out the bits that you're interested in:
stackOfImages = np.array((image1, image2)) #iterate over these if many more
redData = stackOfImages[:, N-1, M-1]

Categories

Resources