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.
Related
I have a table of permissible stress (pink) values for slenderness ratio (green) and yield strength (blue) from local govt byelaws.
Taking values using df.at[A, B], where A and B are variables coming from pre-processes, say A = 100 and B = 250, I want to take values 110 from (green) and 72 (from pink).
What I have tried:
I need these values for interpolation, I have tried interpolation methods which come with pandas, but for the particular use case, I need to interpolate these values mathematically.
I have also tried taking values from index, by first finding index of the value, and then adding 1 to the index, but that method is not a viable option for various reasons.
I have also tried simply adding the column value interval to the A and B values, but as can be observed, while the green values (A) is uniform and incremental, the value intervals for yield strength (B) (in blue), while incremental, is not uniform.
[edit 2]
I am have tried df.where, There should be another way to find co-ordinates of a value.
I have been stuck for a while, any help/suggestions will be appreciated! Thanks!
Not sure if I understood what you are trying to achieve. If I got it right, you want to get the pink value based on the slenderness ratio index that follows a certain value und a yield strength at a specific value (or column).
df.loc[np.roll(df.index == A, shift=1), B]
This would shift the logical index (green) by one.
I understand that
np.argmax(np.max(x, axis=1))
returns the index of the row that contains the maximum value and
np.argmax(np.max(x, axis=0))
returns the index of the row that contains the maximum value.
But what if the matrix contained strings? How can I change the code so that it still finds the index of the largest value?
Also (if there's no way to do what I previously asked for), can I change the code so that the operation is only carried out on a sub-section of the matrix, for instance, on the bottom right '2x2' sub-matrix in this example:
array = [['D','F,'J'],
['K',3,4],
['B',3,1]]
[[3,4],
[3,1]]
Can you try first converting the column to type dtype? If you take the min/max of a dtype column, it should use string values for the minimum/maximum.
Although not efficient, this could be one way to find index of the maximum number in the original matrix by using slices:
newmax=0
newmaxrow=0
newmaxcolumn=0
for row in [array[i][1:] for i in range(1,2)]:
for num in row:
if num>newmax:
newmax=num
newmaxcolumn=row.index(newmax)+1
newmaxrow=[array[i][1:] for i in range(1,2)].index(row)+1
Note: this method would not work if the lagest number lies within row 0 or column 0.
So I am trying to get the sum over a specific range of values in a text file using:
np.sum(d[a:b])
I am using a text file with 10000 entries. I know that we always start at zero. So my range is quite large i.e; index 200-555 (including 200 and 555). I tried just for testing summing over a small range:
In [17]: np.sum(d[1:4])
Out[17]: 50.164228
But the above code summed from the 2nd block (labeled number 1 by python) until the third. The numbers are; (0-> 13.024)
, 1-> 17.4529, 2-> 16.9382, 3-> 15.7731,( 4-> 11.7589), 5-> 14.5178.
zero is just for reference and it ignored the 4th-> 11.7589. Why?
When using range indexing in Python, the second index (the 4 in your case) is not an inclusive index. By specifying [1:4], you're summing the elements from index 1 up to but not including index 4. Specify 5 as the second index if you want to include the element at index 4.
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]]
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).