How to remove the double [[ for a 1D matrix in python? - python

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))

Related

Slicing a 2d array in python via another array in one line

Apologies is the title is not correct. I didn't know how to describe exactly what I am looking for.
So coming from Matlab I want to be able to do the following in Python in one line if possible
Say I have an index array:
index_array= np.array([0,1,1,0,0,0,1])
and a data array:
data_array = np.zeros([len(index_array),2])
I want to place a value (e.g. 100) where index_array=0 to the data_array[:,0] and where index_array=1 to data_array[:,1]
I matlab you could do it with one line. Something like
data_array(index_array)=100
The best I could figure out in python is this
data_array [index_array==0,0]=100
data_array [index_array==1,1]=100
Is it possible to do it more efficiently (w.r.t. lines of code). Also it would be nice to scale for additional dimensions in data_array (beyond 2d)
If I understand your question correctly, please try this:
import numpy as np
index_array= np.array([0,1,1,0,0,0,1],dtype=bool)
data_array = np.zeros([len(index_array),2])
data_array[index_array,:]=100
print(data_array)
Here, index_array is instantiated as boolean. Or it can convert index_array to boolean using == (index_array==0).
Use data_array[index_array,...]=100 if there are additional dimensions.
------ CORRECTED VERSION ------
Hope I now understand your question. So try this:
import numpy as np
index_array= np.array([0,1,1,0,2,0,1,2])
data_array = np.zeros([len(index_array),3])
data_array[np.arange(len(index_array)), index_array] = 100
print(data_array)

How do you print out elements from a Numpy array on new lines using a for loop?

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()]

How to transform multi array according to first element in Python?

I have an array like this:
[[0,21],
[1,23],
[1,21],
[0,23],
[1,24]]
what is the fastest way to transform it into two array like these:
a = [21,23]
b = [21,23,24]
I know to do this with looping, is there any library to do this?
You can do this using numpy:-
import numpy
a=[[0,21],
[1,23],
[1,21],
[0,23],
[1,24]]
b=numpy.array(a)
c = numpy.array([[False,True],[False,True],[False,True],[False,True],[False,True]])
print(b[c][:2])
print(b[c][2:])
Alternatively,we can achieve by numpy.arange().We are creating array c from b by giving index of those element which we want from each of the nested arrays. Here we want second element from each of those nested arrays.So here(1,1,1,1,1) in this case.After getting array c,we did slicing as desired.
import numpy
a=[[0,21],
[1,23],
[1,21],
[0,23],
[1,24]]
b=numpy.array(a)
c=b[numpy.arange(len(b)),[1,1,1,1,1]]
print(c[:2])
print(c[2:])

2D python list of numpy arrays to 2D numpy array

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)

Apply function to 3 elements at a time in numpy

I would like to apply a function to a monodimensional array 3 elements at a time, and output for each of them a single element.
for example I have an array of 13 elements:
a = np.arange(13)**2
and I want to apply a function, let's say np.std as an example.
Here is the equivalent list comprehension:
[np.std(a[i:i+3]) for i in range(0, len(a),3)]
[1.6996731711975948,
6.5489609014628334,
11.440668201153674,
16.336734339790461,
0.0]
does anyone know a more efficient way using numpy functions?
The simplest way is to reshape it and apply the function along an axis.
import numpy as np
a = np.arange(12)**2
b = a.reshape(4,3)
print np.std(b, axis=1)
If you need a little better performance than that, you could try stride_tricks. Below is the same as above except using stride_tricks. I was wrong about the performance gain, because as you can see below, b becomes exactly the same view as b above. I wouldn't be surprised if they compiled to exactly the same thing.
import numpy as np
a = np.arange(12)**2
b = np.lib.stride_tricks.as_strided(a, shape=(4,3), strides=(a.itemsize*3, a.itemsize))
print np.std(b, axis=1)
Are you talking about something like vectorize? http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html
You can reshape it. But that does require that the size not change. If you can tack on some bogus entries at the end you can do this:
[np.std(s) for s in a.reshape(-1,3)]

Categories

Resources