I’m supposed to write a helper function that returns a list with an element removed by the value, in an unchanged order. In this case, I don't have to remove any values multiple times.
This is the picture
image of the code
And how do I understand the code here: new_indices= np.delete(indices,np.where(indices==index_to_remove))
Would highly appreciate it if there are examples to help me better understand the code.
indices!=index_to_remove evaluates to an array of booleans, and we are using that boolean array to mask indices. See the numpy docs here
Related
I am trying to find a way to vectorize the following for loop using numpy. This for loop is making my code really drag. The problem that I am having is that I need to look up a value sitting in the dictionary, d, based on the index where the value,val, falls in a range within the array, row.
for i in range(len(row)-1):
if row[i]<val<=row[i+1]:
return d[i]*row[-1]
I would imagine that I could use np.where and np.logical_and to get between two numbers in the array, but then I need the index to grab the value from a dictionary, and that is the part that I just can't seem to figure out without the loop.
Thanks to Divakar's comment, I think that the right answer is to replace the entire for-loop with this numpy monstrosity:
np.vectorize(d1.get)((np.searchsorted(row[:-1],vals,side='left'))-1)*row[-1]
When I try to access multidimensional arrays in slightly different ways I get different results which I do not understand.
when I run:
ells=np.array([1,2,3,4])
check=np.zeros((2,2,2,len(ells)))
print(check[:,:,:,ells<=4].shape)
print(check[0,:,:,ells<=4].shape)
I can actually fix this problem by using
ells=np.array([1,2,3,4])
check=np.zeros((2,2,2,len(ells)))
print(check[:,:,:,ells<=4].shape)
print(check[0,:,:,:][:,:,ells<=4].shape)
however I would like to understand why the first version is wrong.
In the first case I expect to get arrays of shape (2,2,2,4) and (2,2,4) but I get (2,2,2,4) and (4,2,2).
In the second case I get the answers expected, (2,2,2,4) and (2,2,4).
This is an example of mixed advanced and basic indexing:
https://docs.scipy.org/doc/numpy-1.16.1/reference/arrays.indexing.html#combining-advanced-and-basic-indexing
The two outer indices select size 4, and the two inner slices are added after that:
check[0,:,:,ells<=4]
The reason given is that there's a potential ambiguity, when advanced indices are separated by slices. But the case for this ambiguity is weaker when one of the indices is a scalar (that's an old objection).
I'm sure someone could find a duplicate SO.
I was trying to sort a list of lists on the second item in each of the lists in my "unsorted list", and found this piece of code. It works, but even though I have read about lambda function I'm having some problems wrapping my head around how it works. Could someone explain how it works, and maybe give me some input if this is a good way of sorting a list of lists or if i should use a different approach. In advance, thanks!
sorted_list = sorted(unsorted_list,key=lambda l:l[1])
I don't recognize the language, but it seems that the code is sorting an array of arrays, using the element at index 1 as the sorting key.
I'm sorry, I think this is a really trivial question, but...
I have a binary Numpy array, mostly zeros with a few ones. I would like to find the coordinates of all the locations where myArray[myArray == 1]
Please can you help me?
Thanks
np.where(myArray==1)
I guess, should work (assuming its numpy array, based on your indexing example)
If you are using a list (rather than a numpy array), this answer uses enumerate in both a loop and list comprehension: https://stackoverflow.com/a/17202481/1160876
Sorry if this is a simple question, I've tried to look for a solution but can't find anything.
My code goes like this:
given zip1, create an index to select observations (other zipcodes) where some calculation has not been done yet (666)
I = (df['zip1'] == zip1) & (df['Distances'] == 666)
perform some calculation
distances = calc(zip1,df['zip2'][I])
So far so good, I've checked the distances variable, correct values, correct sized array.
put the distance variable in the right place
df['Distances'][I] = distances
but this last part updates all the df['Distances'] variables to nonsense values FOR ALL observations with df['zip1']=zip1 instead of the ones selected by I.
I've checked the boolean array I before the df['Distances'][I] = distances command and it looks fine. Any ideas would be greatly appreciated.
What you are attempting is called chained assignment and does not work the way you think as it returns a copy rather than a view hence the error you see.
There is more information about it here and related issues, this and this.
So you should either use .loc or .ix like so:
df.loc[I,'Distances']=distances