Which is the most performant way
to convert something like that
problem = [ [np.array([1,2,3]), np.array([4,5])],
[np.array([6,7,8]), np.array([9,10])]]
into
desired = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
Unfortunately, the final number of columns and rows (and length of subarrays) is not known in advance, as the subarrays are read from a binary file, record by record.
How about this:
problem = [[np.array([1,2,3]), np.array([4,5])],
[np.array([6,7,8]), np.array([9,10])]]
print np.array([np.concatenate(x) for x in problem])
I think this:
print np.array([np.hstack(i) for i in problem])
Using your example, this runs in 0.00022s, wherease concatenate takes 0.00038s
You can also use apply_along_axis although this runs in 0.00024s:
print np.apply_along_axis(np.hstack, 1, problem)
Related
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]])
Create an array with numpy and add elements to it. After you do this, print out all its elements on new lines.
I used the reshape function instead of a for loop. However, I know this would create problems in the long run if I changed my array values.
import numpy as np
a = np.array([0,5,69,5,1])
print(a.reshape(5,1))
How can I make this better? I think a for loop would be best in the long run but how would I implement it?
Some options to print an array "vertically" are:
print(a.reshape(-1, 1)) - You can pass -1 as one dimension,
meaning "expand this dimension to the needed extent".
print(np.expand_dims(a, axis=1)) - Add an extra dimension, at the second place,
so that each row will have a single item. Then print.
print(a[:, None]) - Yet another way of reshaping the array.
Or if you want to print just elements of a 1-D array in a column,
without any surrounding brackets, run just:
for x in a:
print(x)
You could do this:
print(a.reshape([a.shape[0], 1]))
This will work regardless of how many numbers are in your numpy array.
Alternatively, you could also do this:
[print(number) for number in a.tolist()]
I am working on a Deep Learning project (in Python), and I had a problem with my code. I get an output like this:
[[-0.00111287 -0.97692661 -0.9939433 -0.99474857]]
and I want to change that to this:
[-0.00111287 -0.97692661 -0.9939433 -0.99474857]
Can anyone help me with a simple function. I could not find any function that did the job.
Thanks in advance!
I assume that you have a numpy.ndarray? You can flatten any array into 1D by doing array.ravel(). Additionally, you can reshape an array into any desired shape (compatible with the number of elements in the array) using array.reshape. array.reshape(-1) is the same as array.ravel(). See here for more on reshaping arrays. Finally, you could also get array_1d = array_2d[0], but I don't find this as clear in code because it seems to the reader like you are just selecting the first row of a 2D array. Doing array_1d = array_2d.ravel() makes it clear that you are keeping all elements, just flattening the array.
Try this:
import numpy as np
my_matrix = [[-0.00111287, -0.97692661, -0.9939433, -0.99474857]]
result = list(np.array(my_matrix).reshape(-1))
print(result)
# [-0.00111287, -0.97692661, -0.9939433, -0.99474857]
If you're not working with a numpy array, itertools provides a function called chain() that can be used to flatten a list:
from itertools import chain
array = list(chain(*matrix))
In Numpy I have two three dimensional arrays representing images. I'm trying to create an overlay of the second image on to first so I'd like to replace all of the elements in the first array with respective elements from the second array but only when they aren't zero. Is there any easy way to do this?
This seems like a perfect use-case for np.where ...
new_arr = np.where(second == 0, first, second)
I've done the replacement out-of-place (creating a new array rather than modifying the original), but that's usually OK...
You can simply do:
zeros_idx = array2 != 0
array1[zeros_idx] = array2[zeros_idx]
Modifying the original using numpy.nonzero. Similar to answer provided by #Holt .
m = numpy.nonzero(array2)
array1[m] = array2[m]
I'm trying to use numpy's savetxt function to generate a bunch of files as inputs for another piece of software.
I'm trying to write an array of the form:
a=np.array([1,2,3,4,...])
a.shape=>(1,n)
to a text file with the formatting
1,2,3,4,...
when I enter the command
np.savetxt('test.csv',a,fmt='%d',delimiter=',')
I get a file that looks like:
1
2
3
4
...
savetxt works as I would expect for a 2D array, but I can't get all of the values for a 1D array onto a single line
Any suggestions?
Thanks
EDIT:
I solved the problem. Using np.atleast_2d(a) as the input to savetxt forces savetxt to write the array as a row, not a column
There are different ways to fix this. The one closest to your current approach is:
np.savetxt('test.csv', a[None], fmt='%d', delimiter=',')
i.e. add the slicing [None] to your array to make it two-dimensional with only a single line.
If you only want to save a 1D array, it's actually a lot faster to use this method:
>>> x = numpy.array([0,1,2,3,4,5])
>>> ','.join(map(str, x.tolist()))
'0,1,2,3,4,5'