Python numpy convert 2d array in 1d array sorted - python

I have a 2D numpy array and I want to change this array into a 1D which is sorted. For example:
A = [[1,0,2],
[0,3,0]]
I want this to be like:
B = [3,2,1,0,0,0]
Any idea how I can do this using python modules and not to write a sorting algorithm or anything like that ?
Thanks

Assuming you are looking to sort them in descending order -
In [127]: A
Out[127]: [[1, 0, 2], [0, 3, 0]]
In [128]: B = np.sort(np.array(A).ravel())
In [129]: B[::-1]
Out[129]: array([3, 2, 1, 0, 0, 0])
Basically, it involves three steps: Flatten the array with ravel(), sort it with np.sort and then reverse the indexing for an effect of descending order sorting.

This is my solution:
A = [[1,0,2], [0,3,0]]
B = []
for i in range(len(A)):
for j in range(len(A[i])):
B.append(A[i][j])
B.sort(cmp=None, key=None, reverse=True)
print B
You can see my code running here: http://ideone.com/P8xBPK

Related

Accessing a numpy array with an array of indices

I'm trying to access a numpy array A using another array B providing the indices at each position:
A = np.array([[1,2],[3,4]])
B = np.array([[[0,0],[0,0]],[[0,1],[0,1]]])
Desired output:
C = array([[1,1],[3,3]])
I haven't gotten it to work using np.take() or the advanced indexing.
I could do it iteratively but my arrays are on the order of 10**7 so I was hoping for a faster way.
I probably should have insisted on seeing the iterative solution first, but here's the array one:
In [45]: A[B[:,:,1], B[:,:,0]]
Out[45]:
array([[1, 1],
[3, 3]])
I first tried A[B[:,:,0], B[:,:,1]], the natural order of the inner dimension. Your own code could have saved me that trial.
The key with advanced indexing is that you have to create or define separate arrays (broadcastable) for each dimension. We can think of that index as a tuple:
idx = (B[:,:,0], B[:,:,1])
A[idx]
Adding on #hpaulj an alternative way is:
idx = tuple(B[:,:,[1,0]].transpose(2,0,1))
A[idx]
# array([[1, 1], [3, 3]])

Iterating through rows in numpy array with one row

For a 2D numpy array A, the loop for a in A will loop through all the rows in A. This functionality is what I want for my code, but I'm having difficulty with the edge case where A only has one row (i.e., is essentially a 1-dimensional array). In this case, the for loop treats A as a 1D array and iterates through its elements. What I want to instead happen in this case is a natural extension of the 2D case, where the loop retrieves the (single) row in A. Is there a way to format the array A such that the for loop functions like this?
Depending on if you declare the array yourself you can do this:
A = np.array([[1, 2, 3]])
Else you can check the dim of your array before iterating over it
B = np.array([1, 2, 3])
if B.ndim == 1:
B = B[None, :]
Or you can use the function np.at_least2d
C = np.array([1, 2, 3])
C = np.atleast_2d(C)
If your array trully is a 2D array, even with one row, there is no edge case:
import numpy
a = numpy.array([[1, 2, 3]])
for line in a:
print(line)
>>> [1 2 3]
You seem to be confusing numpy.array([[1, 2, 3]]) which is a 2D array of one line and numpy.array([1, 2, 3]) which would be a 1D array.
I think you can use np.expand_dims to achieve your goal
X = np.expand_dims(X, axis=0)

Numpy: How to stack arrays in columns?

Let's say that I have n numpy arrays of the same length. I would like to now create a numpy matrix, sucht that each column of the matrix is one of the numpy arrays. How can I achieve this? Now I'm doing this in a loop and it produces the wrong results.
Note: I have to be able to stack them next to each other one by one iteratively.
my code looks like assume that get_array is a function that returns a certain array based on its argument. I don't know until after the loop, how many columns that I'm going to have.
matrix = np.empty((n_rows,))
for item in sorted_arrays:
array = get_array(item)
matrix = np.vstack((matrix,array))
any help would be appreciated
You could try putting all your arrays (or lists) into a matrix and then transposing it. This will work if all arrays are the same length.
mymatrix = np.asmatrix((array1, array2, array3)) #... putting arrays into matrix.
mymatrix = mymatrix.transpose()
This should output a matrix with each array as a column. Hope this helps.
Time and again, we recommend collecting the arrays in a list, and making the final array with one call. That's more efficient, and usually easier to get right.
alist = []
for item in sorted_arrays:
alist.append(get_array(item)
or
alist = [get_array(item) for item in sorted_arrays]
There are various ways of assembling the list. Since you want columns, and assuming get_array produces equal sized 1d arrays:
arr = np.column_stack(alist)
Collecting them in rows and transposing that works too:
arr = np.array(alist).T
arr = np.vstack(alist).T
arr = np.stack(alist).T
arr = np.stack(alist, axis=1)
If the arrays are already 2d
arr = np.concatenate(alist, axis=1)
All the stack variations use concatenate, just varying in how they tweak the shape(s) of the input arrays. The key to using concatenate is to understand the dimensions and shapes, and how to add dimensions as needed. That should, soon or later, become fluent in that kind of coding.
If they vary in shape or dimensions, things get messier.
Equally good is to put the arrays in a pre-allocated array. But you need to know the desired final shape
arr = np.zeros((m,n), dtype)
for i, item in enumerate(sorted_arrays):
arr[:,i] = get_array(item)
n is len(sorted_arrays), and m is the length of one of get_array(item). You also need to know the expected dtype (int, float etc).
If you have a, b, c, d np array of same length, the following code will accomplish what you want:
out_matrix = np.vstack([a, b, c, d]).transpose()
An example:
In [3]: a = np.array([1, 2, 3, 4])
In [4]: b = np.array([5, 6, 7, 8])
In [5]: c = np.array([2, 3, 4, 5])
In [6]: d = np.array([6, 8, 2, 4])
In [10]: np.vstack([a, b, c, d]).transpose()
Out[10]:
array([[1, 5, 2, 6],
[2, 6, 3, 8],
[3, 7, 4, 2],
[4, 8, 5, 4]])

Recover data from lists in Python

I have two lists in python, say A and B.
List A is a list of lists with some integer indexes, for example [[2,3,1,3,2,3,3], [4,2,1,4],[5,4,3,3,3,4,]...] and so on.
List B has the same structure but instead of integers has numpy arrays.
[[array([0, 0]), array([0, 0]), array([0, 1]) ...][[array([0, 0]), array([0, 1])...]...]
These lists are correlated, so each numpy array corresponds to a integer, in other words, the sublists of A and the sublists of B has the same size. For example
[[2,3,3,3,2,4]...]
[[array([0, 0]), array([0, 1]), array([0, 1]), array([0, 1]), array([0, 0]), array([1, 0])]...]
The first integer in the sublist of A, "2" is linked to the first numpy array in the sublist of B.
As you can see there are repeated integers, and in consequence repeated numpy arrays. I want to recover the unique indexes without repeating, and thus their corresponding arrays.
Taking the example above the return of the procedure should be something like this:
[[2,3,4]...]
[[array([0, 0]), array([0, 1]), array([1, 0])]...]
How can I recover the unique elements from list A with their corresponded numpy array in list b?
My first attempt used the numpy.unique function so i can recover list A efficiently, but then I lose the information to recover the information from B. The line was
A = np.array([np.unique(a) for a in A])
TO RECAP
I have the following
import numpy as np
A = [] # PUT A REAL (short) LIST HERE
B = [] # PUT A REAL (short) LIST HERE
uniqueA = np.array([np.unique(a) for a in A])
print uniqueA #prints what I want/dont want
expectA = [1,4] # put what you would expect to get back
#ask additional questions here
First, get unique indices of A, then take them from A and B
import numpy as np
A = [[2,3,1,3,2,3,3], [4,2,1,4]]
B = [[np.zeros(2)]*len(A[0]), [np.zeros(2)]*len(A[1])]
indices = np.array([np.unique(a, True)[1] for a in A])
A = np.array([np.array(arr)[index] for arr, index in zip(A, indices)])
B = np.array([np.array(arr)[index] for arr, index in zip(B, indices)])

Acquiring the Minimum array out of Multiple Arrays by order in Python

Say that I have 4 numpy arrays
[1,2,3]
[2,3,1]
[3,2,1]
[1,3,2]
In this case, I've determined [1,2,3] is the "minimum array" for my purposes, as it is one of two arrays with lowest value at index 0, and of those two arrays it has the the lowest index 1. If there were more arrays with similar values, I would need to compare the next index values, and so on.
How can I extract the array [1,2,3] in that same order from the pile?
How can I extend that to x arrays of size n?
Thanks
Using the python non-numpy .sort() or sorted() on a list of lists (not numpy arrays) automatically does this e.g.
a = [[1,2,3],[2,3,1],[3,2,1],[1,3,2]]
a.sort()
gives
[[1,2,3],[1,3,2],[2,3,1],[3,2,1]]
The numpy sort seems to only sort the subarrays recursively so it seems the best way would be to convert it to a python list first. Assuming you have an array of arrays you want to pick the minimum of you could get the minimum as
sorted(a.tolist())[0]
As someone pointed out you could also do min(a.tolist()) which uses the same type of comparisons as sort, and would be faster for large arrays (linear vs n log n asymptotic run time).
Here's an idea using numpy:
import numpy
a = numpy.array([[1,2,3],[2,3,1],[3,2,1],[1,3,2]])
col = 0
while a.shape[0] > 1:
b = numpy.argmin(a[:,col:], axis=1)
a = a[b == numpy.min(b)]
col += 1
print a
This checks column by column until only one row is left.
numpy's lexsort is close to what you want. It sorts on the last key first, but that's easy to get around:
>>> a = np.array([[1,2,3],[2,3,1],[3,2,1],[1,3,2]])
>>> order = np.lexsort(a[:, ::-1].T)
>>> order
array([0, 3, 1, 2])
>>> a[order]
array([[1, 2, 3],
[1, 3, 2],
[2, 3, 1],
[3, 2, 1]])

Categories