Python - Rewrite a 2D list / numpy array in the Console - list

I want to rewrite/replace a 2d list or numpy array (whatever is easier) in the console:
e.g.:
[[_,_,_],
[_,_,_],
[x,_,_]]
will be replaced with
[[_,_,_],
[x,_,_],
[_,_,_]]
which will be replaced with
[[x,_,_],
[_,_,_],
[_,_,_]]
and so on...so it looks like the x is moving across the "board".
I already wrote the function that enables me to print the lists one after the other but i would rather replace them in the console output.
thanks in advance for help!

import os
import time
import numpy as np
def cls():
os.system('cls' if os.name=='nt' else 'clear')
x0 = np.array([[0, 0, 0],
[0, 0, 0],
[1, 0, 0]])
x1 = np.array([[0, 0, 0],
[2, 0, 0],
[0, 0, 0]])
x2 = np.array([[3, 0, 0],
[0, 0, 0],
[0, 0, 0]])
print(x0)
time.sleep(1)
cls()
print(x1)
time.sleep(1)
cls()
print(x2)
see How to clear the interpreter console?

Related

InvalidArgumentError indices[i,0] = x is not in [0, x) in keras

I have the code using keras 1.2 and tensorflow 1.1. I have run it but with error
import numpy as np
import keras
from keras import backend as K
from keras import initializers
from keras.models import Sequential, Model, load_model, save_model
from keras.layers.core import Dense, Lambda, Activation
from keras.layers import Embedding, Input, Dense, Multiply, Reshape, Flatten
from keras.optimizers import Adagrad, Adam, SGD, RMSprop
from keras.regularizers import l2
from sklearn.metrics import average_precision_score
from sklearn.metrics import auc
def init_normal(shape, name=None):
return initializers.lecun_uniform(seed=None)
def get_model(num_a, num_b, num_c, dim, regs=[0,0,0]):
a = Input(shape=(1,), dtype='int32', name = 'a')
b = Input(shape=(1,), dtype='int32', name = 'b')
c = Input(shape=(1,), dtype='int32', name = 'c')
Embedding_a = Embedding(input_dim = num_a, output_dim = dim,
embeddings_initializer='uniform', W_regularizer = l2(regs[0]), input_length=1)
Embedding_b = Embedding(input_dim = num_b, output_dim = dim,
embeddings_initializer='uniform', W_regularizer = l2(regs[1]), input_length=1)
Embedding_c = Embedding(input_dim = num_c, output_dim = dim,
embeddings_initializer='uniform', W_regularizer = l2(regs[2]), input_length=1)
a_latent = Flatten()(Embedding_a(a))
b_latent = Flatten()(Embedding_b(b))
c_latent = Flatten()(Embedding_c(c))
predict_vector = Multiply()([a_latent, b_latent, b_latent])
prediction = Dense(1, activation='sigmoid', init='lecun_uniform', name = 'prediction')(predict_vector)
model = Model(input=[a, b, c], output=prediction)
return model
def evaluate_model(model, test_pos, test_neg):
global _model
global _test_pos
global _test_neg
_model = model
_test_pos = test_pos
_test_neg = test_neg
print(_test_neg)
a, b, c, labels = [],[],[],[]
for item in _test_pos:
a.append(item[0])
b.append(item[1])
c.append(item[2])
labels.append(1)
for item in _test_neg:
a.append(item[0])
b.append(item[1])
c.append(item[2])
labels.append(0)
a = np.array(a)
b = np.array(b)
c = np.array(c)
predictions = _model.predict([a, b, c],
batch_size=100, verbose=0)
return average_precision_score(labels, predictions), auc(labels, predictions)
model = get_model(4, 8, 12, 2, [0,0,0])
model.compile(optimizer=Adam(lr=0.001), loss='binary_crossentropy')
pos_test = [[0, 0, 2], [4, 8, 8], [2, 5, 4], [0, 0, 0]]
neg_test = [[3, 3, 2], [2, 1, 8], [1, 4, 1], [3, 3, 12]]
aupr, auc = evaluate_model(model, pos_test, neg_test)
print(aupr, auc)
However, It give me error:any way to fix it?
InvalidArgumentError (see above for traceback): indices[1,0] = 4 is not in [0, 4)
[[Node: embedding_4/embedding_lookup = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:#embedding_4/embeddings"], validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](embedding_4/embeddings/read, _recv_a_1_0)]]
The problem is, you defined embedding input_dim as 4, 8 and 12 while it should be is 5, 9, 13. Because input_dim in embedding should be max_index + 1. It is also clearly mentioned in Keras docs:
Size of the vocabulary, i.e. maximum integer index + 1.
How to fix the issue?
Change get_model method to:
model = get_model(5, 9, 13, 2, [0, 0, 0])
Or alternatively change index of data to:
pos_test = [[0, 0, 2], [3, 7, 7], [2, 5, 4], [0, 0, 0]]
neg_test = [[3, 3, 2], [2, 1, 7], [1, 4, 1], [3, 3, 11]]

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.

eliminate selected column of elements from a list of lists

I have a list of lists where each individual list has 3 elements. Something like this:
[[928.7, 554.29999958311, 0],
[928.7, 558.15990063549, 0],
[914.1, 558.15990063549, 0],
[914.1, 554.29999958311, 0]]
How can I delete all the elements from a particular column? For example if I input "1" that will delete the first column, if I input "2" it will delete the second one and so on.
I assume your question regards pyhton...
I would try something like the following (using numpy):
import numpy as np
initial_list = [[928.7, 554.29999958311, 0],
[928.7, 558.15990063549, 0],
[914.1, 558.15990063549, 0],
[914.1, 554.29999958311, 0]]
# transform the list in a numpy array
a = np.array(initial_list)
# remove the column you want and put the output in a new variable
a1 = np.delete(a, 0, 1) # this would the remove the first column(0)
#+the second "1" in the arguments tells to
#+numpy to delete the column instead of the
#+ row.
# convert back to a plain list
final_list = a1.tolist()
If you want to stay with plain python, I would suggest something like:
initial_list = [[928.7, 554.29999958311, 0],
[928.7, 558.15990063549, 0],
[914.1, 558.15990063549, 0],
[914.1, 554.29999958311, 0]]
for row in initial_list:
del row[0] # This would delete the first column from your matrix
final_list = initial_list
Pay attention to the fact that the latter method will "overwrite" the original list and you will loose all the deleted data. Consider, if you need, to create a copy of the initial_list:
initial_list_bck[:] = initial_list[:]
# or
initial_list_bck = initial_list.copy()
# The following would create only a pointer to the first list
initial_list_bck = initial_list
Hope to be helpful.
Iterate through the list of lists. White iterating, remove the nth item.
a = [[928.7, 554.29999958311, 0],
[928.7, 558.15990063549, 0],
[914.1, 558.15990063549, 0],
[914.1, 554.29999958311, 0]]
column_number = 1
for i in range(0, len(a)):
a[i].remove(a[i][column_number])
print a

How to replace values in a list at indexed positions?

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.