How to get a numpy array from a list - python

I have a list
list_num = [4 , 5 , 6]
How to convert it into a numpy array with of shape ( , 3) as when using the function
res = np.array(list_num).shape
output is ( ,3 )

If printing the res variable shape, the result is (3,) and not (,3), so I will understand you have mistyped that.
Just removing the .shape from res, as imM4TT user had cited, the variable will be like you need: res = [4 5 6].
If printing the res.shape, you will obtain (3,).
If you really want to reshape to an array with size (,3), its necessary to use what executable user had cited, the reshape function:
res = res.reshape(1,3)

Related

numpy sum of each array in a list of arrays of different size

Given a list of numpy arrays, each of different length, as that obtained by doing lst = np.array_split(arr, indices), how do I get the sum of every array in the list? (I know how to do it using list-comprehension but I was hoping there was a pure-numpy way to do it).
I thought that this would work:
np.apply_along_axis(lambda arr: arr.sum(), axis=0, arr=lst)
But it doesn't, instead it gives me this error which I don't understand:
ValueError: operands could not be broadcast together with shapes (0,) (12,)
NB: It's an array of sympy objects.
There's a faster way which avoids np.split, and utilizes np.reduceat. We create an ascending array of indices where you want to sum elements with np.append([0], np.cumsum(indices)[:-1]). For proper indexing we need to put a zero in front (and discard the last element, if it covers the full range of the original array.. otherwise just delete the [:-1] indexing). Then we use the np.add ufunc with np.reduceat:
import numpy as np
arr = np.arange(1, 11)
indices = np.array([2, 4, 4])
# this should split like this
# [1 2 | 3 4 5 6 | 7 8 9 10]
np.add.reduceat(arr, np.append([0], np.cumsum(indices)[:-1]))
# array([ 3, 18, 34])

insert 2d arrays into one 3d array

I'm trying to insert some 2d array to a 3d one..
the code is running well but I have difficulties with the insert command.
all of the 2d arrays are correct but when I insert them the 3d array int updating (only zeros)
def binarySeqs (seq_list):
"""taking dna sequences and returning a 3d matrix """
lenght=len(seq_list[0])
num_of_seq=len(seq_list)
for i in seq_list:
if lenght!=len(i):
raise ValueError ("Invalid list of sequences")
for i in seq_list:
if set(i).issubset("ACGT")==False :
raise ValueError ("Invalid list of sequences")
basedict={'A':0 , 'T':1 ,'C':2 , 'G':3}
my_array=np.zeros(( num_of_seq, 4, lenght))
#print(my_array.shape)
for l in seq_list:
dmat=seqBinary(l)
#print(dmat)
for i in range((num_of_seq)):
#print(i)
np.insert(my_array ,i , dmat, axis=0 )
return my_array
numpy.insert is not in-place operation, it does return changed array, consider following example:
import numpy as np
a = np.array([1,2,3])
b = np.insert(a, 1, [5])
print(a) # [1 2 3]
print(b) # [1 5 2 3]
So you should assign changed array back to variable, i.e. instead of
np.insert(my_array ,i , dmat, axis=0 )
do
my_array = np.insert(my_array ,i , dmat, axis=0 )

Covert numpy.ndarray to a list

I'm trying to convert this numpy.ndarray to a list
[[105.53518731]
[106.45317529]
[107.37373843]
[108.00632646]
[108.56373502]
[109.28813113]
[109.75593207]
[110.57458371]
[111.47960639]]
I'm using this function to convert it.
conver = conver.tolist()
the output is this, I'm not sure whether it's a list and if so, can I access its elements by doing cover[0] , etc
[[105.5351873125], [106.45317529411764], [107.37373843478261], [108.00632645652173], [108.56373502040816], [109.28813113157895], [109.75593206666666], [110.57458370833334], [111.47960639393939]]
finally, after I convert it to a list, I try to multiply the list members by 1.05 and get this error!
TypeError: can't multiply sequence by non-int of type 'float'
You start with a 2d array, with shape (n,1), like this:
In [342]: arr = np.random.rand(5,1)*100
In [343]: arr
Out[343]:
array([[95.39049043],
[19.09502087],
[85.45215423],
[94.77657561],
[32.7869103 ]])
tolist produces a list - but it contains lists; each [] layer denotes a list. Notice that the [] nesting matches the array's:
In [344]: arr.tolist()
Out[344]:
[[95.39049043424225],
[19.095020872584335],
[85.4521542296349],
[94.77657561477125],
[32.786910295446425]]
To get a number you have to index through each list layer:
In [345]: arr.tolist()[0]
Out[345]: [95.39049043424225]
In [346]: arr.tolist()[0][0]
Out[346]: 95.39049043424225
In [347]: arr.tolist()[0][0]*1.05
Out[347]: 100.16001495595437
If you first turn the array into a 1d one, the list indexing is simpler:
In [348]: arr.ravel()
Out[348]: array([95.39049043, 19.09502087, 85.45215423, 94.77657561, 32.7869103 ])
In [349]: arr.ravel().tolist()
Out[349]:
[95.39049043424225,
19.095020872584335,
85.4521542296349,
94.77657561477125,
32.786910295446425]
In [350]: arr.ravel().tolist()[0]
Out[350]: 95.39049043424225
But if your primary goal is to multiply the elements, doing with the array is simpler:
In [351]: arr * 1.05
Out[351]:
array([[100.16001496],
[ 20.04977192],
[ 89.72476194],
[ 99.5154044 ],
[ 34.42625581]])
You can access elements of the array with:
In [352]: arr[0,0]
Out[352]: 95.39049043424225
But if you do need to iterate, the tolist() option is good to know. Iterating on lists is usually faster than iterating on an array. With an array you should try to use the fast whole-array methods.
you convert to list of list, so you could not broadcast.
import numpy as np
x = [[105.53518731],
[106.45317529],
[107.37373843],
[108.00632646],
[108.56373502],
[109.28813113],
[109.75593207],
[110.57458371],
[111.47960639],]
x = np.hstack(x)
x * 1.05
array([110.81194668, 111.77583405, 112.74242535, 113.40664278,
113.99192177, 114.75253769, 115.24372867, 116.1033129 ,
117.05358671])
yes, it's a list, you can check the type of a variable:
type(a)
to multiply each element with 1.05 then run the code below:
x = [float(i[0]) * 1.05 for i in a]
print(x)
Try this:
import numpy as np
a = [[105.53518731],
[106.45317529],
[107.37373843],
[108.00632646],
[108.56373502],
[109.28813113],
[109.75593207],
[110.57458371],
[111.47960639]]
b = [elem[0] for elem in a]
b = np.array(b)
print(b*1.05)

create numpy array with varying shape

I want to create a numpy array in order to fill it with numpy arrays. for example:
a = [] (simple array or numpy array)
b = np.array([[5,3],[7,9],[3,8],[2,1]])
a = np.concatenate([a,b])
c = np.array([[1,2],[2,9],[3,0]])
a = np.concatenate([a,c])
I would like to do so because I have wav files from which I extract some features so I can't read from 2 files concurrently but iteratively.
How can I create an empty ndarray with the second dimension fixed e.g. a.shape = (x,2) or how can I concatenate the arrays even without the creation of a "storage" array ?
Actually there are 2 options.
The first one is:
a = np.empty((0, 2)) , which creates an empty np array with the first dimension varying.
The second is to create an empty array
a = [] , append the np arrays in the array and then use np.vstack to concatenate them all together in the end. The latter the most efficient option.
You have to had brackets in concatenate function:
b = np.array([[5,3],[7,9],[3,8],[2,1]])
c = np.array([[1,2],[2,9],[3,0]])
a = np.concatenate([b,c])
Output:
[[5 3]
[7 9]
[3 8]
[2 1]
[1 2]
[2 9]
[3 0]]

Python: Select items from a 2D array based on indices of another 2D array

I'm trying to do a simple task, but can't figure out a quick solution.
I have two 2D arrays of the same size. One array is a mask array that simply contains either 0s or 1s; I want to retain values in another array wherever the mask array index is 1.
The mask array could look like:
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
And say I have a second array of:
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
I want to extract the values [2,2,3,4] from testarr.
Thanks for your help!
If you convert your mask to bools, you can use numpy to do this. First convert your arrays to numpy arrays using maskl = np.array(mask, dtype=bool) and estarray = np.array(estarray). Then:
>>> estarr[mask]
array([2, 2, 3, 4])
You could use zip.
for mask_list,val_list in zip(arr1, arr2):
for m,v in zip(mask_list, val_list):
if m:
# put in results
You could flatten the lists with itertools.chain and then simply iterate over the zipped lists, e.g. like the following:
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
from itertools import chain, izip
print [x for m,x in izip(chain(*mask), chain(*testarr)) if m]
# output:
# [2, 2, 3, 4]
mask = [[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]]
testarr = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]
n = len(mask)
m = len(mask[0])
filtered = []
for i in xrange(n):
for j in xrange(m):
if mask[i][j]:
filtered.append(testarr[i][j])
This is how I'd do it. maybe you can write this as a separate function, and pass in the two lists.
There might be more "elegant" ways, but I don' think there's more "efficient" ways to do it. Personally I like the readability.
You might also check out the numpy.ma module. http://docs.scipy.org/doc/numpy/reference/maskedarray.html
import numpy as np
mask = np.array([[0,0,0,0],[0,0,1,1],[0,0,0,1],[0,0,0,1]],dtype=np.bool)
testarr = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]])
ma = np.ma.array(testarr, mask=np.invert(mask))
print ma[~ma.mask]
output
[2 2 3 4]

Categories