Python Iterating 2D Array, Return Array Value - arrays

I have created a 2D 10x10 Array. using Numpy I want to iterate over the array as efficiently as possible.
However I would like to return the array values. essentially iterating over the 10x10 array 10 times and return a 1x10 array each time.
import datetime
import numpy as np
import random
start = datetime.datetime.now()
a = np.random.uniform(low=-1, high=1, size=(10,10))
print("Time :",datetime.datetime.now() - start)
for x in np.nditer(a):
print(x)
the result is as follows:
0.5738994777717537
0.24988408410910767
0.8391827831682657
0.0015975845830569213
0.54477459840569
0.14091622639476165
-0.36517132895234106
-0.06311125453484467
-0.6572544506539948
...
100 times
However I would expect the result to be:
[0.5738994777717537,
0.24988408410910767,
0.8391827831682657,
0.0015975845830569213,
0.54477459840569,
0.14091622639476165,
-0.36517132895234106,
-0.06311125453484467,
-0.6572544506539948],[...]
...
10 times
Any help would be appreciated!

To directly answer your question, this does exactly what you want:
import numpy as np
a = np.random.uniform(low=-1, high=1, size=(10,10))
print(','.join([str(list(x)) for x in a]))
This will print
[-0.2403881196886386, ... , 0.8518165986395723],[-0.2403881196886386, ... , 0.8518165986395723], ..., [-0.2403881196886386, ... , 0.8518165986395723]
The reason you're printing just the elements of the array is due to the way nditer works. nditer iterates over single elements, even at a multidimensional level, whereas you want to iterate over just the first dimension of the array. For that, for x in a: works as intended.
Edit
Here is a good link if you want to read up on how nditer works: https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html#arrays-nditer

Related

How to get the mean of first values in arrays in matrix in Matlab

If I have a square matrix of arrays such as:
[1,2], [2,3]
[5,9], [1,4]
And I want to get the mean of the first values in the arrays of each row such:
1.5
3
Is this possible in Matlab?
I've used the mean(matrix, 2) command to do this with a matrix of single values, but I'm not sure how to extend this to deal with the arrays.
Get the first elements in all arrays of matrix, then call mean function
mean(matrix(:,:,1))
maybe you need to reshape before call mean
a = matrix(:,:,1);
mean(a(:))
You can apply mean function inside mean function to get the total mean value of the 2D array at index 1. You can do similary with array at index 2. Consider the following snapshot.
After staring at your problem for a long time, it looks like your input is a 3D matrix where each row of your formatting corresponds to a 2D matrix slice. Therefore, in proper MATLAB syntax, your matrix is actually:
M = cat(3, [1,2; 2,3], [5,9; 1,4]);
We thus get:
>> M = cat(3, [1,2; 2,3], [5,9; 1,4])
M(:,:,1) =
1 2
2 3
M(:,:,2) =
5 9
1 4
The first slice is the matrix [1,2; 2,3] and the second slice is [5,9; 1,4]. From what it looks like, you would like the mean of only the first column of every slice and return this as a single vector of values. Therefore, use the mean function and index into the first column for all rows and slices. This will unfortunately become a singleton 3D array so you'll need to squeeze out the singleton dimensions.
Without further ado:
O = squeeze(mean(M(:,1,:)))
We thus get:
>> O = squeeze(mean(M(:,1,:)))
O =
1.5000
3.0000

How to average over the fourth dimension of a 4-D array in R

I have a 4-D array in R and I want to average my array over the fourth dimension so that it gets reduced to a 3-D array. Is there a simple command to do this or is it faster to just do it inside of a for loop?
For example, imagine I have the following 4-D array A and I want some code that would produce the answer in A.mean
A = array(2,c(3,4,2,2))
A[,,,2] = 3
A.mean = array(2.5,c(3,4,2))
We can use
apply(A, c(1,2,3), FUN=mean)

Numpy array - multiplying each column by one another

I would like to know the fastest way to multiply each column of a numpy array by one another and return a new numpy array consisting of these just-built columns - all 2-element combinations from n-element set of columns.
This should be a good starting point
import numpy as np
I, J = np.triu_indices(n)
A[:,I] * A[:,J]

Format of numpy arrays

This is a pretty easy question, I was wondering how to decipher this array:
model[Best[i][j]][6]
Is it recreating another array based off of the 'Best' array within the brackets? I'm not sure how to translate this to myself.
If we are talking about numpy arrays, this will return the value of array model positioned at Best[i][j] (this should be a number perhaps from another array) row and 6th column. Here is an example:
import numpy as np
model = np.array([[1,2],[3,4]])
Best = np.array([[0,0],[1,1]])
i = 0 # Best[i][j] is 0
j = 1
print model[Best[i][j]][1] # It prints model[0][1], which is 2

adding numpy vector to a numpy multi-dimensional array

I have a loop that adds elements to a 1d array:
for i in range(0, 1000):
fvector[0, i] = function_value
after the loop finishes, I have a 1 x 1000 vector that I want to store in a multi-dimensional array fmatrix, which is 50 x 1000. I managed to do this using a loop and copying each element individually - but it is very slow. I've then tried to use slice to copy the whole vector in one go after the loop and then be ready to copy next vector at the next column. How do I make it go to the next column? I've tried:
s=slice([i], None)
fmatrix[s] = fvector
and various combinations for s, but I get error messages about setting an array element with a sequence, or invalid syntax.
I know this should be straight forward but I'm very new to python, numpy and arrays :-(
Try this. Allocate the matrix, here zero-initialized for effect:
>>> import numpy as np
>>> fmatrix = np.zeros((50, 1000))
Then index into it to obtain fvector:
>>> fvector = fmatrix[0]
Then assign to fvector's elements:
>>> for i in xrange(1000):
... fvector[i] = i
If you now inspect fmatrix[0], the first row of fmatrix, you'll find that it has been assigned to in the previous loop. That's because the NumPy row indexing creates fvector as a view on fmatrix's first row. This saves you a copy.
fvector has shape (1,1000). That's a 2D array, even if one axis has length 1.
You can slice it down to a 1D array with fvector[0,:]. This gives the first row.
fmatrix has shape (50,1000). You can slice it down to a 1D array with fmatrix[i,:]. This gives the ith row.
So to assign the values in the first row of fvector to the ith row of fmatrix:
fmatrix[i,:] = fvector[0,:]
Perhaps however there is no need for fvector to be a 2D array? Perhaps just make it a 1D array to begin with:
fvector = np.empty(1000)
for i in range(0, 1000):
fvector[i] = function_value
and then you could do the assignment with
fmatrix[i,:] = fvector

Resources