how to extract the best population from different arrays? - python

I have a 8-10 sample arrays.
[W, T, I, Z, F]
[a, F, k, D, P]
[A, B, C, D, E]
[W, F, T, P, I]
[W, T, I, Z, F]
[H, c, B, V, C]
...
...
Each array has a unique value.
[0.4729,
0.7075,
0.8611,
0.8512,
0.4769,
0.3074
...
...]
I want to select best population.This population should include 5 different values. Any idea?
Note:If results are near to 1 , this means this population is better.
Thanks,

Related

DataFrame with array of Letters

data['Ln']
Out[46]:
0 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
1 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
2 [C, C, C, C, C, C, G, I, O, P, P, R, R, R, R, ...
3 [C, C, C, C, C, C, G, I, O, P, P, R, R, R, R, ...
4 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
...
43244 [G, I, O, P, P, P, R, R, R, R]
43245 [G, I, O, P, P, P, R, R, R, R]
43246 [G, I, O, P, P, R, R, R]
43247 [G, I, O, P, P, R, R, R]
43248 [G, I, O, P, R, R]
Name: Ln, Length: 43249, dtype: object
How can i structure a for loop to iterate over every row, and every letter either using sklearn.preprocessing.LebelEncoding or ord()?
For instance, I want every 'C' in every row to be the same number, as well as G, I, etc.
Create a dict then map it
alphabet_dict = {'C': 0, 'G': 1, }
data['Ln'].map(lambda x: [alphabet_dict.get(i) for i in x])
0 [0, 0, 0, 0, 0]
1 [1, 1, 1, 1, 1]

Is there a function in Tensorflow can do the following math?

I have two tensors, x and y, of shape [B, D]. I want to do something like the following code
B, D = x.shape
x = tf.expand_dims(x, 1) # [B, 1, D]
y = tf.expand_dims(y, -1) # [B, D, 1]
z = x * y # [B, D, D]
z = tf.reshape(z, (B, D**2))
Is there a function in Tensorflow that already does this?

Index pytorch 4d tensor by values in 2d tensor

I have two pytorch tensors:
X with shape (A, B, C, D)
I with shape (A, B)
Values in I are integers in range [0, C).
What is the most efficient way to get tensor Y with shape (A, B, D), such that:
Y[i][j][k] = X[i][j][ I[i][j] ][k]
You probably want to use torch.gather for the indexing and expand to adjust I to the required size:
eI = I[..., None, None].expand(-1, -1, 1, X.size(3)) # make eI the same for the last dimension
Y = torch.gather(X, dim=2, index=eI).squeeze()
testing the code:
A = 3
B = 4
C = 5
D = 7
X = torch.rand(A, B, C, D)
I = torch.randint(0, C, (A, B), dtype=torch.long)
eI = I[..., None, None].expand(-1, -1, 1, X.size(3))
Y = torch.gather(X, dim=2, index=eI).squeeze()
# manually gather
refY = torch.empty(A, B, D)
for i in range(A):
for j in range(B):
refY[i, j, :] = X[i, j, I[i,j], :]
(refY == Y).all()
# Out[]: tensor(1, dtype=torch.uint8)

Combination with multiple criteria

I have the following three lists of unequal lengths:
a = [2.13, 5.48,-0.58]
b = [4.17, 1.12, 2.13, 3.48,-1.01,-1.17]
c = [6.73, 8, 12]
d = [(2.13,2.13),(5.48,-1.17),(-0.58,4.17)]
e = [(4.17,12),(2.13,6.73)]
I need to create a combination_abc = [ (x,y,z) for x in a
for y in b
for z in c] such that (x,y) is not equal to d and (y,z) is not equal to e
If I understood you correct, just add if-statement into your list comprehension:
[(x, y, z) for x in a for y in b for z in c if (x, y) not in d and (y, z) not in e]
Also you can use itertools.product for simplicity:
from itertools import product
[(x, y, z) for x, y, z in product(a, b, c) if (x, y) not in d and (y, z) not in e]

How to construct the following matrix elegantly in numpy?

Suppose I have a 5 dimensional matrix v and now I want a new matrix D fulfilling
D[a, b, n, m, d] = v[a, b, n, n, d]-v[a, b, m, m, d].
How do I elegantly do this in numpy?
How do you want to change the dimensionality? You can reshape it like this
import numpy as np
a, b, n, d = 2, 3, 4, 5
v = np.zeros((a, b, n, n, d))
D = v.reshape((a, b, n*n, d))
I found einsum can do this:
D = np.einsum('abiic->abic', v)[..., None, :] - np.einsum('abiic->abic', v)[:, :, None, ...]

Categories