Changing a list of arrays to 2D array - python

I am new to programming in Python. I was trying to use the sorted command to arrange my 2D array from a csv file in ascending order of the first column elements. However, instead of outputting a 2D array, it gives me a list of arrays instead. Here is an example of the problem I faced.
not_ordered_array = [[-0.633, 0.03279], [-7.330e-01, 3.564e-04], [-0.567 , 0.06843], [-7.000e-01, -5.551e-17], [-0.667, 0.006772], [-0.533, 0.06808], [-0.633, 0.03279]]
ordered_array = sorted(not_ordered_array, key = lambda x: x[0], reverse = False)
print(ordered_array)
[array([-7.330e-01, 3.564e-04]), array([-7.000e-01, -5.551e-17]), array([-0.667, 0.006772]), array([-0.633, 0.03279]), array([-0.6, 0.05097]), array([-0.567 , 0.06843]), array([-0.533, 0.06808])]
I am trying to use operations on the ordered_array after this. How do I get the ordered_array in a 2D array instead of a list of arrays?

Related

Getting a single array containing several sub-arrays iteratively

I have a little question about python Numpy. What I want to do is the following:
having two numpy arrays arr1 = [1,2,3] and arr2 = [3,4,5] I would like to obtain a new array arr3 = [[1,2,3],[3,4,5]], but in an iterative way. For a single instance, this is just obtained by typing arr3 = np.array([arr1,arr2]).
What I have instead, are several arrays e.g. [4,3,1 ..], [4,3,5, ...],[1,2,1,...] and I would like to end up with [[4,3,1 ..], [4,3,5, ...],[1,2,1,...]], potentally using a for loop. How should I do this?
EDIT:
Ok I'm trying to add more details to the overall problem. First, I have a list of strings list_strings=['A', 'B','C', 'D', ...]. I'm using a specific method to obtain informative numbers out of a single string, so for example I have method(list_strings[0]) = [1,2,3,...], and I can do this for each single string I have in the initial list.
What I would like to come up with is an iterative for loop to end up having all the numbers extracted from each string in turn in the way I've described at the beginning, i.e.a single array with all the numeric sub-arrays with information extracted from each string. Hope this makes more sense now, and sorry If I haven't explained correctly, I'm really new in programming and trying to figure out stuff.
Well if your strings are in a list, we want to put the arrays that result from calling method in a list as well. Python's list comprehension is a great way to achieve that.
list_strings = ['A', ...]
list_of_converted_strings = [method(item) for item in list_strings]
arr = np.array(list_of_converted_strings)
Numpy arrays are of fixed dimension i.e. for example a 2D numpy array of shape n X m will have n rows and m columns. If you want to convert a list of lists into a numpy array all the the sublists in the main list should be of same length. You cannot convert it into a numpy array if sublist are of varying size.
For example, below code will give an error
np.array([[1], [3,4]]])
so if all the sublist are of same size then you can use
np.array([method(x) for x in strings]])

Python: Collapsing arrays of arrays into each other without for loops

Suppose I have multiple NxN 2D arrays stored into a list in Python 3. I want to collapse all the arrays into 1 array, with the same dimensions NxN, but such that each element of this new array contains a 1xN array of the corresponding values from the original arrays.
To give you some more context, each array in this list corresponds to the set of values at a given time. For each new time point, I am storing the updated version of that array into the list. Once that's done, I want to compute the standard deviation of the values at each (i,j) element in the array.
I tried using a for loop, but it takes far too long for my simulations because this is a set of 100,000 arrays. I was wondering if there were any numpy or vectorized functions that can help me perform this operation more efficiently. Thanks!
Lets say l is your list of arrays. You need to get std of corresponding elements of those arrays into a single array:
std_l = np.std(np.stack(l),axis=0)

Numpy array of multiple indices replace with a different matrix

I have an array of 2d indices.
indices = [[2,4], [6,77], [102,554]]
Now, I have a different 4-dimensional array, arr, and I want to only extract an array (it is an array, since it is 4-dimensional) with corresponding index in the indices array. It is equivalent to the following code.
for i in range(len(indices)):
output[i] = arr[indices[i][0], indices[i][1]]
However, I realized that using explicit for-loop yields a slow result. Is there any built-in numpy API that I can utilized? At this point, I tried using np.choose, np.put, np.take, but did not succeed to yield what I wanted. Thank you!
We need to index into the first two axes with the two columns from indices (thinking of it as an array).
Thus, simply convert to array and index, like so -
indices_arr = np.array(indices)
out = arr[indices_arr[:,0], indices_arr[:,1]]
Or we could extract those directly without converting to array and then index -
d0,d1 = [i[0] for i in indices], [i[1] for i in indices]
out = arr[d0,d1]
Another way to extract the elements would be with conversion to tuple, like so -
out = arr[tuple(indices_arr.T)]
If indices is already an array, skip the conversion process and use indices in places where we had indices_arr.
Try using the take function of numpy arrays. Your code should be something like:
outputarray= np.take(arr,indices)

How do I easily convert a numpy.ndarray to a list of numpy.array?

I am currently struggling to parsing some data into a training framework.
The problem is that the framework is not able to handle ndarray. I need to convert into a list of array. The input and output data is currently stored as two seperate lists of numpy.ndarray.
The input data has to be converted into a list of numpy array where each array contains a column of the ndarray.
The output data has to be converted into a list of numpy arrays where each array contains the rows of the ndarray?..
Is it possible to convert it to this?
when i print train_output_data[0] i get this:
https://ufile.io/fa816
Assuming ip and op are the input list and output lists respectively,
newInput = [ip[:,i] for i in range(ip.shape[0])]
newOutput = [x for x in op]
If the train_output_data and train_input_data are lists of 2D numpy.ndarray's, then the alternative can be
newInput = []
for ip in train_input_data:
newInput.append([ip[:,i] for i in range(ip.shape[0])])
newOutput = []
for op in train_output_data:
newOutput.append([x for x in op])

IndexError when evaluating ndarray with indexes in another ndarray

I'm trying to get values from an ndarray with indices in another ndarray but I keep getting this error
IndexError too many indices for array.
The array that I'm trying to get the values from, scores , has scores.shape = (10,10000)
and the array pointing out the indices, indices , has indices.shape = (10000,2)
I'm trying to get the values this way:
values = scores[tuple(indices)]
but this is where I get the error.
What I'm trying to do this way is to access several individual values of scores, e.g. scores[0,6], scores[1,9] in another array so I get something like
[scores[0,6],scores[1,9],...]
all in one go and avoiding loops. Those [[0,6] , [1,9], ...] are stored in the indices array. I mention the previous in case that could lead to a work around.
Try the following: scores[indices[:,0],indices[:,1]]. Or alternatively, scores[tuple(indices.T)].
When you do scores[tuple(indices)], tuple(indices) is creating a tuple of 2-element arrays. Numpy interprets this as you trying to get 2 elements of a 10,000 dimensional array! For the sort of indexing you need, Numpy expects arrays of values for each dimension. In other words, rather than ( [x1,y1], [x2,y2] ), it wants ( [x1,x2], [y1, y2] ).

Categories

Resources