Posts

Showing posts with the label numpy

mapping a 2d delay vector from a 1d vector in numpy

mapping a 2d delay vector from a 1d vector in numpy I am trying to generate a 2D vector from a 1D vector where the element is shifted along the row by an increment each row. i would like my input to look like this: input: t = [t1, t2, t3, t4, t5] out = [t5, 0, 0, 0, 0] [t4, t5, 0, 0, 0] [t3, t4, t5, 0, 0] [t2, t3, t4, t5, 0] [t1, t2, t3, t4, t5] [ 0, t1, t2, t3, t4] [ 0, 0, t1, t2, t3] [ 0, 0, 0, t1, t2] [ 0, 0, 0, 0, t1] im unaware of a way to do this without using a for loop, and computational efficieny is important for the task im using this for. Is there a way to do this without a for loop? this is my code using a for loop: import numpy as np t = np.linspace(-3, 3, 7) z = np.zeros((2*len(t) - 1, len(t))) diag = np.arange(len(t)) for index, val in enumerate(np.flip(t, 0)): z[diag + index, diag] = val print(z) 3 Answers 3 What you're asking for here is known as a Toeplitz M...

create structured numpy array in python with strings and int

create structured numpy array in python with strings and int i have this: >>> matriz [['b8:27:eb:d6:e3:10', '0.428s', '198'], ['b8:27:eb:d6:e3:10', '0.428s', '232'], ['b8:27:eb:07:65:ad', '0.796s', '180'], ['b8:27:eb:07:65:ad', '0.796s', '255'], dtype='<U17']` but i need the column `matriz[:, [2]] : [['198'], ['232'], ['180'], ['255']]` to be int and the other columns to be strings, i was trying with structured numpy array but i have this error message, ValueError: invalid literal for int() with base 10: 'b8:27:eb:d6:e3:10' TypeError: a bytes-like object is required, not 'str' i used matriz=np.array(matriz, dtype='U17,U17,i4') i'm using numpy version '1.12.1' for raspberry pi 3, i don't know what i'm doing wrong. thanks a lot ...

Perform operation on elements of numpy array using indexes list

Perform operation on elements of numpy array using indexes list I have numpy array and two python lists of indexes with positions to increase arrays elements by one. Do numpy has some methods to vectorize this operation without use of for loops? for My current slow implementation: a = np.zeros([4,5]) xs = [1,1,1,3] ys = [2,2,3,0] for x,y in zip(xs,ys): # how to do it in numpy way (efficiently)? a[x,y] += 1 print(a) Output: [[0. 0. 0. 0. 0.] [0. 0. 2. 1. 0.] [0. 0. 0. 0. 0.] [1. 0. 0. 0. 0.]] As you treat some indices different than others, this won't be the result of one vectorized operation. You increment one index twice, the others only once. – SpghttCd Jun 30 at 14:36 @SpghttCd Exactly, I want to perform this operation as many times as much occurences are present in index list. ...