I'm kinda new to python but trying to catch up and I have a question about manipulating a list inside a dictionary.
Find below the dictionary structure:
{0: ['LU0', 1, 6597604, 7062193, 464590, 0, 0]}
{1: ['LU0', 2, 7392407, 7615509, 223103, 0, 1]}
{2: ['LU0', 3, 1478083, 1978082, 500000, 0, 4]}
{3: ['LU0', 4, 7633406, 7795137, 161732, 0, 5]}
{4: ['LU1', 1, 0, 1023, 1024, 1, 0]}
{5: ['LU1', 2, 0, 1023, 1024, 1, 0]}
{6: ['LU2', 1, 0, 511, 512, 0, 0]}
I'd like to send the [2] and [3] elements inside the list for a specific
function based on the [0] elements, meaning, have a temporary lists which will look like this:
for 'LU0':
[6597604, 7062193, 7392407, 7615509, 1478083, 1978082, 7633406, 7795137]
for 'LU1':
[0, 1023, 0, 1023]
for 'LU2':
[0, 511]
each one of the above will be sent to allocPer(sourceList)
as the sourceList and get back the returned value which will be saved to other
list inside a dictionary as the [5]th element with the following structure (the key is the LUx value):
{0: [7808000, 8, 8, 0, 24, 0]}
{1: [1024, 2, 0, 0, 0, 0]}
{2: [512, 1, 0, 0, 0, 0]}
Thanks in advance ;)
LH
One way to access a list inside a dictionary is the same way you access nested dictionaries, but using the list's index at the appropriate point.
Example: dictname[0][2] and dictname[0][3], but you need to have the nested dictionaries structured properly for access.
Related
I'm trying to implement the grad-camm algorithm:
https://arxiv.org/pdf/1610.02391.pdf
My arguments are:
activations: Tensor with shape torch.Size([1, 512, 14, 14])
alpha values : Tensor with shape torch.Size([512])
I want to multiply each activation (in dimension index 1 (sized 512)) in each corresponding alpha value: for example if the i'th index out of the 512 in the activation is 4 and the i'th alpha value is 5, then my new i'th activation would be 20.
The shape of the output should be torch.Size([1, 512, 14, 14])
Assuming the desired output is of shape (1, 512, 14, 14).
You can achieve this with torch.einsum:
torch.einsum('nchw,c->nchw', x, y)
Or with a simple dot product, but you will first need to add a couple of additional dimensions on y:
x*y[None, :, None, None]
Here's an example with x.shape = (1, 4, 2, 2) and y = (4,):
>>> x = torch.arange(16).reshape(1, 4, 2, 2)
tensor([[[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]],
[[ 8, 9],
[10, 11]],
[[12, 13],
[14, 15]]]])
>>> y = torch.arange(1, 5)
tensor([1, 2, 3, 4])
>>> x*y[None, :, None, None]
tensor([[[[ 0, 1],
[ 2, 3]],
[[ 8, 10],
[12, 14]],
[[24, 27],
[30, 33]],
[[48, 52],
[56, 60]]]])
Currently, I have a list of tuples that looks like this:
[(0, 0.13), (323, 0.72), (812, 0.35), ..., (2127, 0.44)]
The tuples are ordered by their first element: 0 -> 323 -> 812 -> ...
I want to turn this list of tuples into an array (or a sparse matrx), with the first element of each tuple being the second element's array index:
[0.13, 0, ..., 0, 0.72, 0, ..., 0, 0.35, 0, ...]
And to fill the end of this array with 0s to extend it into a certain length.
Can anyone provide a fast implementation of the function above in python?
I currently use a dictionary to accomplish this procedure, and it's very slow for large arrays.
Thank you.
You can preallocate an array of zeros and then fill in the supplied numbers:
def expand_sparse_array(inp):
length = (inp[-1][0]+1) # index of last element + 1
out = [0]*length
for (idx, val) in inp:
out[idx] = val
return out
For example:
>>> expand_sparse_array([(0, 0.13), (3, 0.72), (5, 0.35), (10, 0.44)])
[0.13, 0, 0, 0.72, 0, 0.35, 0, 0, 0, 0, 0.44]
I think this will do what you require:
results = []
for k,i in list_of_tuples:
while k > len(results):
results.append(0)
results.append(i)
Here is a sample run, given an input of [(0, 12), (5, 43), (10, 1)], there result is:
>>> i = []
>>> for k,v in t:
... while k > len(i):
... i.append(0)
... i.append(v)
...
>>> i
[12, 0, 0, 0, 0, 43, 0, 0, 0, 0, 1]
I am new in using python. My problem might seems easy but unfortunately I could not find a solution for it. I have a set of images in Geotiff format which are at the same size, their pixel values range between 0 to 5 and their non values are -9999. I would like to do kind of image stacking using Numpy and Gdal. I am looking for an stacking algorithm in which those pixels of each image that have a value between 0 to 5 are used and the no data values are not used in computing the average. For example if I have 30 images and for two of them the value at the index Image[20,20] are 2 & 3 respectively and for the rest of images it is -9999 at this index. I want the single band output image to be 2.5 at this index. I am wondering if anyone knows the way to do it?
Any suggestions or hints are highly appreciated.
Edit:
let me clarify it a bit more. Here is a sample :
import numpy as np
myArray = np.random.randint(5,size=(3,3,3))
myArray [1,1,1] = -9999
myArray
>> array([[[ 0, 2, 1],
[ 1, 4, 1],
[ 1, 1, 2]],
[[ 4, 2, 0],
[ 3, -9999, 0],
[ 1, 0, 3]],
[[ 2, 0, 3],
[ 1, 3, 4],
[ 2, 4, 3]]])
suppose that myArray is an ndarray which contains three images as follow:
Image_01 = myArray[0]
Image_02 = myArray[1]
Image_03 = myArray[2]
the final stacked image is :
stackedImage = myArray.mean(axis=0)
>> array([[ 2.00000000e+00, 1.33333333e+00, 1.33333333e+00],
[ 1.66666667e+00, -3.33066667e+03, 1.66666667e+00],
[ 1.33333333e+00, 1.66666667e+00, 2.66666667e+00]])
But I want it to be this :
array([[ 2.00000000e+00, 1.33333333e+00, 1.33333333e+00],
[ 1.66666667e+00, 3.5, 1.66666667e+00],
[ 1.33333333e+00, 1.66666667e+00, 2.66666667e+00]])
Masked arrays are a good way to deal with missing or invalid values. Masked arrays have a .data attribute, which contains the numerical value for each element, and a .mask attribute that specifies which values should be considered 'invalid' and ignored.
Here's a full example using your data:
import numpy as np
# your example data, with a bad value at [1, 1, 1]
M = np.array([[[ 0, 2, 1],
[ 1, 4, 1],
[ 1, 1, 2]],
[[ 4, 2, 0],
[ 3, -9999, 0],
[ 1, 0, 3]],
[[ 2, 0, 3],
[ 1, 3, 4],
[ 2, 4, 3]]])
# create a masked array where all of the values in `M` that are equal to
# -9999 are masked
masked_M = np.ma.masked_equal(M, -9999)
# take the mean over the first axis
masked_mean = masked_M.mean(0)
# `masked_mean` is another `np.ma.masked_array`, whose `.data` attribute
# contains the result you're looking for
print masked_mean.data
# [[ 2. 1.33333333 1.33333333]
# [ 1.66666667 3.5 1.66666667]
# [ 1.33333333 1.66666667 2.66666667]]
I have following list of text positions with all values being set to '-999' as default:
List = [(70, 55), (170, 55), (270, 55), (370, 55),
(70, 85), (170, 85), (270, 85), (370, 85)]
for val in List:
self.depth = wx.TextCtrl(panel, -1, value='-999', pos=val, size=(60,25))
I have indexed list and corresponding values at them such as:
indx = ['2','3']
val = ['3.10','4.21']
I want to replace index locations '2' and '3' with values '3.10' and '4.21' respectively in 'List' and keep the rest as '-999'. Any suggestions?
Solved. I used following example:
>>> s, l, m
([5, 4, 3, 2, 1, 0], [0, 1, 3, 5], [0, 0, 0, 0])
>>> d = dict(zip(l, m))
>>> d #dict is better then using two list i think
{0: 0, 1: 0, 3: 0, 5: 0}
>>> [d.get(i, j) for i, j in enumerate(s)]
[0, 0, 3, 0, 1, 0]
from similar question.
liste = [1,2,8,12,19,78,34,197,1,-7,-45,-97,-32,23]
liste2 = []
def repetisjon(liste,liste2):
for count in liste:
if count > 0:
liste2.append(1)
elif count < 0:
liste2.append(0)
return liste2
return (liste2)
print (repetisjon(liste,liste2))
The point is to change all the values of the list. If it's greater than or equal to 0, it is to be replaced by the value 1. And if it's lower than 0, it is to be replaced by 0. But I wasn't able to change the current list. The only solution I found was to make a new list. But is there anyway to CHANGE the current list without making a new one? I tried this as well, but didnt work at all:
liste = [4,8,43,4,78,24,8,45,-78,-6,-7,-3,8,-12,4,36]
def repe (liste):
for count in liste:
if count > 0:
count == 1
else:
count == 0
print (liste)
repe(liste)
Here, I replace the content of liste with the transformed data. since sameliste points to the same list, its value changes too.
>>> sameliste = liste = [1,2,8,12,19,78,34,197,1,-7,-45,-97,-32,23]
>>> sameliste
[1, 2, 8, 12, 19, 78, 34, 197, 1, -7, -45, -97, -32, 23]
>>> liste
[1, 2, 8, 12, 19, 78, 34, 197, 1, -7, -45, -97, -32, 23]
>>> liste[:] = [int(x >= 0) for x in liste]
>>> liste
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1]
>>> sameliste
[1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1]
>>>