I have a list of 2×1 matrices like: I need to convert it into a tuple of tuples, like: I know I can loop through the list and convert it manually , trying to check if there is a better-optimized way of doing it. Answer You can do it this way using numpy indexing and slicing that outer dimension. Output:
Tag: numpy
Numpy insert matrix values with matrix index
I have the following code which creates a 4D grid matrix and I am looking to insert the rolled 2D vals matrix into this grid. If vals would be a 1D array and I would use a 1D insert_map array as a reference it would work, however using it in multiple dimensions seems to be an issue and it raises
Remove rows in which string contains other letters than A,C,T,G,N
I’m fairly new to numpy and pandas, let’s say that I have a 2D numpy array and I need to delete all rows in which the second value contain only the letters ‘A’, ‘C’, ‘T’, ‘G’ and ‘N’ so after filtering I can get this I wanted to do 3 for …
Performance comparison: Why is it faster to copy an entire numpy Matrix and then change one column than to just use numpy.column_stack?
I am trying to improve the performance of some Python code. In that code, one column of a matrix (numpy-array) has to be changed temporarily. The given code looks as follows: Now I thought it should be a big improvement to not create a copy of the entire matrix A (in the example used, the matrix is 500×5…
2D Vectorization of unique values per row with condition
Consider the array and function definition shown: The point of the function is to return the rows of array a that have exactly grpCount groups of elements that each hold exactly grpSize identical elements. For example: As expected, the code outputs out = [[2, 2, 5, 6, 2, 5], [3, 3, 7, 7, 3, 3]]. The 1st outpu…
How to replace equal consecutive numbers in a list to nan?
I’m trying to replace equal consecutive numbers in a list to nan. I having problems to replace all values when there is a odd number of equal consecutive numbers. This is my code, very simple: Another question, if I want to replace only those values that repeated more than 3 times, for example the numbe…
Optimizing a standard deviation function Pandas Numpy Python
The std pandas function below calculates the standard deviation of every nth value defined by number. So it would take the the values of PC_list with indexes [0,1,2,3,4,5] and calculate the standard deviation and then the indexes [1,2,3,4,5] and calculate the standard deviation until the end of PC_list. I am …
How does numpy avoid copy on access on child process from gc reference counting
On POSIX systems, after you fork(), data should only be copied to the child process after you write to it (copy on write). But because python stores the reference count in the object header, each time you iterate a list in the child process, it will copy it to its memory. Testing that with lists and other dat…
Eucledian distance to point source
I am stimulating a model via a point source, which is located above (z-direction)– to be able to compute the impact of the stimulation i need to compute the eucledian distance from this point power source to each mid of compartment (see picute). I tried it this way, but the results are strange — m…
Getting the minimum indexes with numpy Python
I am trying to get the index values of the function. But I want to get the minimum instead of the maximum values of just like the post: post. I have tried to convert the function below to work for the minimum values unlike the maximum values to getting the indexes. Max Values: Index Function Answer For the mi…