How to replace values in a list at indexed positions? - python-2.7

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.

Related

Pytorch tensor dimension multiplication

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]]]])

theano dot product between a matrix and 3D tensor

I have a matrix and 3D tensor defined as below :
import numpy as np
import theano
import theano.tensor as T
a = T.matrix('a', dtype='float32')
c = T.tensor3('c',dtype='float32')
d = T.batched_dot(c, a)
g = theano.function([a,c],d)
Y = [[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]], [[0, 0 ,0, 0], [0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]]]
X = [[ 0.5052417 , 0.22012063, 0.21787818, 0.41821062, 1, 1, 1, 0], [ 0.48668074, 0.26137591, 0.240702 , 0.41308364, 0, 1, 1, 1]]
x = np.array(X, dtype='float32')
y = np.array(Y, dtype='float32')
print g(x[:,0:4], y)
Although it gives correct answer in the end, but in the middle it shows many error as
ValueError: get_scalar_constant_value detected deterministic IndexError: x.shape[2] when x.ndim=2. x=Subtensor{int64}.0
ERROR (theano.gof.opt): Optimization failure due to: local_gpua_gemmbatch
ERROR (theano.gof.opt): node: BatchedDot(c, a)
ERROR (theano.gof.opt): TRACEBACK:
ValueError: get_scalar_constant_value detected deterministic IndexError: x.shape[2] when x.ndim=2. x=Subtensor{int64}.0
My expected output is
[[ 0.50524169 0.22012062 0.21787818 0. ]
[ 0. 0.2613759 0.240702 0.41308364]]
How can I correctly multiply those two ?

Manipulating a list inside a dictionary (Python 2.7)

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.

Python: turn tuples into array

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]

Change the values of a list?

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]
>>>