append vectors to empty list - python-3.x

In a for loop, I read iteratively one vector from file that then I want to put in a list or numpy array. I don't really understand how this process works for numpy arrays or lists. Since I know numpy arrays are not done to change size, I wanted to use an empty list and iteratively append the vector I'm reading
import numpy as np
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
c = timeStep = list()
c = c.append(a)
c = c.append(b)
The example above describes what I would like to do but, when I print the c list after appending a, the terminal shows there is nothing inside.

If You want to create a list I suggest you to use:
c = [] #this is an empty list in Python
Now, if You want to append a numpy array in that list, You should use:
c.append(YourNumpyArray)
Your code should look like this:
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
c = []
c.append(a)
c.append(b)
print(c)
Notice that You don't need to do c = c.append()

As you can see in the figure also, the append doesn't return anything. It appends to the original array c. So you are overwriting the appended array with None.
This should be the working code for you.
import numpy as np
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
c = timeStep = list()
c.append(a)
c.append(b)
On a side note, yes you can use python lists with numpy arrays. If the number of elements in the list c is fixed or pre-known, you can make it a numpy array too.

Related

How to get value from numpy array with dynamic steps?

So i have numpy array like this
x = [1,2,3,4,5]
if i want to get this
x = [1,2,4]
how to produce this result?
I understand the i:j:k syntax but it only have one step but what i want to achieve is dynamic step

Awkward arrays; choosing an item from each row according to a list of indices

So the challenge is this; given an awkward array with n rows and a list of n indices (i_1 to i_n), return a list containing element i_m of row_m for all rows.
This could be done like;
import awkward
some_awkward_array = awkward.fromiter([[1,2],[3,4,5],[6]])
some_indices = [0,2,0]
desired_elements = [row[i] for row, i in zip(some_awkward_array, some_indices)]
assert desired_elements == [1,5,6]
But if this were a numpy array we would have access to choose and so we could do;
import numpy as np
some_numpy_array = np.array([[1,2,0],[3,4,5],[6,0,0]])
some_indices = [0,2,0]
desired_elements = np.choose(some_indices, some_numpy_array.T)
assert desired_elements == [1,5,6]
The second version seems to scale better, it becomes faster somewhere round 12 rows. Is there an equivalent option for an awkward array?
Edit; maybe this is an IndexedMaskedArray thing, but I can't get it to do what I want.

How to split list into numpy array?

A basic question about populating np arrays from a list:
m is a numpy array with shape (4,486,9).
d is a list with length 23328 and a varying number of items for each index.
I am iterating through m on dimension 1 and 2 and d on dimension 1.
I want to import 9 "columns" from particular lines of d at constant intervals, into m. 6 of those columns are successive, they are shown below with index "some_index".
What I have done below works okay but looks really heavy in syntax, and just wrong. There must be a way to export the successive columns more efficiently?
import numpy as np
m=np.empty(4,486,9)
d=[] #list filled in from files
#some_index is an integer incremented in the loops following some conditions
#some_other_index is another integer incremented in the loops following some other conditions
For i in something:
For j in another_thing:
m[i][j]=[d[some_index][-7], d[some_index][-6], d[some_index][-5], d[some_index][-4], d[some_index][-3], d[some_index][-2], d[some_other_index][4], d[some_other_index][0], d[some_other_index][4]]
Without much imagination, I tried the followings which do not work as np array needs a coma to differentiate items:
For i in something:
For j in another_thing:
m[i][j]=[d[some_index][-7:-1], d[some_other_index][4], d[some_other_index][0], d[some_other_index][4]]
ValueError: setting an array element with a sequence.
m[i][j]=[np.asarray(d[some_index][-7:-1]), d[some_other_index][4], d[some_other_index][0], d[some_other_index][4]]
ValueError: setting an array element with a sequence.
Thanks for your help.
Is this what you are looking for?
You can make use of numpy arrays to select multiple elements at once.
I have taken the liberty to create some data in order to make sure we are doing the right thing
import numpy as np
m=np.zeros((4,486,9))
d=[[2,1,2,3,1,12545,45,12], [12,56,34,23,23,6,7,4,173,47,32,3,4], [7,12,23,47,24,13,1,2], [145,45,23,45,56,565,23,2,2],
[54,13,65,47,1,45,45,23], [125,46,5,23,2,24,23,5,7]] #list filled in from files
d = np.asarray([np.asarray(i) for i in d]) # this is where the solution lies
something = [2,3]
another_thing = [10,120,200]
some_index = 0
some_other_index = 5
select_elements = [-7,-6,-5,-4,-3,-2,4,0,4] # this is the order in which you are selecting the elements
for i in something:
for j in another_thing:
print('i:{}, j:{}'.format(i, j))
m[i,j,:]=d[some_index][select_elements]
Also, I noticed you were indexing this way m[i][j] = .... You can do the same with m[i,j,:] = ...

Finding multiple indexes but the array always has a length of 1

This seems trivial (again) but has me stumped.
I need to find the indexes of multiple values in a numpy array. I can do this with where and isin but the resulting answer always has a length of 1 regardless of how many indexes are found. Example
import numpy as np
a = [1,3,5,7,9,11,13,15]
b = [1,7,13]
x = np.where(np.isin(a,b))
print(x)
print(len(x))
this returns
(array([0, 3, 6]),)
1
I think its because the array is a single item inside a tuple. How do I return just the array?
Just use
x = np.where(np.isin(a,b))[0]
to get what you expect.
As hpaulj points out in the comments where returns a tuple with one array for each input array dimension, in this case there is only one, which is why x is a tuple of length one.

setting an array element with a list

I'd like to create a numpy array with 3 columns (not really), the last of which will be a list of variable lengths (really).
N = 2
A = numpy.empty((N, 3))
for i in range(N):
a = random.uniform(0, 1/2)
b = random.uniform(1/2, 1)
c = []
A[i,] = [a, b, c]
Over the course of execution I will then append or remove items from the lists. I used numpy.empty to initialize the array since this is supposed to give an object type, even so I'm getting the 'setting an array with a sequence error'. I know I am, that's what I want to do.
Previous questions on this topic seem to be about avoiding the error; I need to circumvent the error. The real array has 1M+ rows, otherwise I'd consider a dictionary. Ideas?
Initialize A with
A = numpy.empty((N, 3), dtype=object)
per numpy.empty docs. This is more logical than A = numpy.empty((N, 3)).astype(object) which first creates an array of floats (default data type) and only then casts it to object type.

Resources