Replace string in a list using python and save it with different variable

I am trying to replace a string with another string in a list, but failed and I don’t know why. For example, I have a list: i want to replace __label__1 with “OK” and __label__0 with “NOT OK” and save it in different variable using : but it failed to replace any of it Answer The predicts variable has two dimensions, try:

Why is print not printing my array sorted

This is my original array: a = [0, 4, 7, 8, 1, 3, 5, 2, 6] and this is what it prints: [0, 4, 7, 0, 0, 0, 5, 0, 0] Answer As @’Lee Jun Wei’ it doesn’t look like your algo is right. But there may be a few other things to point out. These two lines look off. i and j are taking on the value of the elements of a not the index values. I think you mean The reason you get zeros is because min is initialized to 0. Note that it is zero not because

How to replace values in a np 2d array based on condition for every row

I have a numpy 2d array (named lda_fit) with probabilities, where I want to replace the probabilities with 0 or 1, based on the max value in each line. So after all the first line should look like [0,1,0,0], the second like [1,0,0,0] and so on. I have tried, and this works, but only for a given threshold (0.5): But as I might not have the largest value being greater than 0.5, I want to specify a new threshold for each line. Unfortunately this gives me the max value of the whole array. Answer You can use np.max with specifying

How do I convert an array of seconds to time series array in Python?

I have an array like this (in seconds) timebin= [79203 79213 79223 79233 79243 79253 79263………………82783] and I wish to convert these values to actual time of the day like [22:00:03, 22:00:13,………22:59:43] I have the following code but it doesn’t convert an entire array to time array in one go and only takes single values of timebin. output for now is only the first value of the required time series, i.e, 22:00:03 Answer You’ll want to apply that function to each element in the list. For slightly faster results and a more convenient API, you can vectorize the operation:

Detect duplicate elements python

I get continuously data from a server and can receive the data via the following line of code: After that I would like to write them into a file via: Now the case is, that the same id can appear multiple times, but the value will be a different one. Therefore I would like to extend the out.write into the following way that the different values are added at the right side but still being referred to the same id: Does anyone has an idea how to do this in python? Answer Using the hints that were already added as

Improvement on copy array elements numpy

I have a question regarding variable assignation and memory allocation in a simple case. Imagine that I am initialising a state vector x, with initial value x0. I am then making iterative updates on that state vector with buffer array X and after each iteration, I store the new state vector in a storage list L. An outline of my initial implementation can be found in the following snippet: Which would print that L holds Because when x is appended to L, only the reference to X[-1, :] is added, not the actual value. The solution I could find is

How to add 2D np array to front of 3D np array?

I have a 3D numpy array and I want to add a 2D np array of 0’s to the front of it. I want to add another array B so that: I’ve tried np.append(B,A) but it returns a 2D array. Answer You can do it using numpy.vstack and by reshaping your array. For instance: By the way, you can create your array A more efficiently:

keras lstm error: expected to see 1 array

so i want to make a lstm network to run on my data but i get this message: ValueError: Error when checking input: expected lstm_1_input to have shape (None, 1) but got array with shape (1, 557) this is my code: Answer You need to change the input_shape value for LSTM layer. Also, x_train must have the following shape. So, change to

Working with 2 arrays to populate a third one in numpy

I am trying to work with two arrays in a certain way in python. Lets say now I have a target array C = [2, 7, 15, 25, 40]. I want to find a output value (say Y) for each element in C (say x) with conditions: If x < A[0] then Y = B[0] If A[i] < x < 2*A[i] < A[i+1] or A[i] < x < A[i+1] < 2*A[i] then Y=x If A[i] < 2*A[i] < x < A[i+1] then Y = B[i+1] If x > A[end] then Y = x i is the maximum possible index satisfying

Finding the minimum of the N numpy matrices?

I want to find the minimum of N numpy matrices elementwise (but with a twist, read till the end). To show, I create 3 numpy matrices as follows: I except my output d to be: I also need to retain the information from where does the each element in the d matrix is coming from. So if I label a, b, c as class 0, 1, 2. In the end I need to have a m matrix like this: I prefere no-loops based numpy approach. Answer To find the minimum numbers: And their origins: