I want to find quantiles of element n in sublists.
Let's say I have (in reality it's much bigger):
List=[[[1,3,0,1],[1,2,0,1],[1,3,0,1]],[[2,2,1,0],[2,2,1,0],[2,2,1,0]]]
I want a way to find quantiles (like numpy.percentile) for the 2:nd elements in the sublist [[1,3,1,1],[1,2,0,1],[9,3,2,1]] and in [[1,2,3,4],[0,2,0,0],[1,2,2,2]] and then I want to do a maximum function so I know which subgroup of those two had the highest chosen quantile, and I also want to know the values the other 3 constant values (1:st, 3:rd and 4:th elements) has at that maximum.
Here's one possible way. Assuming (as in your question)
List=[[[1,3,0,1],[1,2,0,1],[1,3,0,1]],[[2,2,1,0],[2,2,1,0],[2,2,1,0]]]
Then one can convert each first-level tuple to a numpy matrix first, which allows easily selecting the 2nd column, to which one can apply the numpy.percentile function. Shortly,
import numpy as np
quartiles = [np.percentile(np.matrix(l)[:,1], 25) for l in List]
which gives as output the quartiles (25-percentiles) of each first-level tuple:
[2.5, 2.0]
One can then find the maximum with numpy.argmax:
am = np.argmax(quartiles)
and then use it to select the other 3 constant elements
other3 = [List[am][0][0], List[am][0][2], List[am][0][3]]
Related
I would like to generate a list of random values with it's highest value at given index and decreasing values towards start and end of the list. E.g. if I have 1 dim 10 element long list and I'm at index 3 then this would be my highest value and other values decreases towards index 0 and index 9. So the two primary parameters would be list length and Index of top value. It would be also nice to control random value range and mean of the list.
Would anyone know function (combination of functions) from numpy / scipy etc. that would satisfy this case? I was looking at numpy's different kind of norm functions but this is not what I'm looking for.
Code:
import numpy as np
ray = [1,22,33,42,51], [61,71,812,92,103], [113,121,132,143,151], [16,172,183,19,201]
ray = np.asarray(ray)
type(ray)
ray[np.ix_([-2:],[3:4])]
I'd like to use index slicing and get a subarray consisting of the last two rows and the 3rd/4th columns. My current code produces an error:
I'd also like to sum each column. What am I doing wrong? I cannot post a picture because I need at least 10 reputation points.
So you want to make a slice of an array. The most straightforward way to do it is... slicing:
slice = ray[-2:,3:]
or if you want it explicitly
slice = ray[-2:,3:5]
See it explained in Understanding slicing
But if you do want to use np.ix_ for some reason, you need
slice = ray[np.ix_([-2,-1],[3,4])]
You can't use : here, because [] here don't make a slice, they construct lists and you should specify explicitly every row number and every column number you want in the result. If there are too many consecutive indices, you may use range:
slice = ray[np.ix_(range(-2, 0),range(3, 5))]
And to sum each column:
slice.sum(0)
0 means you want to reduce the 0th dimension (rows) by summation and keep other dimensions (columns in this case).
I have a problem. How to find max and min of matrix in Python without using numpy, just old and slow way. I need to display their positions as well.
For 2D numerical lists, a method for what you want, is list comprehension:
mat=[[3,1.5,4,2],[5,9,3,8]]
_min_=min([(min(element),mat.index(element),element.index(min(element))) for element in mat])
print(_min_) # prints (1.5,0,1)
where the first to third elements in the output represent the minimum, the index of row of mat to which that minimum belongs and the index of column of mat to which that minimum belongs.
You can generalize this method for any n-dimensional list if you wish.
I need to find minimum over all elements from the column which has the maximum column sum.
I do the following things:
Create random matrix
from numpy import *
a = random.rand(5,4)
Then calculate sum of each column and find index of the maximum element
c = a.sum(axis=0)
d = argmax(c)
Then I try to find the minimum number in this column, but I am quite bad with syntax, I know how to find the minimum element in the row with current index.
e = min(a[d])
But how can I change it for columns?
You can extract the minimum value of a column as follows (using the variables you have indicated):
e=a[:,d].min()
Note that using
a=min(a[:,d])
will break you out of Numpy, slowing things down (thanks for pointing this out #SaulloCastro).
I have a largish 2d numpy array, and I want to extract the lowest 10 elements of each row as well as their indexes. Since my array is largish, I would prefer not to sort the whole array.
I heard about the argpartition() function, with which I can get the indexes of the lowest 10 elements:
top10indexes = np.argpartition(myBigArray,10)[:,:10]
Note that argpartition() partitions axis -1 by default, which is what I want. The result here has the same shape as myBigArray containing indexes into the respective rows such that the first 10 indexes point to the 10 lowest values.
How can I now extract the elements of myBigArray corresponding to those indexes?
Obvious fancy indexing like myBigArray[top10indexes] or myBigArray[:,top10indexes] do something quite different. I could also use list comprehensions, something like:
array([row[idxs] for row,idxs in zip(myBigArray,top10indexes)])
but that would incur a performance hit iterating numpy rows and converting the result back to an array.
nb: I could just use np.partition() to get the values, and they may even correspond to the indexes (or may not..), but I don't want to do the partition twice if I can avoid it.
You can avoid using the flattened copies and the need to extract all the values by doing:
num = 10
top = np.argpartition(myBigArray, num, axis=1)[:, :num]
myBigArray[np.arange(myBigArray.shape[0])[:, None], top]
For NumPy >= 1.9.0 this will be very efficient and comparable to np.take().