How to get value from numpy array with dynamic steps? - python-3.x

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

Related

append vectors to empty list

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.

How can I subtract two elements (type float) in a row using map and a lambda function

I have this curiosity that im trying to kill but i dont know how.
Im trying to subtract to two elements in a row in a list with the type float using map and a lambda function.
li = [12.12,14.11,43.32]
Im doing this but it appears type float not subscriptable.
x = map(lambda y: y[1] - y[0], li)
You can't without doing preprocessing of li (without outright abusing map just for the sake of using it). map is meant to operate on a single element at a time. To use it, you need to make sure that that single element has all the data you need to produce a new element.
Zipping the list with a shifted version of itself works to achieve this:
li = [12.12,14.11,43.32]
shifted = iter(li)
next(shifted) # Drop the first element
neighbors = zip(li, shifted)
map(lambda pair: pair[0] - pair[1], neighbors)
Although, lambda isn't necessary here:
from operator import sub
from itertools import starmap
li = [12.12,14.11,43.32]
shifted = iter(li)
next(shifted)
neighbors = zip(li, shifted)
starmap(sub, neighbors)
starmap and sub make this far cleaner

Combine lists into a meshgrid parameter

I'm using this function that works well to give me a numpy array of all possible combinations of arrays
arrs = np.array(np.meshgrid([1,2,3], [4,5,6], [7,8,9])).T.reshape(-1,3)
But what I would like to do is take the 3 lists and create the meshgrid parameter like this:
lst1=[1,2,3]
lst2=[4,5,6]
lst3=[7,8,9]
arrs = np.array(np.meshgrid(lst)).T.reshape(-1,3)
But how can I create lst from the 3 lists? If I do this
lst=[lst1,lst2,lst3]
all I get is an error. Thank you.
You need to unpack the lst:
# notice the *
arrs = np.array(np.meshgrid(*lst)).T.reshape(-1,3)

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.

Resources